Removing repeated values

5

I have two vectors , each with (x, y) coordinates. I need to invert the second vector , getting with (y, x) and merge with the first, but I can not get repetition in the first field, so I thought I'd use a set .

However, I need the second value of the structure to always be as large as possible. For example, if I have the following values: {(3, 2); (3, 10)} , I need to set the pair (3,10) on the set.

Is it possible to do this with set?

Pseudo-code example:

vector<pair<int, int> > vector1 = {(10, 2); (10, 1); (3, 7)};
vector<pair<int, int> > vector2 = {(1, 3); (9, 10)};

Inverting the coordinates of the second vector, it would look like:

vector2 = {(3, 1); (10, 9)};

When merging with the first vector, I want the values of the first field to be unique, while the values of the second field are always the largest. In case I wanted a set with the following values:

set1<pair<int, int> > set1 = {(10, 9); (3, 7)};
    
asked by anonymous 15.03.2014 / 15:43

2 answers

6

Considering C ++ 11, the following is valid:

vector<pair<int, int>> vec1 = {{10, 2}, {10, 1}, {3, 7}};
vector<pair<int, int>> vec2 = {{1, 3}, {9, 10}};

To invert the coordinates of each element of the second vector, you can do the following:

for (auto& coord : vec2)
    swap(coord.first, coord.second);

Now regarding inserting them into a std::set while keeping only the highest value, you're probably doing something very wrong here. Checking if the coordinate value is already in the set will cost you a loop going through all elements of the set. This for each insert. So to enter% with% items, you need n operations. It's a lot.

From what I understand, you have a coordinate that identifies the pair and another that is just a value. Clearly you have a pair of key and value and want to have a list of them without repeating keys. What you need is .

map<int, int> m;

void insertPair(const pair<int, int>& p) {
    if (m.find(p.first)) // Se já tem a key
        m[p.first] = max(m[p.first], p.second);
    else
        m[p.first] = p.second;
}

for (const auto& coord : vec1)
    insertPair(coord);

for (const auto& coord : vec2)
    insertPair(coord);

This function has the complexity of std::map now that you do not have to search all elements for the key.

If you still want to use n log(n) , you have to go the long way:

set<pair<int, int>> s;

void insertPair(const pair<int, int>& p) {
    for (const pair<int, int>& p2 : s) { // percorra todo o set em busca do elemento
        if (p2.first == p.first) { // ao encontrar
            p2.second = max(p2.second, p.second); // troque o valor pelo maior
            return; // e retorne
        }
    }
    s.insert(p); // se não encontrou, basta inserir
}

for (const auto& coord : vec1)
    insertPair(coord);

for (const auto& coord : vec2)
    insertPair(coord);
    
15.03.2014 / 17:54
4

Only vectors (using generic lambdas ++ 14):

#include <vector>
#include <utility>
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    vector<pair<int, int>> v1 = { {10, 2}, {10, 1}, {3, 7} };
    vector<pair<int, int>> v2 = { {1, 3}, {9, 10} };

    for(auto &p : v2)
        swap(p.first, p.second);
    v1.insert(end(v1), begin(v2), end(v2));
    sort(rbegin(v1), rend(v1));
    v1.erase(
        unique(begin(v1), end(v1),
            [](auto lhs, auto rhs) { return lhs.first == rhs.first; }),
        end(v1));

    for (auto &p : v1)
        cout << '(' << p.first << ", " << p.second << ')' << endl;
}

or:

#include <vector>
#include <utility>
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    vector<pair<int, int>> v1 = { {10, 2}, {10, 1}, {3, 7} };
    vector<pair<int, int>> v2 = { {1, 3}, {9, 10} };

    for(auto &p : v2)
        swap(p.first, p.second);
    v1.insert(end(v1), begin(v2), end(v2));
    sort(rbegin(v1), rend(v1));
    vector<pair<int, int>> v3;
    unique_copy(begin(v1), end(v1), back_inserter(v3),
        [](auto lhs, auto rhs) { return lhs.first == rhs.first; });

    for (auto &p : v3)
        cout << '(' << p.first << ", " << p.second << ')' << endl;
}
    
16.03.2014 / 02:45