STL中vector的问题

chenzhiwei021 2011-03-09 11:59:00
#include <iostream>
#include <vector>

using namespace std;

void main()
{
std::vector<int> m_vec;
m_vec.push_back(0);
m_vec.push_back(1);
for(int index = 0, std::vector<int>::iterator it = m_vec.begin(); it != m_vec.end(); ++it,++index)
{
cout<<index<<endl;
}
}

编译后出现一下错误,貌似没有重载!=操作



1>------ 已启动生成: 项目: Test, 配置: Debug Win32 ------
1>正在编译...
1>Test.cpp
1>f:\test\test\test\test.cpp(11) : error C2146: 语法错误 : 缺少“,”(在标识符“it”的前面)
1>f:\test\test\test\test.cpp(11) : error C2350: “std::vector<_Ty>::iterator”不是静态成员
1> with
1> [
1> _Ty=int
1> ]
1>f:\test\test\test\test.cpp(11) : error C2440: “初始化”: 无法从“std::_Vector_iterator<_Ty,_Alloc>”转换为“int”
1> with
1> [
1> _Ty=int,
1> _Alloc=std::allocator<int>
1> ]
1> 没有可用于执行该转换的用户定义的转换运算符,或者无法调用该运算符
1>f:\test\test\test\test.cpp(11) : error C2784: “bool std::operator !=(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)”: 无法从“int”为“const std::vector<_Ty,_Alloc> &”推导 模板 参数
1> d:\学习软件\visual studio 2005\vc\include\vector(1268) : 参见“std::operator !=”的声明
1>f:\test\test\test\test.cpp(11) : error C2784: “bool std::operator !=(const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &,const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &)”: 无法从“int”为“const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &”推导 模板 参数
1> d:\学习软件\visual studio 2005\vc\include\iterator(277) : 参见“std::operator !=”的声明
1>f:\test\test\test\test.cpp(11) : error C2784: “bool std::operator !=(const std::allocator<_Ty> &,const std::allocator<_Other> &) throw()”: 无法从“int”为“const std::allocator<_Ty> &”推导 模板 参数
1> d:\学习软件\visual studio 2005\vc\include\xmemory(181) : 参见“std::operator !=”的声明
1>f:\test\test\test\test.cpp(11) : error C2784: “bool std::operator !=(const std::istreambuf_iterator<_Elem,_Traits> &,const std::istreambuf_iterator<_Elem,_Traits> &)”: 无法从“int”为“const std::istreambuf_iterator<_Elem,_Traits> &”推导 模板 参数
1> d:\学习软件\visual studio 2005\vc\include\xutility(2190) : 参见“std::operator !=”的声明
1>f:\test\test\test\test.cpp(11) : error C2784: “bool std::operator !=(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)”: 无法从“int”为“const std::reverse_iterator<_RanIt> &”推导 模板 参数
1> d:\学习软件\visual studio 2005\vc\include\xutility(1872) : 参见“std::operator !=”的声明
1>f:\test\test\test\test.cpp(11) : error C2784: “bool std::operator !=(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)”: 无法从“int”为“const std::pair<_Ty1,_Ty2> &”推导 模板 参数
1> d:\学习软件\visual studio 2005\vc\include\utility(68) : 参见“std::operator !=”的声明
1>f:\test\test\test\test.cpp(11) : error C2677: 二进制“!=”: 没有找到接受“std::_Vector_iterator<_Ty,_Alloc>”类型的全局运算符(或没有可接受的转换)
1> with
1> [
1> _Ty=int,
1> _Alloc=std::allocator<int>
1> ]
1>生成日志保存在“file://f:\Test\Test\Test\Debug\BuildLog.htm”
1>Test - 10 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========
...全文
169 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
libinfei8848 2011-03-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 yangsen600 的回复:]
for(int index = 0, std::vector<int>::iterator it = m_vec.begin(); it != m_vec.end(); ++it,++index)
逗号连接的应该是同一类型的数据。
[/Quote]
yes
千杯不醉-sen 2011-03-10
  • 打赏
  • 举报
