如果字符串向量包含 char 'p',我如何检查 C++
How do I check in c++, if a vector of strings contains char 'p'
1 ) 假设我有一个向导 v
的向量(向导有一个名字、姓氏、字符串向量,其中包括 he/she 参加的科目,以及 he/she属于)
2) 我有一个空向量 cpy
,我想在其中复制那些参加某个主题的巫师,其中有一个字母 'p'。
就我而言,我只想复制 Laura,因为她参加体育运动,这是唯一包含 'p' 的主题。
//wizard.cpp
Wizard::Wizard(string name, string lastname, Vector<string> subjects, Haus haus) :
name{name}, lastname{lastname}, subjects{subjects}, haus{haus}
{
if (name.empty() || lastname.empty() ){
throw runtime_error("name or lastname wrong");
}
}
string Wizard::get_name() const {
return name;
}
string Wizard::get_lastname() const {
return lastname;
}
Vector<string> Wizard::get_subjects() const {
return subjects;
}
Haus Wizard::get_haus() const {
return haus;
}
Vector<Wizard> v;
Wizard harry("Harry", "Potter", {"magic", "music"}, Haus::Gryffindor);
Wizard ron("Ron", "Weasley", {"magic", "dancing"}, Haus::Gryffindor);
Wizard hermione("Hermione", "Granger", {"magic", "defence"}, Haus::Gryffindor);
Wizard laura("Laura", "Someone", {"running", "sports"}, Haus::Slytherin);
v.push_back(harry);
v.push_back(ron);
v.push_back(hermione);
v.push_back(laura);
Vector<Wizard> cpy;
// v is the original vector of all wizards
copy_if(v.begin(), v.end(), back_inserter(cpy), [](const Wizard& w) {
return(any_of(w.get_subjects().begin(), w.get_subjects().end(), [](const string& s) {
return s.find('p') != string::npos;
}));
});
我以退出代码 11 结束
您到处都在使用 值,包括 get_subjects()
的 return 类型。
因此,下面的两个迭代器:
w.get_subjects().begin(), w.get_subjects().end()
指完全独立、不相关的份载体。
将迭代器与两个不相关的向量进行比较具有未定义的行为,这永远行不通。
相反,您的访问器应该 return by (const) reference。
对于初学者声明函数get_subjects
like
const Vector<string> & Wizzard::get_subjects() const {
return subjects;
}
否则在本次算法调用中
any_of(w.get_subjects().begin(), w.get_subjects().end(),...);
begin
和 end
return 不同范围(向量)的迭代器。
1 ) 假设我有一个向导 v
的向量(向导有一个名字、姓氏、字符串向量,其中包括 he/she 参加的科目,以及 he/she属于)
2) 我有一个空向量 cpy
,我想在其中复制那些参加某个主题的巫师,其中有一个字母 'p'。
就我而言,我只想复制 Laura,因为她参加体育运动,这是唯一包含 'p' 的主题。
//wizard.cpp
Wizard::Wizard(string name, string lastname, Vector<string> subjects, Haus haus) :
name{name}, lastname{lastname}, subjects{subjects}, haus{haus}
{
if (name.empty() || lastname.empty() ){
throw runtime_error("name or lastname wrong");
}
}
string Wizard::get_name() const {
return name;
}
string Wizard::get_lastname() const {
return lastname;
}
Vector<string> Wizard::get_subjects() const {
return subjects;
}
Haus Wizard::get_haus() const {
return haus;
}
Vector<Wizard> v;
Wizard harry("Harry", "Potter", {"magic", "music"}, Haus::Gryffindor);
Wizard ron("Ron", "Weasley", {"magic", "dancing"}, Haus::Gryffindor);
Wizard hermione("Hermione", "Granger", {"magic", "defence"}, Haus::Gryffindor);
Wizard laura("Laura", "Someone", {"running", "sports"}, Haus::Slytherin);
v.push_back(harry);
v.push_back(ron);
v.push_back(hermione);
v.push_back(laura);
Vector<Wizard> cpy;
// v is the original vector of all wizards
copy_if(v.begin(), v.end(), back_inserter(cpy), [](const Wizard& w) {
return(any_of(w.get_subjects().begin(), w.get_subjects().end(), [](const string& s) {
return s.find('p') != string::npos;
}));
});
我以退出代码 11 结束
您到处都在使用 值,包括 get_subjects()
的 return 类型。
因此,下面的两个迭代器:
w.get_subjects().begin(), w.get_subjects().end()
指完全独立、不相关的份载体。
将迭代器与两个不相关的向量进行比较具有未定义的行为,这永远行不通。
相反,您的访问器应该 return by (const) reference。
对于初学者声明函数get_subjects
like
const Vector<string> & Wizzard::get_subjects() const {
return subjects;
}
否则在本次算法调用中
any_of(w.get_subjects().begin(), w.get_subjects().end(),...);
begin
和 end
return 不同范围(向量)的迭代器。