How to Properly Resize and Populate a Vector in C++ Without Using push_back?

  • C/C++
  • Thread starter EvanET
  • Start date
  • Tags
    C++ Vector
To populate the vector with integers, we can use a for loop to assign the values in reverse order, starting from newSize and ending at 1. Then, we can print out the elements of the vector using a for loop as well. Finally, we can add the "Go!" message at the end. In summary, to resize and populate a vector without using push_back, we can call resize or re-create the vector with the desired size and then use a for loop to assign values and print them out.
  • #1
EvanET
10
0
Resize vector countDown to have newSize elements. Populate the vector with integers {newSize, newSize - 1, ..., 1}. Ex: If newSize = 3, then countDown = {3, 2, 1}, and the sample program outputs:

3 2 1 Go!

**I want to do this WITHOUT push_back**
#include <iostream>
#include <vector>
using namespace std;

int main() {
vector<int> countDown(0);
int newSize = 0;
int i = 0;

newSize = 3;

//enter code here

for (i = 0; i < newSize; ++i) {
cout << countDown.at(i) << " ";
}
cout << "Go!" << endl;

return 0;
}
 
Technology news on Phys.org
  • #2
EvanET said:
Resize vector countDown to have newSize elements. Populate the vector with integers {newSize, newSize - 1, ..., 1}. Ex: If newSize = 3, then countDown = {3, 2, 1}, and the sample program outputs:

3 2 1 Go!

**I want to do this WITHOUT push_back**
#include <iostream>
#include <vector>
using namespace std;

int main() {
vector<int> countDown(0);
int newSize = 0;
int i = 0;

newSize = 3;

//enter code here

for (i = 0; i < newSize; ++i) {
cout << countDown.at(i) << " ";
}
cout << "Go!" << endl;

return 0;
}

Hi EvanET! Welcome to MHB! ;)

To pre-allocate the required memory, we can use 2 methods:
1. Call [M]countDown.resize(newSize)[/M].
2. Re-create the vector by executing [M]countDown = vector<int>(newSize)[/M].

After that we still have to fill in the newly available memory locations.
 

FAQ: How to Properly Resize and Populate a Vector in C++ Without Using push_back?

What is vector resize in C++?

Vector resize in C++ is a function that allows you to change the size of a vector, which is a dynamic array that can store multiple elements of the same data type. Resizing a vector can be useful when you want to add or remove elements from the vector.

How do I resize a vector in C++?

To resize a vector in C++, you can use the resize() function, which takes in an integer parameter representing the new size of the vector. For example, if you have a vector named myVector and you want to resize it to have 10 elements, you can use the following code: myVector.resize(10);

Can I resize a vector to be smaller than its current size?

Yes, you can resize a vector to be smaller than its current size. This will remove the extra elements from the vector, starting from the end of the vector. For example, if you have a vector with 10 elements and you resize it to have 5 elements, the last 5 elements will be removed from the vector.

What happens to the elements of a vector when it is resized?

When a vector is resized, its elements may be moved to a different memory location. This can result in the elements being copied or moved to a new location in the vector. Additionally, if the new size is larger than the current size, the new elements will be initialized with their default values.

Can I use the resize() function to change the size of a vector multiple times?

Yes, you can use the resize() function to change the size of a vector multiple times. However, keep in mind that each time you resize the vector, its elements may be moved to a different location in memory. This can potentially impact the performance of your program, so it is recommended to only resize a vector when necessary.

Similar threads

Replies
1
Views
4K
Replies
22
Views
3K
Replies
5
Views
2K
Replies
1
Views
1K
Replies
10
Views
2K
Replies
23
Views
2K
Back
Top