gpt4 book ai didi

C++ : Pop_back a paired vector

转载 作者:行者123 更新时间:2023-11-28 06:00:54 24 4
gpt4 key购买 nike

我有一个像这样的配对 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com