iterator for 2d vector
Although your question is not very clear, I'm going to assume you mean a 2D vector to mean a vector of vectors:
vector< vector<int> > vvi;
Then you need to use two iterators to traverse it, the first the iterator of the "rows", the second the iterators of the "columns" in that "row":
//assuming you have a "2D" vector vvi (vector of vector of int's)
vector< vector<int> >::iterator row;
vector<int>::iterator col;
for (row = vvi.begin(); row != vvi.end(); row++) {
for (col = row->begin(); col != row->end(); col++) {
// do stuff ...
}
}
Iteration in 2D Vector in C++
You can iterate like this,
int main()
{
vector< vector<int>> vec;
for(int i=0;i<vec.size();i++)
{
for(int j=0;j<vec[i].size();j++)
cout<<vec[i][j]<<" ";
cout<<endl;
}
}
C++: vectorvectorint iterator?
I am wondering if i is an iterator of an entire 1D array?
Well, vec2d
is a vector of vector<int>
and i
is vec2d
's iterator. You can consider vec2d
a 1d vector of 1d vector, and if so, i
is an iterator for the entire vec2d
(which is, as mentioned, a 1-d vector), or you can look at it as a 2d vector of ints
(which I see as less trivial option).
Notice that not all 1d vectors are the same. Even if you consider vec2d
as a 1d vector, its a vector OF vector<int>
, hence something like this little shenanigan i = tmp2.begin();
(from my example below) will not compile.
Basically an iterator can iterate on a specific container type, be it a vector of ints, or vector of vectors of whatever. The distinction between 1d and 2d vectors isn't the issue, as I see it.
what is that (*i) [1] mean?
Consider the following:
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector< vector<int> > vec2d;
vector<int> tmp1;
tmp1.push_back(1);
tmp1.push_back(2);
vector<int> tmp2;
tmp2.push_back(3);
tmp2.push_back(4);
vec2d.push_back(tmp1);
vec2d.push_back(tmp2);
vector< vector<int> >::iterator i, iEnd;
i = vec2d.begin();
iEnd = vec2d.end();
cout << (*i)[1] << endl; // outputs 2 (same as vec2d[0][1])
cout << vec2d[0][1] << endl; // outputs 2
cout << vec2d[1][0] << endl; // outputs 3
cout << vec2d[1][1] << endl; // outputs 4
return 0;
}
As you can see, *i
takes you "inside" the container you iterate over (in your case, vec2d
) and the [1]
gives you the 2nd element of that inner-container.
Notice that *i[0] != (*i)[0]
because of operator-precedence (in my example case it doesn't even compile).
How do I iterate through a two dimensional vector
Or since we're using c++11...
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<vector<int> > v = {{1,2}, {3,4}};
for (const auto& inner : v) {
for (const auto& item : inner) {
cout << item << " ";
}
}
cout << endl;
return 0;
}
How can I iterate over a 2D vector in functional style?
Use flatten
:
fn main() {
let v = vec![vec![1, 2], vec![3, 4], vec![5, 6]];
for i in v.iter().flatten() {
println!("{}", i); // 1 2 3 4 5 6
}
}
flatten
transforms nested iterators into one iterator.
Related Topics
Can a Declaration Affect the Std Namespace
Default Initialization of Pod Types in C++
Visual Studio: Link:Fatal Error Lnk1181: Cannot Open Input File
What's the C++ Suffix for Long Double Literals
Why Can't I Inherit from Int in C++
Difference Between String.H and Cstring
How to Initialize and Print a Std::Wstring
Why Isn't 'Std::Initializer_List' Defined as a Literal Type
Access Violation on Static Initialization
Is It Safe to Use the "This" Pointer in an Initialization List
Signal Handler Function in Multithreaded Environment
Undefined Symbols for Architecture X86_64: Compiling Problems
C++ Most Efficient Way to Convert String to Int (Faster Than Atoi)