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

  • Thread starter Thread starter EvanET
  • Start date Start date
  • Tags Tags
    C++ Vector
Click For Summary
To resize the vector `countDown` to contain `newSize` elements and populate it with integers from `newSize` down to 1 without using `push_back`, the discussion highlights two methods for pre-allocating memory. The first method involves using `countDown.resize(newSize)`, which adjusts the size of the vector to `newSize`. The second method is to re-create the vector with `countDown = vector<int>(newSize)`. After resizing, the vector needs to be filled with the appropriate integers. The sample program is designed to output the countdown sequence followed by "Go!" when executed correctly.
EvanET
Messages
10
Reaction score
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
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 66 ·
3
Replies
66
Views
5K
  • · Replies 23 ·
Replies
23
Views
3K