这个哪里出错了?

haolly 2011-09-20 12:09:00
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> snum;
int count;
cout<<"Please enter a number(Ctrl+Z to the end):";
while (cin>>count)
snum.push_back(count);
int *pt= new int[snum.size()];
int *pt2=pt;
for(vector<int>::iterator i=snum.begin();i!=snum.end();++i,++pt)
*pt2=*i;
delete [] pt;
for(int a=0;a!=sizeof(pt2)/sizeof(int);a++)
cout<<pt2[a];
return 0;

}

输入1 2 3,运行后只出现3,哪里错了?
...全文
121 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
haolly 2011-09-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 kf44444 的回复:]

#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> snum;
int count;
cout<<"Please enter a number(Ctrl+Z to the end):";
while (cin>>count)
s……
[/Quote]
原来还可以这样。谢谢
rendao0563 2011-09-20
  • 打赏
  • 举报
回复
sizeof 测的是类型的大小. int *pt2 是指针.
haolly 2011-09-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cppresearcher 的回复:]

sizeof(ptr2)的值与sizeof(int)的值一样。
[/Quote]
你的意思是说改为sizeof(pt2)就可以了?
cppresearcher 2011-09-20
  • 打赏
  • 举报
回复
sizeof(ptr2)的值与sizeof(int)的值一样。
淡然一笑 2011-09-20
  • 打赏
  • 举报
回复
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> snum;
int count;
cout<<"Please enter a number(Ctrl+Z to the end):";
while (cin>>count)
snum.push_back(count);
int *pt= new int[snum.size()];
int j=0;
for(vector<int>::iterator i=snum.begin();i!=snum.end();++i,++j)
{
pt[j]=*i;
cout<<pt[j]<<",";
}
delete [] pt;
return 0;
}

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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