免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: andyY
打印 上一主题 下一主题

新手:iterator的小问题 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2006-05-26 16:55 |只看该作者
我在虚拟机上,所以代码和编译输出,是现敲的,漏掉了,后面的也可能有敲错,包涵:)

“改成(*it)->l.push_back(100);试试看 ”
错误: base operand of '->' has non-pointer type 'const mc'

改成(*it).l.push_back(100);
错误: passing 'const std::list<int, std::allocator<int> >' as 'this' argument of 'void std::list<_Tp, _Alloc>::push_back(const _Tp&) [wich _Tp=int, _Alloc = std::allocator<int>]' discard qualifiers

lenovo说得对,应该是 const 的问题。难道只能现删除,再插入新的, 有没有其它做法呢?

我还试过  const_cast< set<mc>::iterator > ,也不行。呵呵,C++刚学,这么做是不是有点幼稚。。。

论坛徽章:
0
12 [报告]
发表于 2006-05-26 16:58 |只看该作者
你按照6楼说的改就是了

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
13 [报告]
发表于 2006-05-26 16:59 |只看该作者
原帖由 gooderfeng 于 2006-5-26 16:55 发表
我认为返回的是一个const list,
所以不能修改这个list。

我调试以下确实是返回const list
但是不知道原因。
斑竹帮忙找找哪个帖子啊。

我还想让你帮我找呢。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
14 [报告]
发表于 2006-05-26 17:07 |只看该作者
原帖由 andyY 于 2006-5-26 16:55 发表
我在虚拟机上,所以代码和编译输出,是现敲的,漏掉了,后面的也可能有敲错,包涵:)

“改成(*it)->l.push_back(100);试试看 ”
错误: base operand of '->' has non-pointer type 'const mc'

改 ...

看看这个吧。
http://stl.winterxy.com/html/item_22.html

论坛徽章:
0
15 [报告]
发表于 2006-05-26 17:13 |只看该作者
现在找不了。
是不是重载 ==的时候是常量,造成返回的是一个只读的区域

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
16 [报告]
发表于 2006-05-26 17:40 |只看该作者
原帖由 gooderfeng 于 2006-5-26 17:13 发表
现在找不了。
是不是重载 ==的时候是常量,造成返回的是一个只读的区域

我手动翻了几十页,终于找到了。
http://bbs.chinaunix.net/viewthr ... amp;extra=page%3D35

论坛徽章:
0
17 [报告]
发表于 2006-05-26 17:41 |只看该作者
原帖由 lenovo 于 2006-5-26 17:07 发表

看看这个吧。
http://stl.winterxy.com/html/item_22.html


多谢!以前看的时候只读了前半部分 :)

现在

  1. it->l.push_back(100);  
复制代码

改成转化到引用,修改不影响关联顺序的部分,编译通过了。
  1. const_cast<MC&>(*it).l.push_back(100);
复制代码



又有个新问题了:
  1.   //debug
  2.   std::cout<<"----- debug -----\n"
  3.   it = find( mcset.begin(),  mcset.end(), 3); //set<mc>::iterator it
  4.   list<int>& nl = it->l;

  5.   list<int>::iterator nit;
  6.   for( nit=nl.begin(); nit!=nl.end(); nit ++)
  7.      std::cout << *nit << std::endl;
复制代码


编译错误:
  conversion from 'const std::list<int, std::allocator<int> >' to 'std::list<int, std::allocator<int> >&' discard qualifiers

如果:
  1.   //debug
  2.   std::cout<<"----- debug -----\n"
  3.   it = find( mcset.begin(),  mcset.end(), 3); //set<mc>::iterator it

  4.   list<int>::iterator nit;
  5.   for( nit=it->l.begin(); nit!=it->l.end(); nit ++)
  6.      std::cout << *nit << std::endl;
复制代码

编译错误:
  stest.cpp: In function `int main()':
stest.cpp:54: no match for `std::_List_iterator<int, int&, int*>& =
   std::_List_iterator<int, const int&, const int*>' operator
/usr/include/c++/3.2.2/bits/stl_list.h:115: candidates are:
   std::_List_iterator<int, int&, int*>& std::_List_iterator<int, int&,
   int*>:perator=(const std::_List_iterator<int, int&, int*>&

[ 本帖最后由 andyY 于 2006-5-26 17:43 编辑 ]

论坛徽章:
0
18 [报告]
发表于 2006-05-26 17:48 |只看该作者

改了下debug

  1.   //debug
  2.   std::cout<<"--------debug------\n";
  3.   it = find( mcset.begin(), mcset.end(), 3);
  4.   list<int> nl = it->l;  //拷贝出来

  5.   list<int>::iterator nit;
  6.   for( nit = nl.begin(); nit != nl.end(); nit ++)
  7.     std::cout<< *nit << std::endl;
复制代码


打印看来,push_back成功了。:)
不过为什么
  1.   list<int>& nl = it->l;  
复制代码


  1.   for( nit = it->l.begin(); nit != it->l.end(); nit ++)
复制代码

都不行呢?

论坛徽章:
0
19 [报告]
发表于 2006-05-26 17:55 |只看该作者
原帖由 lenovo 于 2006-5-26 17:40 发表

我手动翻了几十页,终于找到了。
http://bbs.chinaunix.net/viewthr ... amp;extra=page%3D35

版主辛苦了。
effective stl 对这个问题讨论得很详细,以前看得时候怎么就囫囵吞枣呢

论坛徽章:
0
20 [报告]
发表于 2006-05-26 18:07 |只看该作者
可以了。为什么find也要这要呢?
  1.   std::cout<<"--------debug------\n";
  2.   it = find( mcset.begin(), mcset.end(), 3);
  3.   for( list<int>::iterator nit = const_cast<mc&>(*it).l.begin();
  4.          nit != const_cast<mc&>(*it).l.end();
  5.          nit ++)
  6.     std::cout<< *nit << std::endl;
复制代码

template<class InputIterator, class EqualityComparable>
InputIterator find(InputIterator first, InputIterator last, const EqualityComparable& value);

[ 本帖最后由 andyY 于 2006-5-26 18:09 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP