hytriutucx
hytriutucx

Reputation: 1634

using a nested vector in C++

I am trying to implement a vector<int> within a vector<Type> in C++. However whenever I run the following code, I get an error reading

std::vector<std::vector<int> >::const_iterator’ has no member named ‘begin’
 std::vector<std::vector<int> >::const_iterator’ has no member named ‘end’

Here is the code:

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

typedef vector<int> vector1D ;
typedef vector<vector1D > vector2D ;

void showarr(const vector2D& v)
{
    for (vector<vector1D >::const_iterator it1 = v.begin(); it1 != v.end(); ++it1) {
        for(vector<int>::const_iterator it2 = *it1.begin(); it2 != *it1.end(); ++it2) {
            cout<<*it2<<endl;
        }
    }
}
int main(int argc, char *argv[])
{
    int rownum;
    cin>>rownum;
    vector2D a;
    for ( int i = 0 ; i < rownum ; i++) {
        a.push_back(vector1D(rownum,0));
    }
    showarr(a);
    return 0;
}   

Any type of help is appreciated.

Upvotes: 1

Views: 2629

Answers (3)

uyetch
uyetch

Reputation: 2190

The problem is in the line containing *itr.begin(). Change it to itr->begin(). This way, you won't get any errors.

Upvotes: 1

Mark Ransom
Mark Ransom

Reputation: 308216

It's being parsed as *(it1.begin()), not (*it1).begin(). Change it to it1->begin().

Upvotes: 8

Moe
Moe

Reputation: 29743

Try changing:

*it1.begin()

to

it1->begin()

Upvotes: 9

Related Questions