从最大长度到最小长度迭代映射中的键

Iterate over keys in map from largest length to smallest length

我正在尝试遍历地图中的所有键。我有这个代码:

map<string, array<string, 3>> dat;
array<string, 3> dt({ "var","TEXT","" });
dat["atest"] = dt;
array<string, 3> at({ "var","DATA","" });
dat["testplusalot"] = at;
array<string, 3> t({ "var","NONE","" });
dat["testalot"] = t;
for (const auto& p : dat) {
    cout << p.first << endl;
}

我要它说

testplusalot
testalot 
atest

但我明白了

atest
testalot
testplusalot

我怎么能这样做。

尝试像下面的代码片段中所示那样声明地图

#include <iostream>
#include <utility>
#include <string>
#include <array>
#include <map>

//...

    auto descending = []( const std::string &s1, const std::string &s2 )
    {
        auto n1 = s1.size(), n2 = s2.size();
        return std::tie( n2, s1 ) < std::tie( n1, s2 );
    };

    std::map<std::string, std::array<std::string, 3>, decltype( descending )> dat( descending );

    dat =
    {
        { "test", { "var","TEXT","" } },
        { "testplusalot", { "var","DATA","" } },
        { "testalot", { "var","NONE","" } }
    };

    for (const auto &p : dat)
    {
        std::cout << p.first << '\n';
    }

它的输出是

testplusalot
testalot
test

在 std::map (https://en.cppreference.com/w/cpp/utility/functional/greater).

中添加 std::greated 作为比较函数

#include <map>
#include <array>
#include <string>
#include <iostream>

int main()
{
    std::map<std::string, std::array<std::string, 3>, std::greater<std::string>> dat;
    
    std::array<std::string, 3> dt({ "var","TEXT","" });
    dat["test"] = dt;

    std::array<std::string, 3> at({ "var","DATA","" });
    dat["testplusalot"] = at;

    std::array<std::string, 3> t({ "var","NONE","" });
    dat["testalot"] = t;

    for (const auto& p : dat) {
        std::cout << p.first << std::endl;
    }

    return 0;
}

输出将是:

testplusalot
testalot
test

PS,这段代码也会return

testplusalot
testalot
atest

用于 [testplusalot, testalot, atest] 键。

既然要按长度对key进行排序,然后如果长度相同,则按字母顺序排序(最简单的fall-back排序),那么可以进行如下操作:

#include <map>
#include <string>
#include <array>
#include <iostream>

// Create a type that describes the sort order
struct strCompare 
{
    bool operator()(const std::string& Left, const std::string& Right) const 
    {
        // Sort by length 
        if ( Left.length() != Right.length() )
            return Left.length() > Right.length();

       // Fall back to string < ordering
        return Left < Right;            
    }
};

int main()
{
    std::map<std::string, std::array<std::string, 3>, strCompare> dat;
    std::array<std::string, 3> dt({ "var","TEXT","" });
    dat["atest"] = dt;
    std::array<std::string, 3> at({ "var","DATA","" });
    dat["testplusalot"] = at;
    std::array<std::string, 3> t({ "var","NONE","" });
    dat["testalot"] = t;
    std::array<std::string, 3> t2({ "var","NONE","" });
    dat["testblot"] = t2;
    for (const auto& p : dat) {
        std::cout << p.first << std::endl;
    }   
}

输出:

testplusalot
testalot
testblot
atest

strCompare 是一种具有重载 < 的类型,它决定了关键排序标准。

那么std::map的创建需要在third template parameter中指定排序顺序:

std::map<std::string, std::array<std::string, 3>, strCompare> dat;