Compare tuples?

programming/C++ 2013. 2. 20. 19:15

std::tuple은 lexicographical comparison을 지원한다.(boost::tuple도 마찬가지)


Example


#include <iostream>
#include <string>
#include <set>
#include <tuple>
 
struct S {
    int n;
    std::string s;
    float d;
    bool operator<(const S& rhs) const
    {
        // compares n to rhs.n,
        // then s to rhs.s,
        // then d to rhs.d
        return std::tie(n, s, d) < std::tie(rhs.n, rhs.s, rhs.d);
    }
};
 
int main()
{
    std::set<S> set_of_s; // S is LessThanComparable
 
    S value{42, "Test", 3.14};
    std::set<S>::iterator iter;
    bool inserted;
 
    // unpacks the return value of insert into iter and inserted
    std::tie(iter, inserted) = set_of_s.insert(value);
 
    if (inserted)
        std::cout << "Value was inserted successfully\n";
}


'programming > C++' 카테고리의 다른 글

Explicit conversion  (0) 2013.02.19
Online C++ compilers  (0) 2013.02.18
std::atomic  (0) 2013.02.18
C++11 cheat sheet  (0) 2013.02.18
Universal reference  (0) 2013.02.18
Posted by 무한자전거
,

In C++03, you need to use safe bool idiom to avoid evil things, like

int x = my_object; // this works


In C++11, use explicit conversion.

explicit operator bool() const;



'programming > C++' 카테고리의 다른 글

Compare tuples?  (0) 2013.02.20
Online C++ compilers  (0) 2013.02.18
std::atomic  (0) 2013.02.18
C++11 cheat sheet  (0) 2013.02.18
Universal reference  (0) 2013.02.18
Posted by 무한자전거
,

http://isocpp.org/blog/2013/01/online-c-compilers

'programming > C++' 카테고리의 다른 글

Compare tuples?  (0) 2013.02.20
Explicit conversion  (0) 2013.02.19
std::atomic  (0) 2013.02.18
C++11 cheat sheet  (0) 2013.02.18
Universal reference  (0) 2013.02.18
Posted by 무한자전거
,