主题:[讨论]new创建动态数组出现的问题
include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<int> ivec;
int dvec;
cout<<"Enter number:(ctrl+z to end)"<<endl;
while(cin>>dvec)
ivec.push_back(dvec);
int *tp = new int[ivec.size()];
//int *tp=cp1;//去掉这句编译通过,运行就会出错
for(vector<int>::iterator ix = ivec.begin();ix!=ivec.end();++ix,++tp)
{
*tp = *ix;
cout<<*tp<<" ";
}
delete [] tp;
system("pause");
return 0;
}
出现如下错误:Debug Assertion failed
Expression:_crtIsValidHeappointer(puserdata)
但是在定义一个中间变量int *tp=cp1;就正常运行[/size]
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<int> ivec;
int dvec;
cout<<"Enter number:(ctrl+z to end)"<<endl;
while(cin>>dvec)
ivec.push_back(dvec);
int *tp = new int[ivec.size()];
//int *tp=cp1;//去掉这句编译通过,运行就会出错
for(vector<int>::iterator ix = ivec.begin();ix!=ivec.end();++ix,++tp)
{
*tp = *ix;
cout<<*tp<<" ";
}
delete [] tp;
system("pause");
return 0;
}
出现如下错误:Debug Assertion failed
Expression:_crtIsValidHeappointer(puserdata)
但是在定义一个中间变量int *tp=cp1;就正常运行[/size]