Not sure why this Vector find will not work

Can i get some Help with this..Not sure I have tried different data types still not working

1
2
3
4
5
6
7
int index;
//char test;   did not work
//string result;  did not work
for(int i=0;i<word1.size();i++){
index = find(letters.begin(), letters.end(), word1[i]);//'=': cannot convert from 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<char>>>' to 'int'

}
std::find returns an iterator, not an integer.

http://www.cplusplus.com/reference/algorithm/find/
Last edited on
but isn't an iterator an integer?

I want to get the position of the character in the vector so i can use it for further use
No, an iterator is more similar to a pointer.

You can calculate the index by subtracting the begin() iterator. If nothing was found the end iterator is returned so don't forget to check that before doing anything with the iterator.

1
2
3
4
5
6
7
8
9
std::vector<char>::iterator it = find(letters.begin(), letters.end(), word1[i]);
if (it != letters.end())
{
	index = it - letters.begin();
}
else
{
	// The letter was not found!
}
Last edited on
thanks it works now
Topic archived. No new replies allowed.