5

Please refer to the code and comments below:

vector<int> v1(10);
cin>>v1[0]; // allowed
cin>>v1[1]; // allowed

// now I want v1 to hold 20 elements so the following is possible:

cin>>v1[15]>>v[19]; // how to resize the v1 so index 10 to 19 is available.
Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176

4 Answers4

8

You simply need to resize the vector before adding the new values:

v1.resize(20);
Aaron
  • 9,123
  • 5
  • 40
  • 38
  • after resize, I can take input using cin without requiring push_back? – Shamim Hafiz - MSFT May 07 '11 at 17:37
  • @Gunner: `vector v1(10);` is effectively the same as `vector v1; v1.resize(20);`. That said, what do you have against `push_back` (or, what do you have against a `reserve` followed by `push_back`?) – James McNellis May 07 '11 at 17:45
  • @James: Nothing against those, but the situation is such, I would like to take input directly into the vector instead of a temporary variable. At the end, both will be same, but was wondering if there was an alternative. – Shamim Hafiz - MSFT May 08 '11 at 06:04
  • 2
    @Gunner: yes - after resize you can access v1[0] through v1[19] without a push_back. – Aaron May 08 '11 at 17:11
4

You could use resize like this:

v1.resize(20);
Bart
  • 19,692
  • 7
  • 68
  • 77
3

If you want to read as many values from cin as are available, you can use an istream_iterator iterator range and pass that to the vector range-constructor, like this:

#include <iterator> // for istream_iterator
#include <vector>
#include <iostream> // for cin

// ...

std::vector<int> v1( (std::istream_iterator<int>( std::cin )), // extra ()
                     std::istream_iterator<int>() );

(the extra parentheses are required to prevent "C++ most vexing parse"). Cf. also Constructing a vector with istream_iterators.

Community
  • 1
  • 1
Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
  • Though this isn't exactly what I want, but its an useful feature on its on accord. Under many circumstances, the first thing we would be doing is getting input into a vector after declaring it. – Shamim Hafiz - MSFT May 08 '11 at 06:03
2

vector::resize() will resize it and fill it with default constructed objects (int, in this case, so it doesn't matter).

vector::reserve() will allocate space, without filling it.

You can add additional items using, for example, push_back(), until it has however many items you want - it resizes itself as needed.

jwismar
  • 12,164
  • 3
  • 32
  • 44
  • It does matter. Because it uses the `x[] = T()` (before people complain (conceptually, exact details slightly more complex)) method of initializing the new members and INT is a POD. POD values are value initialized and thus the members are set to 0. – Martin York May 07 '11 at 17:38
  • Note that `reserve` will not increase the size, only the capacity. Thus in the OP's case, it would not be possible to access elements 15 and 19 after a `reserve(20)`. You need `resize` for that. – Steve Fallows May 07 '11 at 17:39
  • @Martin: granted - but what I meant is that it's not an involved, time consuming constructor - or a constructor that you have control over. – jwismar May 07 '11 at 17:44
  • @Steve - right - but I added that because the underlying question probably has to do with capacity, and it's almost never useful to have a vector filled with default constructed objects. I find that reserve() followed by some type of insertion is almost always what I want to do. – jwismar May 07 '11 at 17:46