如何从STL列表中删除结构记录

如何从STL列表中删除结构记录

本文介绍了如何从STL列表中删除结构记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a list of type struct and I want to remove a specific record from that list. What is the best way to do this? I cannot figure out how to do it with .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;
        }

推荐答案

对于std :: list :: remove()- http://en.cppreference.com/w/cpp/container/list/remove

This is the reference you need to understand for std::list::remove() - http://en.cppreference.com/w/cpp/container/list/remove

如果您有类似 int 之类的列表,则只需 remove()即可.在您的情况下,尽管您的列表包含未为其定义相等运算符的结构.相等运算符是 remove()如何知道传入的参数何时与列表中的内容匹配.注意:这将删除所有匹配的元素,而不仅仅是一个.

If you had a list of something like int then just remove() would work. In your case though your list contains a struct with no equality operator defined for it. The equality operator is how remove() will know when the param passed in matches what's in the list. Note: this will remove all elements that match, not just one.

您的带有相等运算符的结构看起来像这样:

Your struct with an equality operator would looks something like this:

struct dat
{
    string s;
    int cnt;

    bool operator==(const struct dat& a) const
    {
         return ( a.s == this->s && a.cnt == this->cnt )
    }
};

或者,您可以通过迭代器从列表中删除元素.在这种情况下,您将使用 erase().

Alternately you can remove elements from a list by iterator. In that case you would use erase().

这实际上取决于您要执行的操作以及选择使用std :: list的原因.如果您不熟悉这些术语,那么建议您先阅读更多内容.

It really depends on what you're trying to do and why you've chosen to use a std::list.If you're not familiar with these terms then I'd recommend reading up more first.

这篇关于如何从STL列表中删除结构记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:17