我在使用find_if函数时遇到错误.它说没有匹配的功能.我确实发现其他人已经遇到了这个错误,但我不太明白这些回复.有人可以纠正这个并解释错误是什么吗?任何帮助将不胜感激.提前致谢.
//Another way to split strings
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using std::endl;
using std::cout;
using std::cin;
using std::string;
using std::vector;
using std::istream;
istream& getWords(istream&, vector<string>&);
string& removeDelimeters(string&);
bool space(char);
bool not_space(char);
void display(const vector<string>&);
int main()
{
vector<string> words;
getWords(cin,words);
display(words);
return 0;
}
void display(const vector<string>& vec)
{
cout<<endl;
for(vector<string>::const_iterator iter = vec.begin();iter != vec.end();iter++)
{
cout<<*iter<<endl;
}
}
bool space(char c)
{
return isspace(c);
}
bool not_space(char c)
{
return !isspace(c);
}
string& removeDelimeters(string& word)
{
string delim = ",.`~!@#$%^&*()_+=-{}][:';?><|";
for(unsigned int i = 0;i<word.size();i++)
{
for(unsigned int j = 0;j<delim.size();j++)
{
if(word[i] == delim[j])
{
word.erase(word.begin()+i); //removes the value at the given index
break;
}
}
}
return word;
}
istream& getWords(istream& in, vector<string>& vec)
{
typedef string::const_iterator iter;
string initial;
cout<<"Enter your initial sentance : ";
getline(cin,initial);
initial = removeDelimeters(initial);
iter i = initial.begin();
while(i != initial.end())
{
//ignore leading blanks
i = find_if(i,initial.end(),not_space);
//find the end of the word
iter j = find_if(j,initial.end(),space);
//copy the characters in [i,j)
if(i != initial.end())
{
vec.push_back(string(i,j));
}
i = j;
}
}
Run Code Online (Sandbox Code Playgroud)
您需要指定std命名空间:
iter j = std::find_if(j,initial.end(),space);
Run Code Online (Sandbox Code Playgroud)
或者做你上面做的,并添加一个using声明:
using std::find_if;
除了using std::find_ifJohn Dibling所提到的以及其他一系列问题(查看getWords方法以及它在传入流中的作用以及返回类型等等)
你的主要问题是,你是通过两种不同的迭代器类型来find_if,第一个迭代器是一个const_iterator-因为你分配到一个const_iterator,但第二个迭代器是非常量,即initial.begin()-因为initial不是常量-和常量/非const的迭代器不同的类型,这就是为什么它不会找到find_if匹配...
| 归档时间: |
|
| 查看次数: |
1557 次 |
| 最近记录: |