编程论坛
注册
登录
编程论坛
→
C++教室
容器类操作
vfdff
发布于 2009-09-21 14:36, 555 次点击
定义了 vector<int> vec(100);
vector<int>::iterator p;后
p = vec.begin() 和 p = &vec[0] 有什么区别?
6 回复
#2
gz81
2009-09-21 16:11
问题点数:2
#3
gz81
2009-09-21 16:13
vector<int> vec(100);
vector<int>::iterator p;
p = &vec[0]; ---->这里编译不通过吧?好像要通过强制转换才行,如下:
#include <iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> ivec;
vector<int>::iterator it;
ivec.push_back(3);
it=(vector<int>::iterator)&ivec[0];
cout<<*it;
}
但当vector中没元素时,ivec.begin()是有意义的,&ivec[0]是没定义的使用它肯定报错。
vector<int> ivec;
if (ivec.begin()==ivec.end()) //正确
{
//...
};
[
本帖最后由 gz81 于 2009-9-21 16:24 编辑
]
#4
forclwy
2009-09-21 19:20
厉害
#5
sunkaidong
2009-09-21 20:19
建议看看设计模式-迭代器。里面讲的比较清楚。。。
#6
vfdff
2009-09-21 23:15
回复 3楼 gz81
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> ivec;
vector<int>::iterator it;
//ivec.push_back(3);
it=(vector<int>::iterator)&ivec[0];
it=ivec.begin();
//cout<<*it<<endl;
return 0;
}
刚才试过了,没有进行ivec.push_back(3);时 ivec;为空,it=(vector<int>::iterator)&ivec[0];操作扔来可以
#7
vfdff
2009-09-21 23:33
下午在外面没有编译器,现在用VC试过了,了解了!
1