std::tie

std::tie

  tie 有两个功能。

1、pack成一个 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);
    }
};

2、可以用于 unpack tuple、pair

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";
}

参考:

1、https://en.cppreference.com/w/cpp/utility/tuple/tie

猜你喜欢

转载自www.cnblogs.com/tekkaman/p/10194548.html
std
今日推荐