我怎样才能找到一个载体重复的元素 [英] how can I find repeated elements in a vector

查看:105
本文介绍了我怎样才能找到一个载体重复的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个vector INT 其中可以包括最多4个元素和最小2,例如:

I have a vector of int which can include maximum 4 elements and minimum 2, for example :

std::vector<int> vectorDATA(X); // x means unknown here

我想要做的是消除重复例如,该元素:

What I want to do is to erase the elements that are repeated for example :

vectorDATA{1,2,2}     to vectorDATA{1,2}
vectorDATA{1,2,3}     to nothing changes
vectorDATA{2,2,2}     to vectorDATA{2}
vectorDATA{3,2,1,3}   to vectorDATA{3,2,1}
vectorDATA{1,2,1,2}   to vector{1,2} 

这里code简单:

cv::HoughLines(canny,lineQ,1,CV_PI/180,200);
        std::cout << " line Size "<<lineQ.size()<< std::endl;
        std::vector<int> linesData(lineQ.size());
        std::vector<int> ::iterator  it;
        if(lineQ.size() <=4 && lineQ.size() !=0 ){
            if(lineQ.size()==1){
                break;
            }else {
            for ( int i = 0; i<lineQ.size();i++){
                linesData[i] = lineQ[i][1]; // my comparison parameter is the lineQ[i][1]
            }

//根据我得到的答案我想这一点,但我真的不如何继续?

// based on the answer I got I'm trying this but I really don't how to continue ?

std::sort(lineQ.begin(),lineQ.end(),[](const cv::Vec2f &a,const cv::Vec2f &b)
            {
                return ????
            }

我试图用并做的,而循环,但我没有得到它,而功能的std :: adjacent_find 这有一个条件,即该元素应该是连续的。照片 也许是容易的,但我就是不明白这一点! 感谢您的帮助!

I tried use a for and do while loop, but I didn't get it, and the function std::adjacent_find this has a condition that the elements should be consecutive.
Maybe it's easy but I just don't get it ! thanks for any help !

推荐答案

最简单的办法是有点那么独特的擦除,但是这改变秩序。

The easy way is sort then unique-erase, but this changes order.

C ++的11阶preserving的方法是创建一个 unordered_set&LT; INT&GT; S; 做:

The c++11 order preserving way is to create an unordered_set<int> s; and do:

unordered_set<int> s;
vec.erase(
  std::remove_if( vec.begin(),vec.end(), // remove from vector
    [&](int x)->bool{
      return !std::get<1>(s.insert(x)); // true iff the item was already in the set
    }
  ),
  vec.end() // erase from the end of kept elements to the end of the `vec`
);

这是使用remove擦除成语 unordered_set 来检测是否有重复。

which is the remove-erase idiom using the unordered_set to detect duplicates.

这篇关于我怎样才能找到一个载体重复的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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