注册 登录
编程论坛 C++教室

疯了 这怎么会出问题呢 简单容器迭代器问题

yijing21 发布于 2008-04-19 12:04, 823 次点击
#include <iostream>
#include <list>
#include <vector>
#include <string>
using namespace std;
int main()
{
    vector<string> ivec;
    ivec.push_back("hy");
    ivec.push_back("hy");
    ivec.push_back("hy");
    ivec.push_back("hy");
    for(vector<string>::iterator i=ivec.begin();i!=[bo]--[/bo]ivec.end();++i)
        cout<<*i;
    vector<string>::iterator it=ivec.begin();
    cout<<*it;
    return 0;
}

这里有3个错误
perfect.cpp(12) : error C2105: '--' needs l-value

error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::a
llocator<char> >' (or there is no acceptable conversion)

error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class std::a
llocator<char> >' (or there is no acceptable conversion)
首先是ivec.end()为什么不能自减呢  但是换成list容器就可以  自减不是常用容器都可以吗  还有下面那个解引用也不行  今天随便写了下  就发现这些问题  出乎意外  想问问大家

[[it] 本帖最后由 yijing21 于 2008-4-19 15:37 编辑 [/it]]
4 回复
#2
kaithwin2008-04-19 15:13
你没有包含string库
加上#include <string>应该就没有问题了
#3
yijing212008-04-19 15:38
string库忘了之前 但是加上去是一样的 里面好像涉及到左右值的问题
#4
sunkaidong2008-04-19 16:29
i!=--ivec.end()?没见过谁这样用过
#5
yijing212008-04-19 20:46
唉  我也是在用list容器输出式发现有一项多余项  然后用--vc.end()就可以去掉  但是换成vector才发现这个问题  vc.end()应该是右值 不能自减  但是为什么list容器却可以呢 是巧合还是?
1