首页 > 解决方案 > 如何删除向量元素c ++

问题描述

我正在尝试使用两个类创建一个记录列表。一个类enterrecords用于输入值,而另一个类records用于收集、组织和打印它们。这是我的代码:

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

class enterrecords
{
    public:
    string name;
    int age;
    string color;

    enterrecords() 
    {
        name = "";
        age = 0;
        color = "";
    }
    enterrecords(string n, int a, string b) 
    {
        name = n;
        age = a;
        color = b;
    }

};
class records
{
    public:
    vector <enterrecords> list;
    records()
    {

    }
    void addtolist(const enterrecords s)
    {
        this->list.push_back(s);
    }
    void print()
    {
        rank();
        cout << endl;
        for (const auto& it : this->list)
        {
            cout << it.name << " " << it.age << " " << it.color << endl;
        }
        cout << endl;
    }
    void rank()
    {
        sort(list.begin(), list.end(), [](const auto& r1, const auto& r2) {return r1.age < r2.age; });
    }
    void remove(int r)
    {
        list.erase(list.begin() + r-1);
    }
};

int main() 
{
    enterrecords s1;
    enterrecords s2;
    enterrecords s3;
    enterrecords s4;
    records c1;
    s1 = enterrecords("john", 21, "pink");
    s2 = enterrecords("male", 25, "orange");
    s3 = enterrecords("rob", 23, "blue");
    s4 = enterrecords("casie", 31, "red");
    c1.addtolist(s1);
    c1.addtolist(s2);
    c1.addtolist(s3);
    c1.addtolist(s4);
    c1.print();
}

上面的代码自动将人们从最年轻到最年长排序。然而,问题是只有30岁及以下的人才能在记录名单上。任何 31 岁及以上的人都将被忽略或删除他们的记录。所以如果我这样做了c1.addtolist(s4),它应该使他们的记录无效。理想情况下,s4将从包含所有记录元素的向量中删除。

这是该程序的当前输出:

john 21 pink

rob 23 blue

male 25 orange

casie 31 red

这将是程序所需的输出:

john 21 pink

rob 23 blue

male 25 orange

请帮忙。先感谢您。

标签: c++classobjectoopvector

解决方案


像这样的东西:

list.erase(
    remove_if(
        list.begin(),
        list.end(),
        [] ( enterrecords t )
{
    return( t.age > 30 );
} ),
list.end() );

请注意,有一个名为“list”的 STL 容器。调用您的记录向量list有点令人困惑。

这是完整的程序代码

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

class enterrecords
{
public:
    string name;
    int age;
    string color;

    enterrecords()
    {
        name = "";
        age = 0;
        color = "";
    }
    enterrecords(string n, int a, string b)
    {
        name = n;
        age = a;
        color = b;
    }

};
class records
{
public:
    vector <enterrecords> myRecords;
    records()
    {

    }
    void add(const enterrecords& s)
    {
        myRecords.push_back(s);
    }
    void print()
    {
        remove();
        rank();
        cout << endl;
        for (const auto& it : myRecords)
        {
            cout << it.name << " "
                 << it.age << " "
                 << it.color << endl;
        }
        cout << endl;
    }
    void rank()
    {
        sort(myRecords.begin(), myRecords.end(),
             [](const auto& r1, const auto& r2)
        {
            return r1.age < r2.age;
        });
    }
    void remove()
    {
        myRecords.erase(
            remove_if(
                myRecords.begin(),
                myRecords.end(),
                [] ( enterrecords t )
        {
            return( t.age > 30 );
        } ),
        myRecords.end() );
    }
};

int main()
{
    records c1;
    c1.add(enterrecords("john", 21, "pink"));
    c1.add(enterrecords("male", 25, "orange"));
    c1.add(enterrecords("rob", 23, "blue"));
    c1.add(enterrecords("casie", 31, "red"));
    c1.print();
}

推荐阅读