lundi 18 février 2019

How to use std::unordered_set to a vector

I have a std::vector< std::vector< int> > that contains prime numbers. I wish to eliminate double entries, as the vector contents will be exported to a file and should only contain unique primes.

The prime numbers will come from certain intervals, fed to my algorithm through a file, e.g. (32,345). There may be cases where the intervals will overlap e.g. (54,434),(345,596). I wish to use std::unordered_set, since it will be the most efficient for my case. I have seen examples with a std::vector< int> and tried to accommodate that to my needs, to no avail.

Here is code I tried, inspired by the print method of a 2d vector.

std::vector<std::vector<int> > sharedAnswersVec;
...
...
std::unordered_set<int> unique_primes;
for (auto i = 0; i < sharedAnswerVec.size(); i++) 
{
    for (auto j = 0; j < sharedAnswerVec[j].size(); j++) 
    {
        unique_primes.insert({ i,j });
        sharedAnswerVec.assign(unique_primes.begin(), unique_primes.end());
        sort(sharedAnswerVec.begin(), sharedAnswerVec.end());
    }
}

sharedAnswerVec cannot be std::vector< int>, has to be std::vector< std::vector< int> >. With that in mind, what changes must I do in order to correct it?

I am a beginner trying to learn, so if the question seems silly to you, please bear that in mind.

Aucun commentaire:

Enregistrer un commentaire