How to remove a struct record from an STL list

爱⌒轻易说出口 提交于 2021-02-05 12:24:55

问题


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

回答1:


This is the reference you need to understand for std::list::remove() - http://en.cppreference.com/w/cpp/container/list/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 )
    }
};

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

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.



来源:https://stackoverflow.com/questions/43419773/how-to-remove-a-struct-record-from-an-stl-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!