久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何通過不同 std::vector 的值對 std::vector 進行排序

How do I sort a std::vector by the values of a different std::vector?(如何通過不同 std::vector 的值對 std::vector 進行排序?)
本文介紹了如何通過不同 std::vector 的值對 std::vector 進行排序?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有幾個 std::vector,長度都一樣.我想對這些向量之一進行排序,并對所有其他向量應用相同的轉換.有沒有一種巧妙的方法來做到這一點?(最好使用 STL 或 Boost)?一些向量包含 ints,其中一些包含 std::strings.

I have several std::vector, all of the same length. I want to sort one of these vectors, and apply the same transformation to all of the other vectors. Is there a neat way of doing this? (preferably using the STL or Boost)? Some of the vectors hold ints and some of them std::strings.

偽代碼:

std::vector<int> Index = { 3, 1, 2 };
std::vector<std::string> Values = { "Third", "First", "Second" };

Transformation = sort(Index);
Index is now { 1, 2, 3};

... magic happens as Transformation is applied to Values ...
Values are now { "First", "Second", "Third" };

推薦答案

friol 的方法與您的方法結合使用時效果很好.首先,構建一個包含數字 1...n 的向量,以及向量中指示排序順序的元素:

friol's approach is good when coupled with yours. First, build a vector consisting of the numbers 1…n, along with the elements from the vector dictating the sorting order:

typedef vector<int>::const_iterator myiter;

vector<pair<size_t, myiter> > order(Index.size());

size_t n = 0;
for (myiter it = Index.begin(); it != Index.end(); ++it, ++n)
    order[n] = make_pair(n, it);

現在您可以使用自定義排序器對這個數組進行排序:

Now you can sort this array using a custom sorter:

struct ordering {
    bool operator ()(pair<size_t, myiter> const& a, pair<size_t, myiter> const& b) {
        return *(a.second) < *(b.second);
    }
};

sort(order.begin(), order.end(), ordering());

現在您已經捕獲了 order 中的重新排列順序(更準確地說,在項目的第一個組件中).您現在可以使用此順序對其他向量進行排序.可能有一個非常聰明的就地變體同時運行,但在其他人提出它之前,這里有一個不是就地變體.它使用 order 作為每個元素新索引的查找表.

Now you've captured the order of rearrangement inside order (more precisely, in the first component of the items). You can now use this ordering to sort your other vectors. There's probably a very clever in-place variant running in the same time, but until someone else comes up with it, here's one variant that isn't in-place. It uses order as a look-up table for the new index of each element.

template <typename T>
vector<T> sort_from_ref(
    vector<T> const& in,
    vector<pair<size_t, myiter> > const& reference
) {
    vector<T> ret(in.size());

    size_t const size = in.size();
    for (size_t i = 0; i < size; ++i)
        ret[i] = in[reference[i].first];

    return ret;
}

這篇關于如何通過不同 std::vector 的值對 std::vector 進行排序?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Assertion failed (size.widthgt;0 amp;amp; size.heightgt;0)(斷言失敗(size.width0 amp;amp; size.height0))
Rotate an image in C++ without using OpenCV functions(在 C++ 中旋轉圖像而不使用 OpenCV 函數)
OpenCV: process every frame(OpenCV:處理每一幀)
Why can#39;t I open avi video in openCV?(為什么我不能在 openCV 中打開 avi 視頻?)
OpenCV unable to set up SVM Parameters(OpenCV 無法設置 SVM 參數)
Convert a single color with cvtColor(使用 cvtColor 轉換單一顏色)
主站蜘蛛池模板: 亚洲成人午夜电影 | 亚洲国产网站 | 91精品国产欧美一区二区成人 | 国产日韩视频 | 欧美日韩专区 | 福利网站在线观看 | 国产色视频网站 | 一级黄色日本片 | 久久久精品一区二区 | 精品久久久久久亚洲综合网 | 一区二区免费看 | 日本粉嫩一区二区三区视频 | 成人av片在线观看 | 免费观看av网站 | 国产一区二区在线免费观看 | 在线a视频| 91精品久久久久久久久 | 色婷婷一区二区三区四区 | 欧美美乳 | 国产99久久精品一区二区永久免费 | 国产精品久久久久久久久久免费看 | 中文字幕精品一区二区三区精品 | 欧美在线| www国产亚洲精品 | 欧美精品在线观看 | 欧美一区二区 | 国产午夜精品一区二区三区嫩草 | 欧美一级淫片免费视频黄 | 亚洲女人天堂成人av在线 | 91av在线不卡 | 亚洲一区在线观看视频 | 久热久热| 欧美一区二区三区四区在线 | 欧美成视频| 欧美a在线 | 不卡一二区 | 九九亚洲 | 日韩在线视频精品 | 日韩福利 | 国内自拍第一页 | 亚洲经典一区 |