vectors size in STL

Hi, i have just started working on vectors in STL, i tried to compile a simple piece of a code but its not working the way i wanted..here is the code

#include<iostream>
#include<conio.h>
#include<vector>

using namespace std;

int main()
{

int a;
vector<vector<int> > vec(3);//this works fine if user enters value less than 3
//vector<vector<int> > vec;

cout<<"enter row number:";
cin>>a;
for (int i=0;i<5;i++)
{
vec[a].push_back(i);
}

vector<int> ::iterator iter;
for(iter=vec[a].begin();iter!=vec[a].end();iter++)
{
cout<<*iter<<endl;
}
getch();
return 0;
}

the issue is i dont want to initialize a vector with some empty vectors(like 3 in my example).i want to use the initialization which is commented in the code because who knows which number is user going to enter, but in that case compiler shows an error at runtime that vector subscript out of range. maybe a stupid question but help me guys.....thanks
You can set the size of the vector after it has been creating by calling resize.
vec.resize(a);

Or you can define vec after you have given a a value and pass a as argument to the constructor.
vector<vector<int> > vec(a);
You can use the following approach

1
2
3
4
5
6
7
int a;
vector<vector<int> > vec;
 
cout<<"enter row number:";
cin>>a;

vec = std::vector<std:;vector<int>( a );


Or

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int a;
vector<vector<int> > vec;
 
cout<<"enter row number:";
cin>>a;

vec.reserve( a );

for ( int i = 0; i < a; i++ )
{
   vec.push_back( std::vector<int>() );
   vec.back().reserve( 5 );
   for (int i=0;i<5;i++)
   {
       vec.back().push_back(i);
   }
 }
@Peter:
thanks for the reply...your first solution of resizing the vector is not working, still the same error persists and i dnt want to use the second solution because what if i take the value from user for more than 1 time , the vector will lose the data stored earlier becoz it will get initialized again..so any other solution??

@vlad :

your solutions are also not working altough there were some syntax errors in first solution but even after removing them ,its not working..:(..any other solution..thanks
Last edited on
Why are not they working? Show the code you are running.

EDIT: The problem is that the bfollowing your code

1
2
3
4
for (int i=0;i<5;i++)
 {
 vec[a].push_back(i);
 }



is invalid and has no sense. There is no such element of vec with index a. Acceptable indexes are [0, a) that is a is nit contained in the range,

So it is your code that does not work, not main.:)
Last edited on
#include<iostream>
#include<conio.h>
#include<vector>

using namespace std;

int main()
{

int a;
vector<vector<int> > vec;
cout<<"enter row number:";
cin>>a;
vec = std::vector<std::vector<int> >( a );
for (int i=0;i<5;i++)
{
vec[a].push_back(i);
}
vector<int> ::iterator iter;
for(iter=vec[a].begin();iter!=vec[a].end();iter++)
{
cout<<*iter<<endl;
}
getch();
return 0;
}
#include<iostream>
#include<conio.h>
#include<vector>

using namespace std;

int main()
{

int a;
vector<vector<int> > vec;
cout<<"enter row number:";
cin>>a;
vec.reserve( a );

for ( int i = 0; i < a; i++ )
{
vec.push_back( std::vector<int>() );
vec.back().reserve( 5 );
for (int i=0;i<5;i++)
{
vec.back().push_back(i);
}
}

vector<int> ::iterator iter;
for(iter=vec[a].begin();iter!=vec[a].end();iter++)
{
cout<<*iter<<endl;
}
getch();
return 0;
}
As I already pointed there is no such element as

vec[a].push_back(i);

in the first example. The acceptable indexes for the vector are 0 : a - 1

The same is valid and for your second program

for(iter=vec[a].begin();iter!=vec[a].end();iter++)


There is no such element in the vector as vec[a].

Consider the correct code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <vector>
 
int main()
{
    {
        std::vector<std::vector<int>> vec;
        
        int a;
        
        std::cout << "Enter rows number: ";
        std::cin >> a;
        
        vec = std::vector<std::vector<int>>( a );
        
        for ( int i = 0; i < a; i++ )
        {
            vec.reserve( 5 );
            for ( int j = 0; j < 5; j++ ) vec[i].push_back( j );
        }
        
        for ( std::vector<int> &v : vec )
        {
            for ( int x : v ) std::cout << x << ' ';
            std::cout << std::endl;
        }
        std::cout << std::endl;
    }
    
    {
        int a;
        std::vector<std::vector<int> > vec;
 
        std::cout << "enter rows number: ";
        std::cin >> a;
 
        vec.reserve( a );
 
        for ( int i = 0; i < a; i++ )
        {
            vec.push_back( std::vector<int>() );
            vec.back().reserve( 5 );
            for (int i=0;i<5;i++)
            {
                vec.back().push_back(i);
            }
        }
        
        for ( std::vector<int> v :vec )
        {
            for ( int x : v ) std::cout << x << ' ';
            std::cout << std::endl; 
        }
    }
}
Last edited on
so lets make it as

vec[a-1].push_back(i);

but this is also not working altough a-1 is in the range..:(

EDIT:
you were right bro..its working now ..thanks alot for the help
Last edited on
By the way if you want to intialize all elements of the original vector with a vector that contains numbers 0, 1, 2, 3, 4 you can do this with only one line of code

vec.resize( a, std::vector<int>( { 0, 1, 2, 3, 4 } ) );
@Khurmi878
so lets make it as

vec[a-1].push_back(i);

but this is also not working altough a-1 is in the range..:(

Please try at least copy and paste the code I showed. It is your errors not main. You are even unable carefully to copy the correct code.
Last edited on
No I dnt actually want to do that, i was doing it just for example...its working now ,thanks alot again for your time..:)
Topic archived. No new replies allowed.