【发布时间】:2016-08-28 12:34:03
【问题描述】:
#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;
}
}
此程序运行不正常,这可能是什么原因?
我也想有排序的对向量,其中排序是由值完成的。
【问题讨论】:
-
每个问题一个问题。
-
使用默认构造函数创建向量时,向量为empty。任何索引都将超出范围并导致未定义的行为。