如何从向量中删除重复的? [英] How to remove duplicates from a vector?

查看:111
本文介绍了如何从向量中删除重复的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个程序,以下面的形式读取一个文本文件,将数据导入到一个向量,然后做一些计算。目前,我可以成对导入我的数据,将它们插入到一个向量和排序的向量..但我所有的努力失败,当它涉及到实际删除重复的,所以我可以使用该向量为其他目的。 / p>

I am writing a program that reads a text file in the form below, imports the data into a vector and then does some calculations with it. At the moment, I am able to import my data in pairs, insert them into a vector and sort the vector.. However all my efforts have failed when it comes to actually removing the duplicates so I can use the vector for other purposes.

1     4
5     6
4     5
4     5
5     4
6     7
...

这是目前我的相关代码。如果我对上面的矢量vec1.size()(只有6行),输出应该是5.然而,每个文本文件,我尝试,我得到一个输出,我不明白为什么。 p>

This is currently my relevant code right now. If I do vec1.size() on the vector above(only the 6 lines), the output should be 5. However, every text file i try, I get an output of 1, I don't understand why..

while( getline( fs1, instrng ) ) {

    istringstream s1(instrng);
    int a, b;
    s1 >> a >> b;
    pair<int,int> pair1 = make_pair(a,b);
    vec1.push_back( pair1 );

    sort( vec1.begin(), myvec1.end() );
            auto last = std::unique(vec1.begin(), vec1.end());
            vec1.erase(last, vec1.end());


推荐答案

看起来像是要使用 set 而不是向量

Seems like you want to use a set instead of a vector.

这篇关于如何从向量中删除重复的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