Reputation: 43
set <vector<int> > myset;
vector<int> v1= { 34,634,758,46,64 };
vector<int >v2= {325, 7457, 586, 865};
myset.insert(v1);
myset.insert(v2);
set<vector<int> > ::iterator it;
it = myset.begin();
for (; it != myset.end(); it++)
{
vector<int> ::const_iterator temp = (*it);
}
I know that (it) is pointing to a vector so if i write vector ::const_iterator temp=(*it) , temp should point to first element of the vector? why is this not happening ? what does a iterator actually point to in a container ? how will i traverse the container in such a situation ?
Upvotes: 0
Views: 83
Reputation: 596287
I know that (it) is pointing to a vector
No. It refers to an element of the set
, not to the content (a vector
) held within that element.
so if i write
vector ::const_iterator temp=(*it)
,temp
should point to first element of the vector?
No. When you dereference it
, you are accessing the vector
itself that is held within the set
element. You are not accessing any of the vector
's elements. For that, you need to use the vector
's own methods, eg:
set< vector<int> > myset;
...
set< vector<int> >::iterator it;
for(it = myset.begin(); it != myset.end(); ++it)
{
vector<int>::const_iterator temp = (*it).begin(); // or: it->begin()
/* which is the same as doing this:
vector<int> &v = *it;
vector<int>::const_iterator temp = v.begin();
*/
}
why is this not happening ?
Because there is no implicit conversion from a vector&
to a vector::const_iterator
.
what does a
iterator
actually point to in a container ?
That is implementation-defined. But in general, an iterator is a conceptual index/pointer to an element of the container. You have to dereference the iterator to access the content of that element.
how will i traverse the container in such a situation ?
Upvotes: 1