用devcpp的请进,有问题请教
顺序容器vector 类中的at()方法为什么不能用??try.....catch也不能用,大家的怎么样??
这是编译器问题吗??想问问大家使用的时候可以吗?还是只是我的系统问题。代码如下:
#include<iostream>
#include<vector>
#include<conio.h>
#include<exception>
#include<algorithm>
using namespace std;
int main()
{ const int size=6;
int a[size]={1,2,3,4,5,6};
vector<int> v(a,a+size);
ostream_iterator<int> output(cout," ");
cout<<"Vector v contais:";
copy(v.begin(),v.end(),output);
cout<<"\nFirst element of v:"<<v.front();
cout<<"\nLast element of v:"<<v.back();
v[0]=7;
v[1]=10;
v.insert(v.begin()+1,22);
cout<<"\nContents of v after changed:";
copy(v.begin(),v.end(),output);
try
{ v.at(100)=77;
}
catch(out_of_range e)
{ cout<<"\nException:"<<e.what();
}
v.erase(v.begin());
cout<<"\nContents of v after erase is:";
copy(v.begin(),v.end(),output);
v.erase(v.begin(),v.end());
cout<<"\nAfter earse,vector v:"
<<(v.empty()?"is":"is not")<<" empty."<<endl;
v.insert(v.begin(),a,a+size);
cout<<"Contents before clear:";
copy(v.begin(),v.end(),output);
v.clear();
cout<<"\nAfter clear,vector v "
<<(v.empty()?"is":"is not")<<" empty."<<endl;
getch();
return 0;
}
出错信息如下
no matching function for call to `vector<int,allocator<int> >::at (int)'
parse error before `e'
confused by earlier errors, bailing out
这是《c++大学教程》里面的一个例子