• 全部
...

C++ STL list 删除一个元素(remove) 报错!!

vipsite7521 2013-04-12 04:04:34
#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;
}


...全文
给本帖投票
1331 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
最恋芬 2015-11-28
  • 打赏
  • 举报
回复
猜一下 楼主 可能是在移植Java的代码 因为Java的list 就是这么用的。 多次插入 ,随意删除。况且 还可以加上 try catch语句。 移植 到 C++会 报错, 具体 为啥 编译错误 ,我也不知 ,但是改成 erase 应该没问题 。如下: for( list<student>::iterator it=stulist.begin();it!=stulist.end();it++) { if(it->name=="lijie") { stulist.erase(it); } };
最恋芬 2015-11-28
  • 打赏
  • 举报
回复
真是看不下去了 ,被一楼刷屏 刷的 ,人家问的是编译为什么编译错误 ,尽回答些没用的东西 , 什么多次插入 ,什么没插入就删除什么的 ,这些都是运行时 逻辑错误,编译器 是不知道的, 你先帮别人解决编译错误再说好不 。
tracyh1988 2013-04-12
  • 打赏
  • 举报
回复
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) { return s.name == "tracy"; } }; int main() { list<stu> lis; stu s; s.name = "tracy"; s.number = "1"; lis.push_back(s); s.name = "bob"; s.number = "2"; lis.push_back(s); lis.remove_if(fun()); //删除语句 for (list<stu>::iterator it = lis.begin(); it != lis.end(); it++) { cout<<(*it).name; cout<<" "<<(*it).number<<endl; } }
czjsai1989 2013-04-12
  • 打赏
  • 举报
回复
你可以直接将list<student>写成list<student*>,这样你就可以删除对应的数据了。或者如果还是要用list<student>的话,可以重载一下student的==操作符函数。
vipsite7521 2013-04-12
  • 打赏
  • 举报
回复
引用 16 楼 zhcosin 的回复:
引用 15 楼 chenyg5 的回复:引用 13 楼 zhcosin 的回复:引用 12 楼 chenyg5 的回复:对不起大家,第一次发帖;刚才我的程序提交的有点问题;我稍改了一下; student stu1,stu2; list<student> stulist; stu1.name="chenyigeng"; stu1.address="hebei……
for( list<student>::iterator it=stulist.begin();it!=stulist.end();it++) { if(it->name=="lijie") { stulist.remove(it); } }; 这样可以吗? 想不出来怎么写了,能帮我写出来吗,哈哈,谢谢
zhcosin 2013-04-12
  • 打赏
  • 举报
回复
引用 15 楼 chenyg5 的回复:
引用 13 楼 zhcosin 的回复:引用 12 楼 chenyg5 的回复:对不起大家,第一次发帖;刚才我的程序提交的有点问题;我稍改了一下; student stu1,stu2; list<student> stulist; stu1.name="chenyigeng"; stu1.address="hebei"; stu1.tel="138111……
使用迭代器。
vipsite7521 2013-04-12
  • 打赏
  • 举报
回复
引用 13 楼 zhcosin 的回复:
引用 12 楼 chenyg5 的回复:对不起大家,第一次发帖;刚才我的程序提交的有点问题;我稍改了一下; student stu1,stu2; list<student> stulist; stu1.name="chenyigeng"; stu1.address="hebei"; stu1.tel="13811114116"; stu1.score=……
谢谢朋友,你说的对,那么list里的对象名称如何引用呢?这种复合结构根据值去删除是不是没有办法调用remove处理了?
vipsite7521 2013-04-12
  • 打赏
  • 举报
回复
13楼的朋友,你说的对,那我如何删除list里的对象呢?
zhcosin 2013-04-12
  • 打赏
  • 举报
回复
引用 12 楼 chenyg5 的回复:
对不起大家,第一次发帖;刚才我的程序提交的有点问题;我稍改了一下; student stu1,stu2; list<student> stulist; stu1.name="chenyigeng"; stu1.address="hebei"; stu1.tel="13811114116"; stu1.score=1123.423; stulist.p……
问题在于,插入链表的时候是复制了一个插进去的,也就是说,链表里的那个对象和你外面使用的那个对象已经是两个不同的对象了,不信的话你修改外面的对象,然后输出链表里保存的那个对象,看看是不是没有被修改。
vipsite7521 2013-04-12
  • 打赏
  • 举报
回复
对不起大家,第一次发帖;刚才我的程序提交的有点问题;我稍改了一下; student stu1,stu2; list<student> stulist; stu1.name="chenyigeng"; stu1.address="hebei"; stu1.tel="13811114116"; stu1.score=1123.423; stulist.push_back(stu1); stu2.name="lijie"; stu2.address="henan"; stu2.tel="13411323116"; stu2.score=123.423; stulist.push_back(stu2); const student &ss=stu1; stulist.remove(ss); 我根据remove的参数类型定义了一个常引用,还是报错;另外我将其改成基本类型int 或者char 删除某一个元素的时候就可以,难道remove的方法删除一个结构体的数据不支持吗?
vipsite7521 2013-04-12
  • 打赏
  • 举报
回复
引用 8 楼 zhcosin 的回复:
引用 7 楼 chenyg5 的回复:引用 4 楼 zhcosin 的回复:引用 3 楼 jjq1011snake 的回复:查MSDN,remove用法和用例。 应该查 STL 的用法,他连用法都没搞清楚,语法错误一堆,逻辑错误也是一堆,这是我见过的错误最多的程序了。 错误一堆是什么错误?我编译的时候就调用remove的时候报错了啊?如果没有这个,插入,显示都没问……
1remove的参数是 void remove( const TYPE &val ); 2我把元素插入链表了,从新定义了4个对象, 同样还是报原来的错误,我如果改成基本类型的int 或者char 都没有问题
海的神话 2013-04-12
  • 打赏
  • 举报
回复

你的list里都没这个值,怎么删除呢
xubo1210 2013-04-12
  • 打赏
  • 举报
回复
1L正解,ss都没插入,怎么删除?
zhcosin 2013-04-12
  • 打赏
  • 举报
回复
引用 7 楼 chenyg5 的回复:
引用 4 楼 zhcosin 的回复:引用 3 楼 jjq1011snake 的回复:查MSDN,remove用法和用例。 应该查 STL 的用法,他连用法都没搞清楚,语法错误一堆,逻辑错误也是一堆,这是我见过的错误最多的程序了。 错误一堆是什么错误?我编译的时候就调用remove的时候报错了啊?如果没有这个,插入,显示都没问题呢
第一:remove的参数是迭器,你传递给它的参数不对。这个是语法错误,当然通不过编译的。 第二:那个元素你根本没有插入到链表里就想从链表里删除它,这个怎么可能呢,这个是逻辑错误,编译不会有问题,运行要出问题。 第三:同一个对象,反复插入到链表里了,这个会导致什么后果我也不清楚,没有测试过,应该是什么后果都有可能,看编译器是怎么实现的了。
vipsite7521 2013-04-12
  • 打赏
  • 举报
回复
引用 4 楼 zhcosin 的回复:
引用 3 楼 jjq1011snake 的回复:查MSDN,remove用法和用例。 应该查 STL 的用法,他连用法都没搞清楚,语法错误一堆,逻辑错误也是一堆,这是我见过的错误最多的程序了。
错误一堆是什么错误?我编译的时候就调用remove的时候报错了啊?如果没有这个,插入,显示都没问题呢
vipsite7521 2013-04-12
  • 打赏
  • 举报
回复
引用 1 楼 zhcosin 的回复:
插都没插入链表,你还从链表删除它,不报错才怪。
我是把stu多次赋值,然后插入list里,我改了一下 student stu1,stu2; list<student> stulist; stu1.name="chenyigeng"; stu1.address="hebei"; stu1.tel="13811114116"; stu1.score=1123.423; stulist.push_back(stu1); stu2.name="lijie"; stu2.address="henan"; stu2.tel="13411323116"; stu2.score=123.423; stulist.push_back(stu2); const student &ss=stu1; stulist.remove(ss); //这样也是编译不过 instantiated from here
vipsite7521 2013-04-12
  • 打赏
  • 举报
回复
引用 2 楼 zhcosin 的回复:
乱七八糟的,你那个 stu 怎么还多次插入链表了?
我是把stu多次赋值,然后插入list里,我改了一下 student stu1,stu2; list<student> stulist; stu1.name="chenyigeng"; stu1.address="hebei"; stu1.tel="13811114116"; stu1.score=1123.423; stulist.push_back(stu1); stu2.name="lijie"; stu2.address="henan"; stu2.tel="13411323116"; stu2.score=123.423; stulist.push_back(stu2); const student &ss=stu1; stulist.remove(ss); //这样也是编译不过 instantiated from here
zhcosin 2013-04-12
  • 打赏
  • 举报
回复
引用 3 楼 jjq1011snake 的回复:
查MSDN,remove用法和用例。
应该查 STL 的用法,他连用法都没搞清楚,语法错误一堆,逻辑错误也是一堆,这是我见过的错误最多的程序了。
SNAKE-SNAKE 2013-04-12
  • 打赏
  • 举报
回复
查MSDN,remove用法和用例。
zhcosin 2013-04-12
  • 打赏
  • 举报
回复
乱七八糟的,你那个 stu 怎么还多次插入链表了?
加载更多回复(1)
第一篇 预备知识 第1章 C++编程技术 2 1.1 类和对象 2 1.2 类的继承 5 1.3 函数重载 5 1.4 访问控制 7 1.5 操作符重载 8 1.6 显式类型转换 9 1.7 异常处理 13 1.8 名字空间 17 1.9 友员函数 20 1.10 内联函数 21 1.11 静态成员 22 1.12 本章小结 23 第2章 C++模板技术 25 2.1 函数模板 25 2.2 类模板 27 2.3 模板完全特化 28 2.4 函数模板重载 30 2.5 类模板继承 30 2.6 本章小结 31 第3章 C++ I/O流技术 32 3.1 I/O流类 32 3.2 标准输入输出 34 3.3 文件输入输出 36 3.4 流的格式控制 41 3.5 本章小结 45 第二篇 C++ STL泛化技术基础 第4章 C++ STL泛型库概述 48 4.1 C++ STL的发展历程 48 4.2 C++ STL的各种实现版本 49 4.2.1 HP STL 49 4.2.2 SGI STL 50 4.2.3 STLport 50 4.2.4 P.J.Plauger STL 50 4.2.5 Rouge Wave STL 50 4.3 C++ STL的Visual C++编译 50 4.4 C++ STL的体系结构 52 4.4.1 容器(Container) 52 4.4.2 迭代器(Iterator) 53 4.4.3 算法(Algorithm) 53 4.4.4 函数对象(Function Object) 54 4.4.5 适配器(Adapter) 55 4.4.6 内存分配器(Allocator) 56 4.4.7 概念(Concept)和模型(Model) 56 4.5 C++ STL存在的一些问题 57 4.6 本章小结 57 第5章 C++ STL泛化技术分析 58 5.1 算法和迭代器 58 5.1.1 算法 58 5.1.2 迭代器 61 5.1.3 函数对象 65 5.1.4 适配器 68 5.2 内存分配器和容器 74 5.2.1 内存分配器 75 5.2.2 容器 77 5.3 概念 82 5.3.1 基础性概念 82 5.3.2 容器概念 84 5.3.3 迭代器概念 86 5.3.4 函数对象概念 88 5.4 本章小结 89 第三篇 C++ STL容器技术 第6章 vector向量容器 92 6.1 vector技术原理 92 6.2 vector应用基础 94 6.3 本章小结 101 第7章 deque双端队列容器 102 7.1 deque技术原理 102 7.2 deque应用基础 108 7.3 本章小结 115 第8章 list双向链表容器 116 8.1 list技术原理 116 8.2 list应用基础 124 8.3 本章小结 131 第9章 slist单向链表容器 132 9.1 slist技术原理 132 9.2 slist应用基础 140 9.3 本章小结 148 第10章 bit_vector位向量容器 149 10.1 bit_vector技术原理 149 10.2 bit_vector应用基础 156 10.3 本章小结 161 第11章 set集合容器 162 11.1 set技术原理 162 11.2 set应用基础 181 11.3 本章小结 186 第12章 multiset多重集合容器 187 12.1 multiset技术原理 187 12.2 multiset应用基础 190 12.3 本章小结 196 第13章 map映照容器 197 13.1 map技术原理 197 13.2 map应用基础 200 13.3 本章小结 206 第14章 multimap多重映照容器 207 14.1 multimap技术原理 207 14.2 multimap应用基础 210 14.3 本章小结 216 第15章 hash_set哈希集合容器 217 15.1 hash_set技术原理 217 15.2 hash_set应用基础 230 15.3 本章小结 234 第16章 hash_map哈希映照容器 235 16.1 hash_map技术原理 235 16.2 hash_map应用基础 237 16.3 本章小结 242 第17章 string基本字符序列容器 243 17.1 string技术原理 243 17.2 string应用基础 258 17.3 本章小结 264 第18章 stack堆栈容器 265 18.1 stack技术原理 265 18.2 stack应用基础 266 18.3 本章小结 269 第19章 queue队列容器 270 19.1 queue技术原理 270 19.2 queue应用基础 271 19.3 本章小结 274 第20章 priority_queue优先队列容器 275 20.1 priority_queue技术原理 275 20.2 priority_queue应用基础 278 20.3 本章小结 281 第四篇 C++ STL算法技术 第21章 非变易算法 284 21.1 逐个容器元素for_each 284 21.2 查找容器元素find 285 21.3 条件查找容器元素find_if 286 21.4 邻近查找容器元素adjacent_find 287 21.5 范围查找容器元素find_first_of 289 21.6 统计等于某值的容器元素个数count 290 21.7 条件统计容器元素个数count_if 291 21.8 元素不匹配查找mismatch 293 21.9 元素相等判断equal 295 21.10 子序列搜索search 296 21.11 重复元素子序列搜索search_n 299 21.12 最后一个子序列搜索find_end 301 21.13 本章小结 303 第22章 变易算法 304 22.1 元素复制copy 304 22.2 反向复制copy_backward 305 22.3 元素交换swap 306 22.4 迭代器交换iter_swap 307 22.5 区间元素交换swap_ranges 308 22.6 元素变换transform 309 22.7 替换 310 22.8 条件替换replace_if 311 22.9 替换和复制replace_copy 312 22.10 条件替换和复制replace_copy_if 313 22.11 填充fill 314 22.12 n次填充fill_n 315 22.13 随机生成元素generate 316 22.14 随机生成n个元素generate_n 317 22.15 移除复制remove_copy 318 22.16 条件移除复制remove_copy_if 319 22.17 移除remove 320 22.18 条件移除remove_if 321 22.19 不连续重复元素复制unique_copy 322 22.20 剔除连续重复元素unique 324 22.21 元素反向reverse 325 22.22 反向复制reverse_copy 326 22.23 旋转rotate 327 22.24 旋转复制rotate_copy 329 22.25 随机抖动random_shuffle 330 22.26 随机采样random_sample 331 22.27 容器分割partition 333 22.28 容器稳定分割stable_partition 335 22.29 本章小结 338 第23章 排序算法 339 23.1 元素入堆push_heap 339 23.2 创建堆make_heap 343 23.3 元素出堆pop_heap 348 23.4 堆排序sort_heap 351 23.5 是否为堆is_heap 352 23.6 局部排序partial_sort 354 23.7 局部排序复制partial_sort_copy 356 23.8 排序sort 359 23.9 归并merge 366 23.10 内部归并inplace_merge 368 23.11 稳定排序stable_sort 376 23.12 是否排序is_sorted 383 23.13 第n个元素nth_element 384 23.14 下确界lower_bound 386 23.15 上确界upper_bound 388 23.16 等价区间equal_range 390 23.17 折半搜索binary_search 392 23.18 子集合includes 393 23.19 集合求并set_union 394 23.20 集合求交set_ intersection 396 23.21 集合求差set_difference 398 23.22 集合求异set_symmetric_difference 399 23.23 最小值min 401 23.24 最大值max 402 23.25 最小元素min_element 403 23.26 最大元素max_element 404 23.27 字典比较lexicographical_compare 405 23.28 下一排列组合next_permutation 406 23.29 上一排列组合prev_permutation 409 23.30 本章小结 411 第24章 数值算法 412 24.1 递增赋值iota 412 24.2 元素求和accumulate 413 24.3 两序列元素内积inner_product 414 24.4 部分元素求和partial_sum 415 24.5 相邻元素求差adjacent_difference 417 24.6 n次方计算power 419 24.7 本章小结 421 第五篇 C++ STL迭代器技术 第25章 输入输出流迭代器 424 25.1 输入流迭代器 424 25.2 输出流迭代器 426 25.3 本章小结 427 第26章 插入/反向/存储迭代器 428 26.1 向前插入迭代器 428 26.2 向后插入迭代器 429 26.3 插入迭代器 431 26.4 反向迭代器 432 26.5 反向双向迭代器 434 26.6 原始存储迭代器 435 26.7 本章小结 437 附录 STL版权说明 438

65,195

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧

手机看
关注公众号

关注公众号

客服 返回
顶部