STL容器list使用remove删除自定义类型数据

1.

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <list>

//自定义数据类型
class Person
{
public:
    Person(string name, int age, int height)
    {
        this->m_Name = name;
        this->m_Age = age;
        this->m_Height = height;
    }
    string m_Name;
    int m_Age;
    int m_Height;
};
//自定义数据类型 排序必须指定规则
bool personCompare(Person p1, Person p2)
{
    if (p1.m_Age == p2.m_Age)
    {
        return p1.m_Height < p2.m_Height;   //身高升序
    }
    else
    {
        return p1.m_Age > p2.m_Age; //年龄降序
    }
}
void printPerson(list<Person>& L)
{
    for (list<Person>::iterator it = L.begin(); it != L.end(); it++)
    {
        cout << "姓名: " << (*it).m_Name << " 年龄:" << (*it).m_Age << " 身高:" << (*it).m_Height << endl;
    }
}
void test01()
{
    list<Person> L;
    Person p1("猴子", 500, 185);
    Person p2("猪头", 600, 175);
    Person p3("水怪", 500, 165);
    Person p4("和尚", 500, 195);
    L.push_back(p1);
    L.push_back(p2);
    L.push_back(p3);
    L.push_back(p4);

    L.remove(p2);       //删除 猪头

    printPerson(L);
}

int main()
{
    test01();
    system("Pause");
    return 0;
}

报错输出:

已启动生成…
1>------ 已启动生成: 项目: list删除自定义类型, 配置: Debug Win32 ------
1>list删除自定义类型.cpp
1>E:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includelist(1603,65): error C2676: 二进制“==”:“const _Ty”不定义该运算符或到预定义运算符可接收的类型的转换
1>        with
1>        [
1>            _Ty=Person
1>        ]
1>E:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includelist(1602): message : 在编译 类 模板 成员函数“auto std::list<Person,std::allocator<Person>>::remove(const _Ty &)”时
1>        with
1>        [
1>            _Ty=Person
1>        ]
1>F:VSProgramSTL容器vectorlist删除自定义类型list删除自定义类型.cpp(34): message : 查看对正在编译的 类 模板 实例化“std::list<Person,std::allocator<Person>>”的引用
1>E:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includelist(1614,1): error C2451: 类型为“void”的条件表达式无效
1>E:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includelist(1614,22): message : void 类型的表达式不能转换为其他类型
1>E:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includelist(1603): message : 查看对正在编译的函数 模板 实例化“auto std::list<Person,std::allocator<Person>>::remove_if<std::list<Person,std::allocator<Person>>::remove::<lambda_1f5f748c494c18687aa1671b24bf511b>>(_Pr1)”的引用
1>        with
1>        [
1>            _Pr1=std::list<Person,std::allocator<Person>>::remove::<lambda_1f5f748c494c18687aa1671b24bf511b>
1>        ]
1>已完成生成项目“list删除自定义类型.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========

错误列表有这两个错误:

第一个提示没有定义运算符可接收的类型

就是operater==没指定类型 需要重载

继续报错: 输出栏

已启动生成…
1>------ 已启动生成: 项目: list删除自定义类型, 配置: Debug Win32 ------
1>list删除自定义类型.cpp
1>E:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includelist(1603,1): error C2678: 二进制“==”: 没有找到接受“const _Ty”类型的左操作数的运算符(或没有可接受的转换)
1>        with
1>        [
1>            _Ty=Person
1>        ]
1>F:VSProgramSTL容器vectorlist删除自定义类型list删除自定义类型.cpp(17,10): message : 可能是“bool Person::operator ==(Person &)”
1>E:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includelist(1603,1): message : 尝试匹配参数列表“(const _Ty, const _Ty)”时
1>        with
1>        [
1>            _Ty=Person
1>        ]
1>E:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includelist(1602): message : 在编译 类 模板 成员函数“auto std::list<Person,std::allocator<Person>>::remove(const _Ty &)”时
1>        with
1>        [
1>            _Ty=Person
1>        ]
1>F:VSProgramSTL容器vectorlist删除自定义类型list删除自定义类型.cpp(43): message : 查看对正在编译的 类 模板 实例化“std::list<Person,std::allocator<Person>>”的引用
1>E:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includelist(1614,1): error C2451: 类型为“void”的条件表达式无效
1>E:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includelist(1614,22): message : void 类型的表达式不能转换为其他类型
1>E:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includelist(1603): message : 查看对正在编译的函数 模板 实例化“auto std::list<Person,std::allocator<Person>>::remove_if<std::list<Person,std::allocator<Person>>::remove::<lambda_1f5f748c494c18687aa1671b24bf511b>>(_Pr1)”的引用
1>        with
1>        [
1>            _Pr1=std::list<Person,std::allocator<Person>>::remove::<lambda_1f5f748c494c18687aa1671b24bf511b>
1>        ]
1>已完成生成项目“list删除自定义类型.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========

错误列表

这个错误是重载时没有加const修饰符

再次运行继续报错: 输出栏

已启动生成…
1>------ 已启动生成: 项目: list删除自定义类型, 配置: Debug Win32 ------
1>list删除自定义类型.cpp
1>E:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includelist(1603,1): error C2678: 二进制“==”: 没有找到接受“const _Ty”类型的左操作数的运算符(或没有可接受的转换)
1>        with
1>        [
1>            _Ty=Person
1>        ]
1>F:VSProgramSTL容器vectorlist删除自定义类型list删除自定义类型.cpp(17,10): message : 可能是“bool Person::operator ==(const Person &)”
1>E:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includelist(1603,1): message : 尝试匹配参数列表“(const _Ty, const _Ty)”时
1>        with
1>        [
1>            _Ty=Person
1>        ]
1>E:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includelist(1602): message : 在编译 类 模板 成员函数“auto std::list<Person,std::allocator<Person>>::remove(const _Ty &)”时
1>        with
1>        [
1>            _Ty=Person
1>        ]
1>F:VSProgramSTL容器vectorlist删除自定义类型list删除自定义类型.cpp(43): message : 查看对正在编译的 类 模板 实例化“std::list<Person,std::allocator<Person>>”的引用
1>E:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includelist(1614,1): error C2451: 类型为“void”的条件表达式无效
1>E:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includelist(1614,22): message : void 类型的表达式不能转换为其他类型
1>E:Program Files (x86)Microsoft Visual Studio2019ProfessionalVCToolsMSVC14.29.30037includelist(1603): message : 查看对正在编译的函数 模板 实例化“auto std::list<Person,std::allocator<Person>>::remove_if<std::list<Person,std::allocator<Person>>::remove::<lambda_1f5f748c494c18687aa1671b24bf511b>>(_Pr1)”的引用
1>        with
1>        [
1>            _Pr1=std::list<Person,std::allocator<Person>>::remove::<lambda_1f5f748c494c18687aa1671b24bf511b>
1>        ]
1>已完成生成项目“list删除自定义类型.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========

还是没有限制改值问题

运行成功,成功删除数据

原文地址:https://www.cnblogs.com/yifengs/p/15193075.html