简体   繁体   English

C ++:Pop_back配对向量

[英]C++ : Pop_back a paired vector

I have a paired vector like this 我有一个像这样的配对向量

 vector <pair<int , string> > Names;    

I put data in it this way: 我以这种方式放入数据:

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));

        }

I sort it and then i print it: 我对其进行排序,然后进行打印:

  for(j=0;j<Ar_mathiton;j++)
        {
            cout<<Names[i].first<<" "<<Names[i].second<<endl;
           Names.pop_back();
        }

There's a problem with my pop_back() , it doesn't delete the set of pair. 我的pop_back()有问题,它不会删除配对对。 I don't know if there's another command to do it. 我不知道是否还有其他命令可以执行此操作。 Thanks. 谢谢。

[edit] the whole code [编辑]整个代码

  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();
        }

}

Consider following changes: 考虑以下更改:

change name of variable i inside loop into j 将循环内的变量i名称更改为j

and you can call Names.clear() after cout, instead of Names.popBack(): 并且您可以在cout之后调用Names.clear(),而不是Names.popBack():

so your final code will be: 因此您的最终代码将是:

#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;
}

There's a variety of problems with the example you posted. 您发布的示例存在很多问题。

The first one is that you are iterating over the list using j , but accessing Names with the index of i . 第一个是使用j遍历列表,但是使用索引i访问Names However, even if you fixed that you would get a crash because you are iterating forward through the vector, but popping off the back , this means that you will eventually get an out-of-range error as the iterating index passes over the length of the list. 但是,即使您修复了崩溃问题,因为您正在向量中向前迭代但又从后面弹出时,这也意味着您最终将在迭代索引超过整个长度时得到超出范围的错误。名单。

If you want to remove all items in one go, you should just call Names.clear() . 如果要一次性删除所有项目,则只需调用Names.clear()

Additionally, it's bad form to be using C++ and not using the C++ way of iterating through lists. 另外,使用C ++而不使用C ++遍历列表的方式是一种不好的形式。

Consider changing your second loop to the following: 考虑将第二个循环更改为以下内容:

for (auto& name : Names)
{
    cout << Names[j].first << " " << Names[j].second << endl;
}

Then finally, call this: 最后,调用此命令:

Names.clear();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM