is there any vay to access iterator's index value without using another variable in the for loop?

is there any vay to access iterator's index value without using another variable in the for loop?

#include <iostream>
#include <vector>
using namespace std;

int main()
{
	vector <int> vec;
	vector<int>::iterator iter;

	for(int i = 0; i < 5; ++i)
		vec.push_back(i);

	for(iter = vec.begin(); iter < vec.end(); ++iter)
		cout << iter - vec.begin() <<  " ";

	cin.get();
}

The results: 0 1 2 3 4 :)

That's just the point: a STL iterator is not an index value. It looks like an index value (via incr/decr overloaded operators) but it's not a number. The std::vector container (or std::valarray or std::string) has integer index values for its elements via overloaded subscript operator but std::map (or std::set) container has not any indicies at all.

In actual fact an index is a simplest iterator too but not vice versa.

Hi,

I am trying to see to which key the iterator is pointing in a map. is there a way to do so.
Also, would it be the fastest way to store unique keys in a map.

mapType::iterator it = cube.begin();
	mapType::iterator its = it;
	std::advance(its,1);
	for(; it!= cube.end(); ++it)
	{
		cout << "Inside it = " << (it) - cube.begin() << endl;
		cout << "Inside its = " << (its) - cube.begin() << endl;
		for(; its!= cube.end(); ++its)
		{
			cout << "its = " << (its) - cube.begin() << endl;
			vector<int>::const_iterator it1;
			vector<int>::const_iterator it3;
			for( it1 = it->first.begin(), it3 = its->first.begin(); it1!= it->first.end(), it3!= its->first.end(); ++it1,++it3)
			{
				//cout << *it1 << " " << *it3 << " ";
				if((*it1 == 0 && *it3 == 1) || (*it1 == 1 && *it3 == 0))
				break;
				else if(*it1 == *it3)
				tempo.push_back(*it1);
				else if((*it1 == 0 && *it3 == 2) || (*it1 == 2 && *it3 == 0))
				tempo.push_back(0);
				else 
				tempo.push_back(1);
			}
			if(cube.count(tempo) <= 0)
			cube[tempo].push_back(10);
			cout << endl;
		}
	}

The code marked in green does not work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.