Sort vector of vectors

后端 未结 4 1624
离开以前
离开以前 2021-02-13 22:20

I have

    vector> vec 

in my c++ app.

Every vector of integers as an element of \"big\" vector has 4 INT valu

4条回答
  •  情书的邮戳
    2021-02-13 22:34

    If you want to compare the two vectors by cost, try this:

    bool predicate(const std::vector& a, const std::vector& b)
    {
        return COST(a) < COST(b);
    }
    

    Notes:

    • The above works with C++98, too, I'm not sure about how widespread the use of C++11 is and whether you have a compliant compiler. Otherwise, you can of course use a lambda expression, too, as sftrabbit suggested.
    • You don't say what COST returns, I simply assumed some sortable value like float or long.
    • I hope you don't copy the vector when passing it to COST(), that would be horribly inefficient.
    • COST suggests a macro, like all UPPERCASE_NAMES. Don't use macros. Don't use macro names for functions.

提交回复
热议问题