0
votes

I have an array of vectors but for some reason i can't iterate through the arrays of vectors. It rather gave me such long error message.

int num = 6;
vector<string> vArray[num];

for (int i = 0; i < 50; i++)
{
 vArray[i % num].insert(vArray[i].begin(), data);
} //runs fine

for (int i = 0; i < num; i++)
{
 for(std::vector<int>::iterator it = vArray[i].begin(); it != vArray[i].end(); it++)
  {
    cout << *it << endl;
  }
}//problem..

and it just gives me this super long error message

p2.cpp:94: error: conversion from ‘__gnu_cxx::__normal_iterator, std::allocator >*, std::vector, std::allocator >, std::allocator, std::allocator > > > >’ to non-scalar type ‘__gnu_cxx::__normal_iterator > >’ requested

p2.cpp:94: error: no match for ‘operator!=’ in ‘it != vArray[i].std::vector<_Tp, _Alloc>::end with _Tp = std::basic_string, std::allocator >, _Alloc = std::allocator, std::allocator > >’

1
why do you try to use std::vector<int>::iterator for std::vector<string> ? - user3159253
^That is what I was thinking. Also, what is data? - Dean Knight
lol thanks a lot that helpssss - Jack Smother
I got it worked out. the data is just simple string. I feel so dumb. lol - Jack Smother

1 Answers

2
votes

Look at the line:

vArray[i % num].insert(vArray[i].begin(), data);

Now when i=6 this says:

vArray[0].insert(vArray[6].begin(), data);

and vArray[6] is not defined

The second problem is that you use a std::vector<int>::iterator for an std::vector<string> you could really use the keyword auto to avoid these mistakes

Try the following:

for(auto it = vArray[i].begin(); it != vArray[i].end(); it++)

it means that even if you change the type in vArray the for loop will work assuming c++11