关于用find_if实现查找的有关问题
关于用find_if实现查找的问题使用标准库函数对象来查找指定长度的单词。vectorstring text// …// input
关于用find_if实现查找的问题
使用标准库函数对象来查找指定长度的单词。
vector<string> text;
// … // input words into text
string::size_type len;
cin >> len;
string *w;
for ( vector<string>::iterator it = text.begin(); it != text.end(); ++it )
{
w = find_if ( it, text.end(), bind2nd( equal_to<string::size_type>(), len) );
}
cout << “ The words whose length is “<< len << ” was found.,It is:”
<< *w <<endl;
这里find_if为什么要放在循环里?直接find_if ( text.begin(), text.end(), bind2nd( equal_to<string::size_type>(), len) )不行吗?
[解决办法]Return value:
An iterator to the first element in the range for which pred does not return false.
If pred is false for all elements, the function returns last.
本来是找到该集合的第一个元素的,但是放在for里面就说明是查找最后一个!