免费注册 查看新帖 |

Chinaunix

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

为何两种迭代方式,一种可以,另一种报错? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-06-18 12:00 |只看该作者 |倒序浏览
代码:
#include <map>;
#include <iterator>;
#include <algorithm>;
#include <string>;
using namespace std;

int main()
{
        typedef map<string,float>; string_float_map;
        string_float_map stocks;

        stocks["basf"]=369.50;
        stocks["vw"]=413.50;
        stocks["daimer"]=819.00;
        stocks["bmn"]=834.00;
        stocks["simens"]=842.20;

        string_float_map::const_iterator iter=stocks.begin();
        string_float_map::const_iterator iter_end=stocks.end();
        for ( ; iter !=iter_end ; ++iter )
        {
                cout << "stock " << iter->;first << "\t" << "price " << iter->;second << endl ;
        }
        cout << endl;

        string_float_map::const_iterator pos;
        for ( pos=stocks.begin(); pos !=stocks.end; ++pos )
        {
                cout << "stock " << pos->;first << "\t" << "price " << pos->;second <<endl; }
        cout << endl;
}
报错信息:[gag@genomic-server cpp]$ c++ -o map1.out map1.cpp
map1.cpp: In function `int main()':
map1.cpp:28: no match for `std::_Rb_tree_iterator<std::pair<const std::string,
   float>;, const std::pair<const std::string, float>;&, const std::pair<const
   std::string, float>;*>;& != <unknown type>;' operator

请问这是怎么回事?

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2004-06-18 12:05 |只看该作者

为何两种迭代方式,一种可以,另一种报错?

for ( pos=stocks.begin(); pos !=stocks.end; ++pos )

是不是stocks.end少了一对括号呀?
以后贴代码请用code功能,
这样看着真难受,没几个愿意给你看。

论坛徽章:
0
3 [报告]
发表于 2004-06-18 17:49 |只看该作者

为何两种迭代方式,一种可以,另一种报错?

请问如何用code功能?

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
4 [报告]
发表于 2004-06-18 18:05 |只看该作者

为何两种迭代方式,一种可以,另一种报错?

在你的代码前面点击code按钮,
后面再点击一次就可以了。
不要说你找不到这个按钮。

论坛徽章:
0
5 [报告]
发表于 2004-06-18 21:30 |只看该作者

为何两种迭代方式,一种可以,另一种报错?

2 errors after compiling your code:

1. #include <iostream>;
2. for ( pos=stocks.begin(); pos !=stocks.end; ++pos )

    should be 'stocks.end()'

    for ( pos=stocks.begin(); pos !=stocks.end(); ++pos )

After you fix these two errors, everything will be fine.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP