C ++在向量中查找并保存重复项

时间:2018-05-03 09:58:13

标签: c++ vector stl duplicates unique

我有一个用户自定义类型向量的自定义向量

第一个向量通过stdin填充元素,然后我对它进行排序并尝试在其中查找重复项并保存它们

我设法找到所有独特的元素,但我需要找到并获得重复的矢量 我需要一个提示或一个简单的解决方案来解决这个问题

这是我的代码:

Agressor.h

#ifndef Agressor_h
#define Agressor_h

#include <string>
#include <vector>

using namespace std;

class Agressor{
public:
    /*const char**/ string traderIdentifier;
    /*const char**/ string side;
    int quantity;
    int price;
    vector<Agressor> v;

    void display(){
        cout << traderIdentifier << " " << side << " " << quantity << " " << price << endl;
    }
    explicit Agressor(){

    }
    ~Agressor(){

    }
    friend ostream &operator<<(ostream& stream, const Agressor& item);
    const friend bool operator > (const Agressor &a1, const Agressor &a2);

   // const friend bool operator == (const Agressor &a1, const Agressor &a2);

    /* vector<Agressor>& operator[](int i ){
        return v[i];
    }*/


};


ostream &operator<<(ostream& stream, const Agressor& item) {
    string side = "";
    if(item.side == "B"){
        side = '+';
    }else{
        if(item.side == "S"){
            side = "-";
        }
    }

    stream << item.traderIdentifier << side << item.quantity << "@" << item.price << "\n";
    return stream;
}

const bool operator == (const Agressor &a1, const Agressor &a2){
    bool isEqual = false;
    if((a1.price*a1.quantity == a2.price*a2.quantity) && (a1.traderIdentifier == a2.traderIdentifier) && (a1.side == a2.side)){
        isEqual = true;
    }
    return(isEqual);
}


const bool operator > (const Agressor &a1, const Agressor &a2){
    bool isGreater = false;
    if(a1.price*a1.quantity > a2.price*a2.quantity){
        isGreater = true;
    }
    return(isGreater);
}



#endif /* Agressor_h */

的main.cpp

#include <iostream>
#include "Agressor.h"
#include <sstream>
using namespace std;


vector<string> &split(const string &s, char delim, vector<string> &elems)
{
    stringstream ss(s);
    string item;
    while (getline(ss, item, delim))
    {
        elems.push_back(item);
    }
    return elems;
}

vector<string> split(const string &s, char delim)
{
    vector<string> elems;
    split(s, delim, elems);
    return elems;
}

bool equal_comp(const Agressor& a1, const Agressor& a2){
    if((a1.price*a1.quantity == a2.price*a2.quantity) && (a1.traderIdentifier == a2.traderIdentifier) && (a1.side == a2.side)){
        return true;
    }
    return false;
}

int main(int argc, const char * argv[]) {
    Agressor agr;
    while (true) {
            std::string sText;
            cout << "enter query:" << endl;
            std::getline(std::cin, sText);
        if(sText == "q"){
            cout << "Program terminated by user" << endl;
            break;
        }else{
            std::vector<std::string> sWords = split(sText, ' ');
            agr.traderIdentifier = sWords[0];
            agr.side = sWords[1];
            agr.quantity = stoi(sWords[2]);
            agr.price = stoi(sWords[3]);
            agr.v.push_back(agr);

            vector<Agressor>::iterator it;
            sort(agr.v.begin(), agr.v.end(), greater<Agressor>());
            //unique(agr.v.begin(), agr.v.end(), equal_comp);
            for (vector<Agressor>::const_iterator i = agr.v.begin(); i != agr.v.end(); ++i)
                cout << *i << ' ';

    }
    }
    cout << "here we go..." << endl;
    vector<Agressor>::iterator it;
    sort(agr.v.begin(), agr.v.end(), greater<Agressor>());
    //it = unique(agr.v.begin(),agr.v.end(), equal_comp);
    //agr.v.resize( distance(agr.v.begin(),it) );
    agr.v.erase(unique(agr.v.begin(),agr.v.end(), equal_comp), agr.v.end());

    copy(agr.v.begin(), agr.v.end(), ostream_iterator<Agressor>(cout, "\n"));



    return 0;
}

2 个答案:

答案 0 :(得分:1)

您可以使用以下内容:

template <typename T>
std::vector<T> get_duplicates(const std::vector<T>& v)
{
     // expect sorted vector

     auto it = v.begin();
     auto end = v.end();
     std::vector<T> res;
     while (it != end) {
         it = std::adjacent_find(it, end);
         if (it != end) {
             ++it;
             res.push_back(*it);
         }
     }
     return res;
}

答案 1 :(得分:1)

std::unique使用以后的非重复值覆盖重复值。您可以实现一个类似的算法,将值移动到某个位置。

template<class ForwardIt, class OutputIt, class BinaryPredicate>
ForwardIt unique_retain(ForwardIt first, ForwardIt last, OutputIt d_first, BinaryPredicate p)
{
    if (first == last)
        return last;

    ForwardIt result = first;
    while (++first != last) {
        if (!p(*result, *first) && ++result != first) {
            *d_first++ = std::move(*result);
            *result = std::move(*first);
        }
    }
    return ++result;
}

(改编自std::unique的{​​{3}})

然后你会像

一样使用它
vector<Agressor> dups;
sort(agr.v.begin(), agr.v.end(), greater<Agressor>());
auto it = unique_retain(agr.v.begin(),agr.v.end(), std::back_inserter(dups), equal_comp);
agr.v.erase(it, agr.v.end());