'Getting an error while using begin and end iterators
vector <vector<int> > v8;
int N;
cin >> N;
for (int i = 0; i < N; i++)
{
int n;
cin >> n;
vector <int> temp;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
temp.push_back(x);
}
v8.push_back(temp);
}
vector <int> ::iterator it;
for (it = v8.begin(); it < v8.end(); it++)
{
cout << (*it) << " ";
}
I'm getting this error:
no operator "=" matches these operandsC/C++(349)
intro.cpp(161, 13): operand types are: __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int>>> = __gnu_cxx::__normal_iterator<std::vector<int, std::allocator<int>> *, std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>>>
How should I solve this??
Solution 1:[1]
To iterate through vectors in v8, enumerating their elements, use range-based for loops.
for (auto& v: v8) {
for (auto& it: v) {
std::cout << it << ' ';
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Oasin |
