C++ Vector Resize: Counting Down to the Test

  • C/C++
  • Thread starter ineedhelpnow
  • Start date
  • Tags
    C++ Vector
In summary, the conversation is about resizing a vector and populating it with integers in descending order to study for a test. The summary also includes an example program and possible solution for resizing the vector.
  • #1
ineedhelpnow
651
0
going through problems to study for a test

Resize vector countDown to have newSize elements. Populate the vector with integers newSize down to 1. Ex: If newSize = 3, then countDown = {3, 2, 1}, and the sample program outputs:

3 2 1 Go!
Sample program:

Code:
#include <iostream>
#include <vector>
using namespace std;

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

   newSize = 3;
   <STUDENT CODE>

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

   return 0;
}

i tried countDown.resize(newSize); but it didnt work.
 
Technology news on Phys.org
  • #2
ineedhelpnow said:
i tried countDown.resize(newSize); but it didnt work.

Hi! ;)

That should work to resize the vector.
But I think you still have to populate the vector in order to get 3, 2, 1. (Thinking)
 

FAQ: C++ Vector Resize: Counting Down to the Test

What is the purpose of resizing a vector in C++?

The purpose of resizing a vector in C++ is to change the size of the vector, either increasing or decreasing it. This allows for more efficient memory management and can also help improve the performance of the program.

How do you resize a vector in C++?

To resize a vector in C++, you can use the resize() function and specify the desired size as the argument. Alternatively, you can use the push_back() or pop_back() functions to add or remove elements from the vector, which will automatically resize the vector.

Can you resize a vector to a specific size?

Yes, you can specify the desired size when using the resize() function to resize a vector in C++. However, if the new size is smaller than the current size, some elements will be removed from the vector.

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

When a vector is resized, the elements are either added or removed to match the new size. If the new size is larger than the current size, the vector will be extended and new elements will be added. If the new size is smaller, some elements will be removed from the end of the vector.

Can you resize a vector to a size larger than its current capacity?

Yes, you can resize a vector to a size larger than its current capacity. This will extend the vector's capacity and add new elements to the vector. However, this can potentially lead to memory management issues and it is recommended to use the reserve() function to increase the capacity of a vector beforehand.

Similar threads

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