C++ 擦除&x2013;删除惯用语c++;(无好友功能)
我有两门课:C++ 擦除&x2013;删除惯用语c++;(无好友功能),c++,C++,我有两门课: #include <iostream> #include <vector> #include <string> #include <stdexcept> #include <algorithm> #include <cmath> #include <list> using namespace std; enum class Programm{Koch, Normal, Bunt, Fein};
#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
#include <algorithm>
#include <cmath>
#include <list>
using namespace std;
enum class Programm{Koch, Normal, Bunt, Fein};
class KleidSt {
private:
string bezeichnung;
int gewicht;
Programm Pflegehinweis;
public:
KleidSt(string bezeichnung, int gewicht);
KleidSt(string bezeichnung, int gewicht, Programm Pflegehinweis);
int get_gewicht() const;
bool vertraeglich(Programm)const;
int get_Pflegehinweis() const;
friend ostream& operator<<(ostream& os, const KleidSt& kleid);
};
class WaschM{
private:
int ladungsgewicht;
vector<KleidSt> wasch;
public:
WaschM(int ladungsgewicht);
void zuladen(const vector<KleidSt>& z);
void waschen(Programm);
friend ostream& operator<<(ostream& os, const WaschM& kleid);
int programme() const;
vector<KleidSt> aussortieren(Programm pr);
};
在这里,vertraeglich(pr)
完成了我上面描述的工作。
但它显然返回了错误,因为
vertraeglich
函数的定义超出了类WaschM
的范围。问题是:如何使用Erase–remove惯用法(或者其他一些变体),这样代码才能正常工作?看起来像是lambda函数的作业
vector<KleidSt> WaschM::aussortieren(Programm pr){
wasch.erase(
remove_if(
begin(wasch),
end(wasch),
[&](const KleidSt& k){ return k.vertraeglich(pr); }),
end(wasch));
return wasch;
}
向量WaschM::aussortieren(程序公关){
瓦希抹掉(
如果需要,请删除(
开始(wasch),
结束(瓦什),
[&](const kleidt&k){返回k.vertraeglich(pr);}),
完(wasch);;
返回瓦什;
}
未测试的代码。看起来像是lambda函数的作业
vector<KleidSt> WaschM::aussortieren(Programm pr){
wasch.erase(
remove_if(
begin(wasch),
end(wasch),
[&](const KleidSt& k){ return k.vertraeglich(pr); }),
end(wasch));
return wasch;
}
向量WaschM::aussortieren(程序公关){
瓦希抹掉(
如果需要,请删除(
开始(wasch),
结束(瓦什),
[&](const kleidt&k){返回k.vertraeglich(pr);}),
完(wasch);;
返回瓦什;
}
未测试的代码。您可以使用lambda函数
vector<KleidSt> WaschM::aussortieren(Programm pr){
wasch.erase(remove_if(begin(wasch),
end(wasch),
[pr](KleidSt const& item) {return item.vertraeglich(pr);},
end(wasch));
return wasch;
}
向量WaschM::aussortieren(程序公关){
wasch.擦除(如果(开始(wasch)),则删除,
结束(瓦什),
[pr](KleidSt const&item){return item.vertraeglich(pr);},
完(wasch);;
返回瓦什;
}
您可以使用lambda函数
vector<KleidSt> WaschM::aussortieren(Programm pr){
wasch.erase(remove_if(begin(wasch),
end(wasch),
[pr](KleidSt const& item) {return item.vertraeglich(pr);},
end(wasch));
return wasch;
}
向量WaschM::aussortieren(程序公关){
wasch.擦除(如果(开始(wasch)),则删除,
结束(瓦什),
[pr](KleidSt const&item){return item.vertraeglich(pr);},
完(wasch);;
返回瓦什;
}
@john再看一遍。页面上没有提到
^
字符。lambda捕获可以使用=
或&
,但不能使用^
。在这种情况下,使用&
,因此pr
是通过引用而不是通过值捕获的。我相信你的意思是&
(或=/code>).^
是按位异或。天哪,我完全瞎了,现在修好了,谢谢大家。@john再看一次。页面上任何地方都没有提到^
字符。lambda捕获可以使用=
或&
,但不能使用^
。在这种情况下,请使用&
,这样pr
就可以通过引用而不是通过引用捕获我相信你的意思是&
(或者=
).^
是按位异或。天哪,我完全瞎了,现在修好了,谢谢大家。你为什么在捕获列表中使用=
而不是&
?是不是=pr
甚至合法?@RemyLebeau,那是个疏忽。你为什么在捕获列表中使用=
而不是
?是不是=pr
甚至合法?@RemyLebe欧,这是一个疏忽。