This simple example fills a vector with numbers 0-9. Then I use find() to find where the 7 is. Then I saw online you can get the index by simply subtracting V.begin() from the iterator that find() returns. Has - been overloaded for iterators to do some magic and return the offset? Or are iterators simply these offsets?

unsigned int num = 10;
	vector<unsigned int> V(num);
	for(unsigned int i = 0; i < num; i++)
	{
		V[i] = i;
	}

	vector<unsigned int>::iterator it = find(V.begin(), V.end(), 7);
	if(it == V.end())
	{
		cout << "Could not find 7 in the vector"  << endl;
	}
	else
	{
		cout  << "Have found 7 in the vector"  << endl;

////////// THE LINE IN QUESTION //////////////
		cout << "The number 7 is located at index " << it - V.begin() << endl;
	}

Thanks,
Dave

I don't know why you ask this question: if you are calling std::find to get an iterator and after, find the position of the iterator on the vector, this is not a good approach. First, you are searching on all the vector for 7 with find, and after, if I understand, you are relying on memory addresses of iterators to do a substraction and get the index? If the memory for the iterators is allocated at a totally bizarre place, the substraction will not work.

Yeah, there is still the operator-() that the iterator may have, but I think that it is not an elegant programming style.

The best thing is to write a loop and check on each index. It is much safer, if you really want the index.

Don't forget that the common methods on the std::vector class takes iterators, so you don't even need the index.

Using an iterator or the index is your choice.

Iterators are not numbers. Iterators are abstract pointers (remember pointer arithmetics in C)!

So when would you use find() then? The only thing I would know how to do with the result of find is get the value of the thing you found by dereferencing the iterator, but you already know that value because it is what you were searching for! Is the reason to use find() only to see IF something exists, not WHERE it is in the vector?

So when would you use find() then? The only thing I would know how to do with the result of find is get the value of the thing you found by dereferencing the iterator, but you already know that value because it is what you were searching for! Is the reason to use find() only to see IF something exists, not WHERE it is in the vector?

A standard iterator class should have a special iterator value returned by container.end() member function. It plays the same role as a null pointer:

iter = object.find(value);
if (iter == object.end())  { // not found

It's impossible to dereference such iterator value, it refers to nowhere.

That's why no needs in a special exists member function: the find can answer yes or no. Of course, the find member function implements the fastest possible method to search (or address) values in the container.

It looks like a simple and clear solution.

However, you can use the std::distance function to determine the
distance between two sequentual iterators.
e.g.

std::vector<int> A;
std::list<int> B;
// ... populate A,B:
std::vector<int>::iterator AIter=find(A.begin(),A.end(),7);
std::list<int>::iterator BIter=find(B.begin(),B.end(),7);

std::cout<<"Distance Vector == "<<distance(A.begin(),AIter)<<std::endl;
std::cout<<"Distance List== "<<distance(B.begin(),BIter)<<std::endl;

// THIS DOES NOT WORK::
// std::cout<<BIter-B.begin() <<std::endl;

The list iterator is not random access so you can use +/- between iterators. But distance is ok.

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.