Not sure why this Vector find will not work

Feb 3, 2016 at 5:20pm
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'

}
Feb 3, 2016 at 5:27pm
std::find returns an iterator, not an integer.

http://www.cplusplus.com/reference/algorithm/find/
Last edited on Feb 3, 2016 at 5:28pm
Feb 3, 2016 at 5:32pm
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
Feb 3, 2016 at 5:44pm
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 Feb 3, 2016 at 5:53pm
Feb 3, 2016 at 6:03pm
thanks it works now
Topic archived. No new replies allowed.