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
AI Thread 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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top