C++ STL list 剔除一个元素(remove) 报错!
C++ STL list 删除一个元素(remove) 报错!!
#include <iostream>
#include <cstdlib>
#include <string>
#include <bitset>
#include <vector>
#include <iterator>
#include <list>
#include <unistd.h>
#include <istream>
#include <iomanip>
using namespace std;
struct student
{
char *name;
char *address;
char *tel;
float score;
};
//我定义了一个结构体,然后定义了结构体list,向结构体list扔了4个元素,然后想remove掉其中的一
//个,结果在remove的时候报错了
int main(int argc,char *argv[])
{
student stu;
list<student> stulist;
stu.name="chenyigeng";
stu.address="hebei";
stu.tel="13811114116";
stu.score=1123.423;
stulist.push_back(stu);
stu.name="lijie";
stu.address="henan";
stu.tel="13411323116";
stu.score=123.423;
stulist.push_back(stu);
stu.name="xuhontao";
stu.address="shandong";
stu.tel="122311323116";
stu.score=1123.423;
stulist.push_back(stu);
stu.name="jianian";
stu.address="langfang";
stu.tel="13423323116";
stu.score=12334.423;
stulist.push_back(stu);
student s1={"lijie","henan","13411323116",123.423};
const student &ss=s1;
stulist.remove(ss); //这里编译不通过
system("pause");
return 0;
}
------解决方案--------------------
插都没插入链表,你还从链表删除它,不报错才怪。
------解决方案--------------------
乱七八糟的,你那个 stu 怎么还多次插入链表了?
------解决方案--------------------
第一:remove的参数是迭器,你传递给它的参数不对。这个是语法错误,当然通不过编译的。
第二:那个元素你根本没有插入到链表里就想从链表里删除它,这个怎么可能呢,这个是逻辑错误,编译不会有问题,运行要出问题。
第三:同一个对象,反复插入到链表里了,这个会导致什么后果我也不清楚,没有测试过,应该是什么后果都有可能,看编译器是怎么实现的了。
------解决方案--------------------
使用迭代器。
------解决方案--------------------
你可以直接将list<student>写成list<student*>,这样你就可以删除对应的数据了。或者如果还是要用list<student>的话,可以重载一下student的==操作符函数。
------解决方案--------------------
list是必须从头开始一个一个遍历查看是否等于所要删除的内容的节点的,不能直接定义一个iterator指向要删除的节点,而且remove删除的是const参数,可以用remove_if来实现,下面是一个小例子哈。
#include <iostream>
#include <list>
#include <iterator>
#include <stdio.h>
#include <string>
using namespace std;
struct stu
{
char* number;
char* name;
};
class fun
{
public:
bool operator()(const stu& s)
#include <iostream>
#include <cstdlib>
#include <string>
#include <bitset>
#include <vector>
#include <iterator>
#include <list>
#include <unistd.h>
#include <istream>
#include <iomanip>
using namespace std;
struct student
{
char *name;
char *address;
char *tel;
float score;
};
//我定义了一个结构体,然后定义了结构体list,向结构体list扔了4个元素,然后想remove掉其中的一
//个,结果在remove的时候报错了
int main(int argc,char *argv[])
{
student stu;
list<student> stulist;
stu.name="chenyigeng";
stu.address="hebei";
stu.tel="13811114116";
stu.score=1123.423;
stulist.push_back(stu);
stu.name="lijie";
stu.address="henan";
stu.tel="13411323116";
stu.score=123.423;
stulist.push_back(stu);
stu.name="xuhontao";
stu.address="shandong";
stu.tel="122311323116";
stu.score=1123.423;
stulist.push_back(stu);
stu.name="jianian";
stu.address="langfang";
stu.tel="13423323116";
stu.score=12334.423;
stulist.push_back(stu);
student s1={"lijie","henan","13411323116",123.423};
const student &ss=s1;
stulist.remove(ss); //这里编译不通过
system("pause");
return 0;
}
C++
STL
Iterator
------解决方案--------------------
插都没插入链表,你还从链表删除它,不报错才怪。
------解决方案--------------------
乱七八糟的,你那个 stu 怎么还多次插入链表了?
------解决方案--------------------
第一:remove的参数是迭器,你传递给它的参数不对。这个是语法错误,当然通不过编译的。
第二:那个元素你根本没有插入到链表里就想从链表里删除它,这个怎么可能呢,这个是逻辑错误,编译不会有问题,运行要出问题。
第三:同一个对象,反复插入到链表里了,这个会导致什么后果我也不清楚,没有测试过,应该是什么后果都有可能,看编译器是怎么实现的了。
------解决方案--------------------
使用迭代器。
------解决方案--------------------
你可以直接将list<student>写成list<student*>,这样你就可以删除对应的数据了。或者如果还是要用list<student>的话,可以重载一下student的==操作符函数。
------解决方案--------------------
list是必须从头开始一个一个遍历查看是否等于所要删除的内容的节点的,不能直接定义一个iterator指向要删除的节点,而且remove删除的是const参数,可以用remove_if来实现,下面是一个小例子哈。
#include <iostream>
#include <list>
#include <iterator>
#include <stdio.h>
#include <string>
using namespace std;
struct stu
{
char* number;
char* name;
};
class fun
{
public:
bool operator()(const stu& s)