一个向量包含 class 个对象,class 个对象每个对象包含 3 个字符串。我如何找到特定的字符串,然后删除整个元素?
A vector holds class objects, The class object contains 3 strings per object. How do i find the specific string, then delete the entire element?
我有一个包含 3 个元素的 class 例如 {first_name, Last_name, Phone}
我有一个包含这组信息的向量。我可以用什么方式寻找集合中的单个元素,例如 find(last_name),并删除包含该特定姓氏的所有元素?
我已经尝试了很多例子,并且在世界范围内进行了广泛的搜索 google。请帮忙。附件是一些代码:
int number = 4;
vector <Friend> BlackBook(number);
Friend a("John", "Nash", "4155555555");
Friend d("Homer", "Simpson", "2064375555");
BlackBook[0] = a;
BlackBook[1] = d;
现在只是相同的基本设置代码。这是我尝试过的几件事。但是我越看代码说的越多,它似乎越不允许字符串参数......但是我不知道如何就特定字符串进行 class 争论...好吧,我不知道我做错了什么。我有一种感觉,我可以用指针来做到这一点,但整个指针的东西还没有点击。但是这里有一些我试过的东西。
vector <Friend> :: iterator frienddlt;
frienddlt = find (BlackBook.begin(), BlackBook.end(), nofriend);
if (frienddlt != BlackBook.end())
{
BlackBook.erase( std::remove( BlackBook.begin(), BlackBook.end(), nofriend), BlackBook.end() );
}
else
{
cout << nofriend <<" was not found\n" << "Please Reenter Last Name:\t\t";
}
当我编译项目时,头文件 stl_algo.h 打开并指向第 1133 行。
任何帮助将非常感激!!谢谢你!
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct Friend {
string first_name;
string last_name;
string phone;
};
bool RemoveByName (vector<Friend>& black_book, const string& name) {
vector<Friend>::iterator removed_it = remove_if(
black_book.begin(), black_book.end(),
[&name](const Friend& f){return f.first_name == name;});
if (removed_it == black_book.end())
return false;
black_book.erase(removed_it, black_book.end());
return true;
}
int main() {
vector <Friend> black_book {
Friend {"John", "Nash", "4155555555"},
Friend {"Homer", "Simpson", "2064375555"}
};
if (RemoveByName(black_book, "John")) {
cout << "removed" << endl;
} else {
cout << "not found" << endl;
}
if (RemoveByName(black_book, "Tom")) {
cout << "removed" << endl;
} else {
cout << "not found" << endl;
}
for (int i = 0; i < black_book.size(); ++i) {
Friend& f = black_book.at(i);
cout << f.first_name << " " << f.last_name << " " << f.phone << endl;
}
return 0;
}
输出:
removed
not found
Homer Simpson 2064375555
当然,您始终可以遍历所有 Friend 元素并手动删除它们。
Blackbook::iterator friend = Blackbook.begin();
while (friend != Blackbook.end())
{
if (friend->last_name == bad_name)
{
friend = Blackbook.erase(friend);
}
else
{
++friend;
}
}
我有一个包含 3 个元素的 class 例如 {first_name, Last_name, Phone}
我有一个包含这组信息的向量。我可以用什么方式寻找集合中的单个元素,例如 find(last_name),并删除包含该特定姓氏的所有元素?
我已经尝试了很多例子,并且在世界范围内进行了广泛的搜索 google。请帮忙。附件是一些代码:
int number = 4;
vector <Friend> BlackBook(number);
Friend a("John", "Nash", "4155555555");
Friend d("Homer", "Simpson", "2064375555");
BlackBook[0] = a;
BlackBook[1] = d;
现在只是相同的基本设置代码。这是我尝试过的几件事。但是我越看代码说的越多,它似乎越不允许字符串参数......但是我不知道如何就特定字符串进行 class 争论...好吧,我不知道我做错了什么。我有一种感觉,我可以用指针来做到这一点,但整个指针的东西还没有点击。但是这里有一些我试过的东西。
vector <Friend> :: iterator frienddlt;
frienddlt = find (BlackBook.begin(), BlackBook.end(), nofriend);
if (frienddlt != BlackBook.end())
{
BlackBook.erase( std::remove( BlackBook.begin(), BlackBook.end(), nofriend), BlackBook.end() );
}
else
{
cout << nofriend <<" was not found\n" << "Please Reenter Last Name:\t\t";
}
当我编译项目时,头文件 stl_algo.h 打开并指向第 1133 行。 任何帮助将非常感激!!谢谢你!
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct Friend {
string first_name;
string last_name;
string phone;
};
bool RemoveByName (vector<Friend>& black_book, const string& name) {
vector<Friend>::iterator removed_it = remove_if(
black_book.begin(), black_book.end(),
[&name](const Friend& f){return f.first_name == name;});
if (removed_it == black_book.end())
return false;
black_book.erase(removed_it, black_book.end());
return true;
}
int main() {
vector <Friend> black_book {
Friend {"John", "Nash", "4155555555"},
Friend {"Homer", "Simpson", "2064375555"}
};
if (RemoveByName(black_book, "John")) {
cout << "removed" << endl;
} else {
cout << "not found" << endl;
}
if (RemoveByName(black_book, "Tom")) {
cout << "removed" << endl;
} else {
cout << "not found" << endl;
}
for (int i = 0; i < black_book.size(); ++i) {
Friend& f = black_book.at(i);
cout << f.first_name << " " << f.last_name << " " << f.phone << endl;
}
return 0;
}
输出:
removed
not found
Homer Simpson 2064375555
当然,您始终可以遍历所有 Friend 元素并手动删除它们。
Blackbook::iterator friend = Blackbook.begin();
while (friend != Blackbook.end())
{
if (friend->last_name == bad_name)
{
friend = Blackbook.erase(friend);
}
else
{
++friend;
}
}