下面的程序在调试时执行到return 0;的时候突然死掉了,出现错误的语句是编译器内部的
deallocate(pointer__p,size_type){::operatordelete(__p);}这句话出现了问题,求教各位高手#include<iostr...
deallocate(pointer __p, size_type)
{ ::operator delete(__p); }这句话出现了问题,求教各位高手
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main ()
{
string str[]={"Alex","John","Robert"};
vector<int> v2(10);
cout << "v2:" << endl << v2[0] << endl;
vector<int> v1;
v1.push_back(3);
v1[1] = 4;
v1[2] = 5;
cout << "v1:" << endl;
for(int i = 0; i < 3; ++i)
cout << v1[i] << " ";
cout << endl;
vector<int> v3(10,0);
cout << "v3:" << endl;
for(int i = 0; i < 10; ++i)
cout << v3[i] << " ";
cout << endl;
vector<string> v4(str+0,str+3);
vector<string>::iterator sIt = v4.begin();
while ( sIt != v4.end() )
cout << *sIt++ << " ";
cout << endl;
vector<string> v5(v4);
for ( int i=0; i<3; i++ )
cout << v5[i] << " ";
cout << endl;
return 0;
} 展开
{ ::operator delete(__p); }这句话出现了问题,求教各位高手
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main ()
{
string str[]={"Alex","John","Robert"};
vector<int> v2(10);
cout << "v2:" << endl << v2[0] << endl;
vector<int> v1;
v1.push_back(3);
v1[1] = 4;
v1[2] = 5;
cout << "v1:" << endl;
for(int i = 0; i < 3; ++i)
cout << v1[i] << " ";
cout << endl;
vector<int> v3(10,0);
cout << "v3:" << endl;
for(int i = 0; i < 10; ++i)
cout << v3[i] << " ";
cout << endl;
vector<string> v4(str+0,str+3);
vector<string>::iterator sIt = v4.begin();
while ( sIt != v4.end() )
cout << *sIt++ << " ";
cout << endl;
vector<string> v5(v4);
for ( int i=0; i<3; i++ )
cout << v5[i] << " ";
cout << endl;
return 0;
} 展开
1个回答
展开全部
vector<int> v1;
v1.push_back(3); // V1 只压入力一个元素 3,所以V1只存在V[0]这个元素
v1[1] = 4; // 问题语句:赋值只能对已经存在的元素实行,V[1]实际不存在。
v1[2] = 5; // 问题语句:同上
表面上后面两条赋值语句执行成功,实际上所修改的内存已经越界。所以在函数结束时,进行vector的析构发生问题。
要想正确执行要再加入两句:
vector<int> v1;
v1.push_back(3);
v1.push_back(0);
v1.push_back(0);
v1[1] = 4;
v1[2] = 5;
v1.push_back(3); // V1 只压入力一个元素 3,所以V1只存在V[0]这个元素
v1[1] = 4; // 问题语句:赋值只能对已经存在的元素实行,V[1]实际不存在。
v1[2] = 5; // 问题语句:同上
表面上后面两条赋值语句执行成功,实际上所修改的内存已经越界。所以在函数结束时,进行vector的析构发生问题。
要想正确执行要再加入两句:
vector<int> v1;
v1.push_back(3);
v1.push_back(0);
v1.push_back(0);
v1[1] = 4;
v1[2] = 5;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询