回复
for(int index = 0, std::vector<int>::iterator it = m_vec.begin(); it != m_vec.end(); ++it,++index)
逗号连接的应该是同一类型的数据。
雪人2015 2011-03-10
  • 打赏
  • 举报
回复
void main()
{
std::vector<int> m_vec;
m_vec.push_back(0);
m_vec.push_back(1);
std::vector<int>::iterator it = m_vec.begin();
for(int index = 0;it != m_vec.end(); ++it,++index)
{
cout<<index<<endl;
}
}
千杯不醉-sen 2011-03-10
  • 打赏
  • 举报
回复

vector<int>::iterator it;
for(it = m_vec.begin(); it != m_vec.end(); ++it) //过长
{
cout << *it << endl;
}
千杯不醉-sen 2011-03-10
  • 打赏
  • 举报
回复

#include <iostream>
#include <vector>

using namespace std;

void main()
{
vector<int> m_vec;
m_vec.push_back(0);
m_vec.push_back(1);

/*
int index = 0;
for(vector<int>::iterator it = m_vec.begin(); it != m_vec.end(); ++it,++index)
{
cout<<index<<endl;
}
*/
for(vector<int>::iterator it = m_vec.begin(); it != m_vec.end(); ++it)
{
cout << *it << endl;
}
}
双杯献酒 2011-03-10
  • 打赏
  • 举报
回复
int index = 0, std::vector<int>::iterator it = m_vec.begin();

定义变量的语句可不能这样写.
王旺旺旺 2011-03-10
  • 打赏
  • 举报
回复
4楼正解.
我以前也遇到过这样的问题.
xiaoyao18301 2011-03-10
  • 打赏
  • 举报
回复
简单中的不平凡
yjjlyyj151 2011-03-10
  • 打赏
  • 举报
回复
index 改成*it

std::vector<int>::iterator it = m_vec.begin(); it != m_vec.end(); ++it
定义了一个迭代器,命名为it,it就是向量里的地址,*it就是值。
waxilo1988 2011-03-10
  • 打赏
  • 举报
回复
6楼 正解!!!!! 顶六楼
wei801516 2011-03-10
  • 打赏
  • 举报
回复

#include <iostream>
#include <vector>

using namespace std;

int main()
{
vector<int> m_vec;
m_vec.push_back(0);
m_vec.push_back(1);
for(vector<int>::iterator it = m_vec.begin(); it != m_vec.end(); ++it)
{
cout<< *it <<endl;
}

return 0;
}



这个样子看是不是更舒服一些呢?
碎碎念 2011-03-10
  • 打赏
  • 举报
回复
for(int index = 0, std::vector<int>::iterator it = m_vec.begin(); it != m_vec.end(); ++it,++index)
{
cout<<index<<endl;
}
for(这里只能放置一个副职语句;;)
改成
std::vector<int>::iterator it = m_vec.begin();
for(int index = 0\; it != m_vec.end(); ++it,++index)
{
cout<<index<<endl;
}

迭代器....以后可能还会用的...不需要只定义在for循环体内...
pengzhixi 2011-03-10
  • 打赏
  • 举报
回复
int index = 0, std::vector<int>::iterator it = m_vec.begin();你应该去看看逗号运算符了。
像你这样在逗号运算符里面定义两种不同的变量是不允许的。
heartgoon2010 2011-03-10
  • 打赏
  • 举报
回复
这样即可:
---------------------
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> m_vec;
m_vec.push_back(0);
m_vec.push_back(1);
int index;
vector<int>::iterator it;
for(index=0, it=m_vec.begin(); it!=m_vec.end(); it++,index++)
cout<<index<<endl;
return 0;
}
---------------------
for(int index = 0, std::vector<int>::iterator it = m_vec.begin();。。。)
中的逗号连接的不是表达式
shaotine_wu 2011-03-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 pengzhixi 的回复:]

int index = 0, std::vector<int>::iterator it = m_vec.begin();你应该去看看逗号运算符了。
像你这样在逗号运算符里面定义两种不同的变量是不允许的。
[/Quote]
++

64,652

社区成员

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

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