How do I print all elements in set?

Vojin

I have trouble printing out elements in set.

std::set<triple, Compare> edges;
for (int i = 0; i < n; i++)
            for (std::list<std::pair<int, int>>::iterator j = graph[i].begin(); j != graph[i].end(); j++)
                edges.insert(makeTriple((*j).second, i, (*j).first));

        for (std::set<triple, Compare>::iterator j = edges.begin(); j != edges.end(); j++)
            printf("%d and %d\n\n", (*j).first + 1, (*j).second + 1);

Only 7 elements are printed of of 13. Compare function looks like:

bool operator()(const triple &a, const triple &b) const
    {
        if (a.distance == b.distance && a.first == b.first)
            return (a.second < b.second);
        if (a.distance == b.distance && a.second == b.second)
            return (a.first < b.first);
        return (a.distance < b.distance);
    }
Ted Lyngmo

Your Compare function does not fulfill the requirement

The return value of the function call operation applied to an object of a type satisfying Compare, when contextually converted to bool, yields true if the first argument of the call appears before the second in the strict weak ordering relation induced by this type, and false otherwise.

The most obvious fix would be:

bool operator()(const triple &a, const triple &b) const {
    if (a.distance == b.distance) {
        if(a.first == b.first)  return a.second < b.second;
        else                    return a.first < b.first;
    } else                      return a.distance < b.distance;
}

This can however be done simpler with std::tie from <tuple>:

bool operator()(const triple &a, const triple &b) const {
    return
        std::tie(a.distance, a.first, a.second) < std::tie(b.distance, b.first, b.second);
}

Demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I find all the subsets of a set, with exactly n elements?

How do I compare an arrays set of elements and return the desired elements

How do I print the elements of a C++ vector in GDB?

How do I print out all keys in hashmap?

How can I print all the elements of a vector in a single string in R?

How do I print only non repeated elements in a list in python?

How do I store and print elements from document.getElementsByClassName?

How do I print certain elements of a struct in Swift?

In Python, how do I add all the elements of a list to a set?

C How Do I Print The Elements of an Array

How can I correctly set all elements of an array to a negative value?

How do I print nth elements in the list simultaneously?

How do I print all corresponding elements of 2 lists and separate them by text?

How do I print all of the instances from a set of variables that are undefined from the beginning?

How do I print all but the Nth to last line in sed?

How do I print all labels that are defined in a sphinx project?

How to print all subsets of a set?

How do I center all my elements?

How do I print all array items on one line?

Given a array with a blank element how do I print all elements except the blank one?

how do I print the number of elements

How do I access and print individual elements of a vector of vectors?

How do I print elements of structures contained in an array

(C Programming) How do I insert string into a character array and then print out all the elements of this array?

How do I print all filtered elements in PyMongo?

How do i uniquely print out the elements of a list inside a dictionary

How do i do a fade in effect on all the elements for darkmode and lightmode?

How do I print the sum of an array's elements?

How do I print the user and umask for all running processes?

TOP Ranking

HotTag

Archive