vector iterator error on code

On this code:
1
2
3
4
5
6
7
8
9
10
if (std::find(vec.begin(), vec.end(), 3) != vec.end())
{
	auto pos = std::find(vec.begin(), vec.end(), 3);
	std::cout << "\nThe value 3 is at position " << pos << " in the original vector\n";
}
if (std::find(vec2.begin(), vec2.end(), 3) != vec2.end())
{
	auto pos = std::find(vec2.begin(), vec2.end(), 3);
	std::cout << "\nThe value 3 is at position " << pos << " in the copy vector\n";
}


I have the error message (on the lines where I'm trying to print pos):
error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>' (or there is no acceptable conversion)


Anyone have any idea how to fix this? Thanks in advance.
cout can only output basic types or types which overload the >> operator.

pos is of type: std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>> which is not a basic type nor does it overload the << operator.

Last edited on
To get the position from an iterator use distance(...):

http://www.cplusplus.com/reference/iterator/distance/?kw=distance

std::cout << "\nThe value 3 is at position " << std::distance(vec.begin(), pos) << " in the original vector\n";
@Coder777: Yeah, thanks. That did it.

How about I just overload the operator<< for that iterator, though?
Topic archived. No new replies allowed.