#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<pair<int,int> > arr;
arr[0].first=20,arr[0].second=1;
arr[1].first=3,arr[1].second=2;
arr[2].first=230,arr[2].second=3;
arr[3].first=230,arr[3].second=4;
arr[4].first=202,arr[4].second=5;
arr[5].first=-20,arr[5].second=6;
sort(arr.begin(),arr.end());
vector<pair<int,int> >::iterator it;
for(it=arr.begin();it!=arr.end();it++)
{
cout<<it->first<<it->second<<endl;
}
}
此程序運行不正常,背後有什麼可能的原因?
此外,我想有排序對矢量的排序完成的價值。這個程序崩潰了,爲什麼?
[ 'vector> arr(6);'](http://en.cppreference.com/w/cpp/container/vector/vector)應該解決這個問題。 –
請問每個問題有一個問題。 –
使用默認構造函數創建矢量時,矢量爲***空***。任何索引都會超出範圍並導致*未定義的行爲*。 –