问一下这段C++代码为什么不合法, find算法返回的是什么类型

C++语言 码拜 7年前 (2017-04-22) 1037次浏览
	int i = 0;
	vector<int> number;
	for (i = 0;i < 10;i++) {
		number.push_back(i + 1);
	}
	int value = 5;
	auto result = find(number.cbegin(), number.cend(), value);
	cout << "result = " << result;	/*报错, 没有与这些操作数匹配的"<<"运算符...*/
解决方案

6

https://msdn.microsoft.com/en-us/library/h64454kx.aspx
Return Value
An input iterator addressing the first occurrence of the specified value in the range being searched. If no element is found with an equivalent value, returns last.
所以返回的是一个迭代器,如需cout,前置‘*’进行解引用

7

是vector<int>::iterator这种类型
打印有问题
if (result == number.end())
{
cout << “No find” << endl;
}
else
{
cout << “Yes find result= ” << *result << endl;
}

14

返回的是一个迭代器
Return value
Iterator to the first element satisfying the condition or last if no such element is found.
http://en.cppreference.com/w/cpp/algorithm/find
cout r的 << 没有重载对这种类型的输出

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明问一下这段C++代码为什么不合法, find算法返回的是什么类型
喜欢 (0)
[1034331897@qq.com]
分享 (0)