我用的是C++ VS2019
想排序个vector数组,运行后报了这个错
要具体源码评论区发
肯定要看源码啊,这就是数组越界访问啊
仅供参考
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<string>
#include<unordered_map>
using namespace std;
int main(){
//我们将学生信息存入map中
unordered_map<string,int>map;
string name;
int height;
for(int i=0;i<7;i++)
{
cin>>name>>height;
map[name]=height;
}
//将学生名字存入vector数组中
vector<string>res;
for(auto &[key,value]:map)
{
res.emplace_back(key);
}
//自定义排序,按身高降序,身高相同时则按名字升序排列
sort(res.begin(),res.end(),[&](const string&a,const string&b)->bool{
return map[a]==map[b]?a<b:map[a]>map[b];
});
//输出排列后的顺序
cout<<endl;
cout<<"after sorted: "<<endl;
for(int i=0;i<res.size();i++)
{
cout<<res[i]<<" "<<map[res[i]]<<endl;
}
system("pause");
return 0;
}
说了半天,代码呢?????