Question

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

Thank you.

Was it helpful?

Solution

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

OTHER TIPS

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);  // ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top