简体   繁体   中英

Sort QMap<QString, int>

I have data struct QMap<QString, int> how can i sort it by int key?

Thank you.

1) Create std::map<int, std::string> and push all data to it (or your QString and QMap ).

or

2) Create std::vector<std::pair<int, std::string>> vec , push all data to it and call std::sort(vec.begin(), vec.end());

or

3) Use boost::bimap

template<class K, class V>
struct InvertPairOf {
    std::pair<V,K> operator()(const std::pair<K,V>& p) const {
        return std::make_pair(p.second, p.first);
    }
};

void process(const QString& qm) {
    std::map<int, QString> sorted;
    std::transform(qm.begin(), qm.end(),
                   std::inserter(sorted, sorted.begin()),
                   InvertPairOf<QString,int>());
    process_sorted(sorted);  // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM