免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3877 | 回复: 6
打印 上一主题 下一主题

[C++] stl::map的一个问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-31 16:17 |只看该作者 |倒序浏览
#include <map>
#include <string>
using namespace std;

int main()
{
    map<string, string> _map;
    const map<string, string>& _map1 = _map;
//    map<string,string>::iterator iter = _map.begin();  // 这个可以
    map<string,string>::iterator iter = _map1.begin(); // 这个报错
}


iter 赋值的两行上面的能编译通过,下面那行编译报错(分别用g++和sun CC编译器),
>g++ ref.cpp
ref.cpp: In function `int main()':
ref.cpp:10: error: conversion from `std::_Rb_tree_const_iterator<std::pair<const std::string, std::string> >' to non-scalar type `std::_Rb_tree_iterator<std::pair<const std::string, std::string> >' requested

>CC ref.cpp
"ref.cpp", line 10: Error: Cannot use __rwstd::__rb_tree<std::string, std::pair<const std::string, std::string>, __rwstd::__select1st<std::pair<const std::string, std::string>, std::string>, std::less<std::string>, std::allocator<std::pair<const std::string, std::string>>>::const_iterator to initialize __rwstd::__rb_tree<std::string, std::pair<const std::string, std::string>, __rwstd::__select1st<std::pair<const std::string, std::string>, std::string>, std::less<std::string>, std::allocator<std::pair<const std::string, std::string>>>::iterator.
1 Error(s) detected.

论坛徽章:
0
2 [报告]
发表于 2009-03-31 16:20 |只看该作者
改成:map<string,string>::const_iterator iter = _map1.begin();

论坛徽章:
0
3 [报告]
发表于 2009-03-31 16:21 |只看该作者
const map<string, string>& _map1 = _map;


const  map<string,string>::iterator iter = _map1.begin();

注意const关键字

论坛徽章:
0
4 [报告]
发表于 2009-03-31 16:22 |只看该作者

回复 #1 zhongyj 的帖子

const 修饰了以后就不能用普通的iter了

[ 本帖最后由 yatusiter 于 2009-3-31 16:23 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2009-03-31 16:23 |只看该作者

回复 #2 bidongliang 的帖子

这个可以了,能解释下什么原因吗,对这些模板看着头晕

论坛徽章:
0
6 [报告]
发表于 2009-03-31 16:25 |只看该作者

回复 #3 alibase_xg 的帖子

这个不行

论坛徽章:
0
7 [报告]
发表于 2009-03-31 16:25 |只看该作者
原因就是: conversion from `std::_Rb_tree_const_iterator<std::pair<const std::string, std::string> >' to non-scalar type `std::_Rb_tree_iterator<std::pair<const std::string, std::string> >' requested
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP