类型匹配问题?
我准备在vector <string> text 中查找所需要的字符串并保存.
string *max=max_element(text.begin(),text.end(),length_less);
提示错误,意思是说类型不匹配。max_element返回的是iterator,不能转换成string *.
请问大家有什么办法?
我又声明了一个vector <string> ::iterator iter来保存这个iterator,然后打算将*iter 赋值给一个string对象。可是还是出现类型不匹配的错误。
请大家指教!
[解决办法]
没有问题啊:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector <string> text;
for (int i=0;i <10;i++)
{
text.push_back(string(1, 'a '+i));
}
vector <string> ::iterator it=max_element(text.begin(),text.end());
cout < <string(*it) < <endl;//用*it构造一个string对象
return 0;
}
不会是你的length_less有问题吧