提问:关于list容器的操作问题 lst.splice(iter, beg, end)

ysysbaobei 2008-11-21 10:24:02
题目要求:书本讲解到list容器的操作的时候,提供了操作:lst.splice(iter, beg, end)
我的问题:下面写了个程序来练习,有一个错误,麻烦大家帮忙解决下

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

int main()
{
int array[] = {1, 2, 3, 4, 5, 6, 7, 8};
list<int> ilist(array, array + 8);
list<int>::iterator lter = ilist.begin(), beg = ilist.begin(), end = ilist.end();
++beg;
ilist.splice(lter, beg, end);
for (lter = ilist.begin(); lter != ilist.end(); ++lter)
cout << *lter << " ";
cout << endl;

return 0;
}

D:\Program Files\Microsoft Visual Studio\MyProjects\page375_5\page375_5.cpp(12) : error C2664: 'void __thiscall std::list<int,class std::allocator<int> >::splice(class std::list<int,class std::allocator<int> >::iterator,class std::list<int,class std
::allocator<int> > &,class std::list<int,class std::allocator<int> >::iterator)' : cannot convert parameter 2 from 'class std::list<int,class std::allocator<int> >::iterator' to 'class std::list<int,class std::allocator<int> > &'
A reference that is not to 'const' cannot be bound to a non-lvalue
Error executing cl.exe.

page375_5.exe - 1 error(s), 0 warning(s)
...全文
317 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ysysbaobei 2008-11-21
  • 打赏
  • 举报
回复
可能书本上写错了:
lst.splice(iter, beg, end);

应该是2楼、6楼说的:
splice ( iterator position, list <T,Allocator>& x, iterator first, iterator last );

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

int main() // splice(iterator position, list <T,Allocator>& x, iterator first, iterator last);
{
int array[] = {1, 2, 3, 4, 5, 6, 7, 8};
list<int> ilist(array, array + 8);
list<int>::iterator lter = ilist.begin(), beg = ilist.begin(), end = ilist.end();
++lter; // lter 指向 2
++beg;
++beg; // beg 指向 3
--end;
--end;
--end; // end 指向 6,由于左闭合性,故 beg-end 包含:3 4 5
ilist.splice(lter, ilist, beg, end); // 把 beg与end间的元素,放到lter的前面
for (lter = ilist.begin(); lter != ilist.end(); ++lter)
cout << *lter << " "; // 1 3 4 5 2 6 7 8
cout << endl;

return 0;
}

问题解决了,谢谢大家,结贴了
ysysbaobei 2008-11-21
  • 打赏
  • 举报
回复
lst.splice(iter, beg, end);

第3个版本移动迭代器beg和end标记范围内的元素。beg和end必须指定一个有效的范围。这两个迭代器可标记任意list对象内的范围,包括lst。当它们指定lst的一段范围是,如果iter也指向这个范围内的一个元素,则该运算未定义。
ysysbaobei 2008-11-21
  • 打赏
  • 举报
回复
是c++ primer 书本里的
讲解list容器特有的操作的时候,列出了下面3个:
lst.splice(iter, lst2)
lst.splice(iter, lst2, iter2)
lst.splice(iter, beg, end)-----在练习这个的时候,出的问题
liumingrong 2008-11-21
  • 打赏
  • 举报
回复
呵呵,你想干嘛呢,把ilist清空?
[Quote=引用 7 楼 ztz0223 的回复:]
你对函数的理解有错误:
修改如下:

C/C++ code#include <iostream>
#include <list>
using namespace std;

int main()
{
int array[] = {1, 2, 3, 4, 5, 6, 7, 8};
list<int> ilist(array, array + 8);
list<int> ilist2;
list<int>::iterator lter = ilist.begin(), beg = ilist.begin(), end = ilist.end();
++beg;
ilist.splice(lter, ilist2, end);
for (lter = ilist.begi…
[/Quote]
就呆在云上 2008-11-21
  • 打赏
  • 举报
回复
你对函数的理解有错误:
修改如下:
#include <iostream>
#include <list>
using namespace std;

int main()
{
int array[] = {1, 2, 3, 4, 5, 6, 7, 8};
list<int> ilist(array, array + 8);
list<int> ilist2;
list<int>::iterator lter = ilist.begin(), beg = ilist.begin(), end = ilist.end();
++beg;
ilist.splice(lter, ilist2, end);
for (lter = ilist.begin(); lter != ilist.end(); ++lter)
cout << *lter << " ";
cout << endl;

return 0;
}
Longinc 2008-11-21
  • 打赏
  • 举报
回复
splice(iterator it , list &x)

splice(iterator it, list &x, iterator first)

splice(iterator it,list &x, iterator first, iterator last)
http://blog.sina.com.cn/s/blog_4ce0688901008ekj.html
sffofn 2008-11-21
  • 打赏
  • 举报
回复
void splice(iterator it, list& x, iterator first);
中间的参数不是iterator,而是list, 你把中间的换成ilist就可以运行成功了
liumingrong 2008-11-21
  • 打赏
  • 举报
回复
例如你可以这样
ilist.splice(lter, ilist, beg, end);
WingForce 2008-11-21
  • 打赏
  • 举报
回复
用stl就不要用vc6
liumingrong 2008-11-21
  • 打赏
  • 举报
回复
void splice ( iterator position, list<T,Allocator>& x );
void splice ( iterator position, list<T,Allocator>& x, iterator i );
void splice ( iterator position, list<T,Allocator>& x, iterator first, iterator last );
第二个参数是一个list对象,从它的某些位置挪动元素到调用者的position处
ysysbaobei 2008-11-21
  • 打赏
  • 举报
回复
page375_5.cpp (p361/375)
编译环境是 vc6.0

65,211

社区成员

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

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