C++ : Pop_back a paired vector

标签 c++ vector

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

相关文章:

c++ - 将 "int"添加到 "uint_64t"

c++如何通过复制将结构 vector 复制到另一个结构 vector 中

c++ - 如何取消引用存储在智能指针 vector 中的派生类对象?

c++ - 范围结束后程序崩溃

c++ - Winsock 单客户端服务器同时发送和接收

c++ - C/C++ : print contents of (config) header file

c++ - Linux C++ ._MyFirst ._MyLast vector 等效

c++ - 释放存储在 vector 中的对象?

c++ - D8048 : cannot compile C file 'openssl\applink.c' with/ZW option

c++ - 最长回文子串递归解