C++ 如何从STL列表中删除结构记录
我有一个类型为C++ 如何从STL列表中删除结构记录,c++,list,stl,C++,List,Stl,我有一个类型为struct的列表,我想从该列表中删除一条特定的记录。最好的方法是什么?我不知道如何使用。删除 struct dat { string s; int cnt; }; void StructList() { list<dat>::iterator itr; //-- create an iterator of type dat dat data1; //-- create a
struct
的列表,我想从该列表中删除一条特定的记录。最好的方法是什么?我不知道如何使用。删除
struct dat
{
string s;
int cnt;
};
void StructList()
{
list<dat>::iterator itr; //-- create an iterator of type dat
dat data1; //-- create a dat object
list<dat> dList; //-- create a list of type dat
itr = dList.begin(); //-- set the dList itereator to the begining of dList
string temp; //-- temp string var for whatever
data1.s = "Hello"; //-- set the string in the struct dat to "Hello"
data1.cnt = 1; //-- set the int in the struct dat to 1
dList.push_back(data1); //-- push the current data1 struct onto the dList
data1.s = "Ted"; //-- set the string in the struct dat to "Ted"
data1.cnt = 2; //-- set the int in the struct dat to 2
dList.push_back(data1); //-- push the current data1 struct onto the dList
cout << "Enter Names to be pushed onto the list\n";
for(int i = 0; i < 5; i++)
{
cout << "Enter Name ";
getline(cin,data1.s); //-- This will allow first and last name
cout << "Enter ID ";
cin >> data1.cnt;
cin.ignore(1000,'\n');
dList.push_back(data1); //-- push this struct onto the list.
}
// I would like to remove the "Ted, 2" record from the list
itr = dList.begin();
dList.pop_front(); //-- this should pop "Hello" and 1 off the list
dList.pop_front(); //-- this should pop "Ted" and 2 off the list
//-- Itereate through the list and output the contents.
for(itr = dList.begin(); itr != dList.end(); itr++)
{
cout << itr->cnt << " " << itr->s << endl;
}
struct-dat
{
字符串s;
int-cnt;
};
void StructList()
{
列表::迭代器itr;//--创建dat类型的迭代器
dat data1;//--创建dat对象
list dList;//--创建dat类型的列表
itr=dList.begin();/--将dList itereator设置为dList的开头
字符串temp;/--temp字符串变量
data1.s=“Hello”;/——将struct dat中的字符串设置为“Hello”
data1.cnt=1;//--将结构dat中的int设置为1
dList.push_back(data1);/--将当前的data1结构推送到dList上
data1.s=“Ted”;/——将结构dat中的字符串设置为“Ted”
data1.cnt=2;//--将结构dat中的int设置为2
dList.push_back(data1);/--将当前的data1结构推送到dList上
cout这是您需要了解的std::list::remove()的参考
如果您有一个类似int
的列表,那么只需remove()
就可以了。在您的情况下,虽然列表包含一个没有定义相等运算符的结构。相等运算符是如何remove()
将知道传入的参数何时与列表中的匹配。注意:这将删除所有匹配的元素,而不仅仅是一个
带有相等运算符的结构如下所示:
struct dat
{
string s;
int cnt;
bool operator==(const struct dat& a) const
{
return ( a.s == this->s && a.cnt == this->cnt )
}
};
或者,您可以通过迭代器从列表中删除元素。在这种情况下,您可以使用erase()
这实际上取决于您试图做什么以及为什么选择使用std::list。
如果你不熟悉这些术语,那么我建议你先多读一读。你没有在代码中使用remove
,是吗?请参见std::list::remove\u如果@tobi303,我拉它是因为我无法使它工作。“//出于某种原因,这不起作用//dList sort();”--谁确切地告诉你可以期待它“dlist.sort();"C++ C++ C++从没有这样工作。这听起来像是移植的Java开发者认为,这只是因为你是如何在java中排序向量或列表的,它必须是C++中的。嗯,C++不这样工作。如果你知道java,你正在尝试学习C++,最好的方法就是读C++的书,而不是O。在C++中,你知道如何在java中做一些事情。“SamVarshavchik”,我想说清楚,我有一本书,读它。它有一个小例子,它是<代码>列表<代码>。所以我想我会看到它如何能用<代码>结构> <代码>数据类型,因此实验。也许我应该说练习。这很有帮助,但现在我们已经偏离了话题,关于最初的问题,如何删除上面示例中的特定记录。我不是想让它做一些不可能的事情,我只是想学习和理解它。这个操作符==
应该是常量。更好的是,让它成为一个自由函数。是的,是的。明白了吗