下面这个代码有问题

femalelover 2008-08-19 11:12:11
list<int*> stInt;
int* pInt = stInt.begin(); //这个stInt为空.

我个人觉得, 如果用SGI STL, 从它的源码来看, 应该不会有问题.

但现在我的代码在VC6中跑N次之后, 出问题了, 而VC6的源码比较难看.
...全文
279 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
美丽海洋 2008-08-21
  • 打赏
  • 举报
回复
int *pInt = *stInt.begin() ;
JackyRao 2008-08-21
  • 打赏
  • 举报
回复
int pInt = *strInt.begin(); 才对, strInt.begin 函数返回iterator.
taodm 2008-08-21
  • 打赏
  • 举报
回复
搞个VC2005编译试试吧。别用vc6糟蹋生命了。
[Quote=引用 21 楼 femalelover 的回复:]
晕死, 这回是粘错了.

上面打错了, 实际代码是这样写的:

list <int*> stInt;
int* pInt = *stInt.begin(); //这个stInt为空
[/Quote]
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wangdeqie 的回复:]
C/C++ code
//这么用
#include <iostream>
#include <vector>
#include <list>
using namespace std;

void main()
{

list <int*> stInt;
list<int*>::iterator i= stInt.begin(); //这个stInt为空.
}
[/Quote]

___________________________________________________________________________________
欢迎各位朋友加入,一起来学习DirectX!!!
加该群之前请自行下载DX7,DX8,DX9的相关实例代码.
Directx群号:2502856 加群验证号:D
API群号:2502896 加群验证号:A

一起学习,创造神话!
ymntomyimi8855 2008-08-21
  • 打赏
  • 举报
回复
楼主可以改成这样
list <int*> stInt;
int* * pInt = stInt.begin();
jia_xiaoxin 2008-08-20
  • 打赏
  • 举报
回复

应该这样写吧
list <int*> stInt;
int * pInt = * stInt.begin();
ysuliu 2008-08-20
  • 打赏
  • 举报
回复
你为什么要把一个迭代器直接赋给一个指针呢,虽然迭代器本身就是指针,可是还是应该按迭代器的用法。。
taodm 2008-08-20
  • 打赏
  • 举报
回复
楼主代码真的编译过?
另外,用stl就认真学stl的正规使用方法,别懒。
找本《STL.Tutorial.and.Reference.Guide》《effective stl》好好学。
wjb_yd 2008-08-20
  • 打赏
  • 举报
回复
貌似list默认初始化的时候,first是随机的,但是vector就不一样了,它默认的时候first是0.

vector <int> stInt;
int pInt = *stInt.begin() ;

这样直接运行出错,访问0x00000000了
herman~~ 2008-08-20
  • 打赏
  • 举报
回复
不明白lz这样写的用意是什么
wuwenjie0506 2008-08-20
  • 打赏
  • 举报
回复
list <int*> stInt;
int* pInt = stInt.begin();
太天才了,居然能写出这样的代码!
用叠代器iterator是来访问聚合物里的元素的.
应写为list<int*>::iterator it = stInt.begin();
int* pInt = *it;
不要写这样晦涩的代码,自己不容易懂,错了也不知道为什么!
也可写为typedef int* POINT_INT;
list<POINT_INT> stInt;
list<POINT_INT>::iterator it = stInt.begin();
POINT_INT pInt = *it;
这样就清晰多了!
还要问一句,为什么要把指针作为元素放到list里,这样很危险!
yshuise 2008-08-20
  • 打赏
  • 举报
回复
对vector我已经用了N次,不应该再怀疑了。
yshuise 2008-08-20
  • 打赏
  • 举报
回复
list的迭代器是一个类,需要对*操作符进行重载,而求出其值。
sidaeren 2008-08-20
  • 打赏
  • 举报
回复
#include<iostream>
#include<list>
using namespace std;

int main()
{
list<int*> strInt;
int *a = *strInt.begin();
return 0;
}

上面的代码在vc6.0中编译和执行都能通过,在vs2005中编译和链接没问题,执行时报错: list iterator not dereferencable.
意思是:list的iterator是不可解引用的。这时因为:你所定义的list的元素类别是:int*(即指针),而strInt.begin()返回
的是元素的迭代器,而迭代器的功能就像指针,它是指向容器中元素的。你只有对迭代器解引用才能获得元素(在你的代码中,元素类型是int*),所以才有:int *a=*strInt.begin(); 之所以执行出现错误,是因为strInt是空容器,它的beging()是等于end()的,迭代器实现很有可能是:typedef list<value_type>::value_type* iterator;或者是将迭代器实现为在其内部维护一个指向元素的指针的类。无论哪种实现,迭代器接口都是一样的。不妨假设是第一种实现,那么迭代器类型就是:int**。由于容器为空,所以begin()和end()相等,不指向任何元素,也即迭代器类内部指针被赋值为null,在上面假设下就是:
int **_begin=null;所以*begin()的使用就是在执行*null,显然这是会导致执行期异常。vc6执行不报错,有可能是stl实现版本问题,它对标准支持不好。
taojian_hhu 2008-08-20
  • 打赏
  • 举报
回复
stInt.begin()是一个迭代器,我们可以将其看做是一个指针,二从你的List的定义来看,其元素的类型为int*
所以stInt.begin()的一个指向int*的指针,即int**,所以应该为int** pInt = stInt.begin();
wangdeqie 2008-08-20
  • 打赏
  • 举报
回复

//这么用
#include <iostream>
#include <vector>
#include <list>
using namespace std;

void main()
{

list <int*> stInt;
list<int*>::iterator i= stInt.begin(); //这个stInt为空.
}
yshuise 2008-08-20
  • 打赏
  • 举报
回复
int* pInt = &(*stInt.begin());//用来中文的小括弧,应该是int* pInt = &(*stInt.begin());
sansky99 2008-08-20
  • 打赏
  • 举报
回复
pInt = *(stInt.begin());
yshuise 2008-08-20
  • 打赏
  • 举报
回复
list <int*> stInt;
int* pInt = stInt.begin(); //这个stInt为空.

我个人觉得, 如果用SGI STL, 从它的源码来看, 应该不会有问题.

但现在我的代码在VC6中跑N次之后, 出问题了, 而VC6的源码比较难看.
======================
你的代码应该是错误的。
第二行应该是:int* pInt = &(*stInt.begin());这样才是符合标准,如果你那样写
在vc2003上编译都通不过。从道理上讲,vector可以那样写,但是仍通不过编译器。而
list绝对是个错误。vc6.0不好使,不支持标准。
jay的Fans 2008-08-20
  • 打赏
  • 举报
回复
int* pInt = NULL;
if (!stInt.empty())
{
pInt = stInt.begin();
}

另外不要用VC6.0调试这个了。
加载更多回复(8)

64,282

社区成员

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

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