【发布时间】:2017-05-24 00:18:05
【问题描述】:
对于学校作业,我需要通过使用从矢量中删除紫色,这就是我想出的:
bool IsEqual(string s, string s2)
{
if (s == s2)
{
return true;
}
}
int main() {
vector<string> coulours2 = { "red", "green", "blue", "orange", "purple", "orange", "black", "green" };
vector<string>::iterator newEnd;
newEnd = remove_if(coulours2.begin(), coulours2.end(), bind2nd(IsEqual, "purple"));
colours2.erase(newEnd);
cin.get();
return 0;
}
但是我得到了很多错误,我认为我使用 bind2nd 错误。应该如何正确使用?
【问题讨论】:
-
有什么错误?你的minimal reproducible example 在哪里?还有please don't do
cin.get()like that! -
你使用
remove_if+erase错误。 -
具体来说
erase有多个版本。您使用的是在迭代器中擦除单个元素的那个,而不是擦除范围的那个。 -
抱歉标题我更正了。我正在尝试从 colours2 中删除“紫色”。我怎么用错了 Jesprer Juhl?
-
IsEqual会像return s == s2;一样简单。另外,为什么不只是std::equal?
标签: c++ string functional-programming