我有一个像这样的配对 vector
vector <pair<int , string> > Names;
我是这样放数据的:
cin>>taxi>>Ar_taxis>>Ar_mathiton;
for(j=0;j<Ar_mathiton;j++)
{
cin>>Ar_Mitroou>>Onoma;
Names.push_back(make_pair(Ar_Mitroou,Onoma));
}
我对它进行排序然后打印它:
for(j=0;j<Ar_mathiton;j++)
{
cout<<Names[i].first<<" "<<Names[i].second<<endl;
Names.pop_back();
}
我的 pop_back() 有问题,它没有删除 pair 的集合。我不知道是否还有其他命令可以执行此操作。谢谢。
[编辑]整个代码
cin>>Ar_taxeon;
for(i=0;i<Ar_taxeon;i++)
{
cin>>taxi>>Ar_taxis>>Ar_mathiton;
for(j=0;j<Ar_mathiton;j++)
{
cin>>Ar_Mitroou>>Onoma;
Names.push_back(make_pair(Ar_Mitroou,Onoma));
}
sort(Names.begin(),Names.end());
cout<<taxi<<Ar_taxis<<endl;
for(j=0;j<Ar_mathiton;j++)
{
cout<<Names[i].first<<" "<<Names[i].second<<endl;
Names.pop_back();
}
}
最佳答案
考虑以下更改:
将循环内变量i
的名称更改为j
你可以在 cout 之后调用 Names.clear(),而不是 Names.popBack():
所以你的最终代码将是:
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector <pair<int , string> > Names;
int Ar_mathiton,Ar_Mitroou;
string Onoma;
cin>>Ar_mathiton;
for(int j=0;j<Ar_mathiton;j++)
{
cin>>Ar_Mitroou>>Onoma;
Names.push_back(make_pair(Ar_Mitroou,Onoma));
}
for(int j=0;j<Ar_mathiton;j++)
{
cout<<Names[j].first<<" "<<Names[j].second<<endl;
}
Names.clear();
return 0;
}
关于C++ : Pop_back a paired vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33308095/