【问题标题】:How to remove a struct record from an STL list如何从 STL 列表中删除结构记录
【发布时间】:2017-04-14 21:59:35
【问题描述】:

我有一个struct 类型的列表,我想从该列表中删除特定记录。做这个的最好方式是什么?我不知道如何使用.remove

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;
        }

【问题讨论】:

  • 您没有在该代码中使用remove,是吗?
  • @tobi303,我拉它是因为我无法让它工作。
  • "// 出于某种原因这不起作用 // dList.sort();" -- 谁告诉你你可以期待“dlist.sort();”去工作? C++ 从来没有这样工作过。这听起来像是来自一个移植的 Java 开发人员,他认为仅仅因为这是你在 Java 中对向量或列表进行排序的方式,它必须是在 C++ 中完成的方式。好吧,C++ 不是这样工作的。如果您了解 Java 并且正在尝试学习 C++,那么最好的方法是阅读您的 C++ 书籍,而不是猜测如何在 C++ 中做一些您知道如何在 Java 中做的事情。
  • @SamVarshavchik,为了清楚起见,我有一本书,正在阅读。它有一个&lt;int&gt; list 的小例子。所以我想我会看到它如何与struct 数据类型一起工作,因此进行了实验。也许我应该说练习。无论哪种方式,您的输入都是有帮助的,但现在我们偏离了原始问题的主题,即如何删除上述示例中的特定记录。我不是想让它做一些不可能的事情,我是在努力学习和理解它。

标签: c++ list stl


【解决方案1】:

这是您需要了解的 std::list::remove() 参考 - http://en.cppreference.com/w/cpp/container/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 的原因。 如果您不熟悉这些术语,那么我建议您先阅读更多内容。

【讨论】:

  • operator== 应该是 const。更好的是,让它成为一个免费的功能。
  • 是的,没错。明白了
猜你喜欢
  • 2022-08-10
  • 2010-09-29
  • 2012-12-10
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多