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

C++,基于另一個向量對一個向量進行排序

C++, Sort One Vector Based On Another One(C++,基于另一個向量對一個向量進行排序)
本文介紹了C++,基于另一個向量對一個向量進行排序的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我得到的最好的例子是我想根據他們的分數對姓名進行排序.

The best example I've got is that I want to sort Names based on their Score.

vector <string> Names {"Karl", "Martin", "Paul", "Jennie"};
vector <int> Score{45, 5, 14, 24};

因此,如果我將分數排序為 {5, 14, 24, 45},則名稱也應根據其分數進行排序.

So if I sort the score to {5, 14, 24, 45}, the names should also be sorted based on their score.

推薦答案

正如其他答案中已經建議的那樣:結合每個人的姓名和分數可能是最簡單的解決方案.

As already suggested in other answers: Combining the name and the score of each individual is likely the simplest solution.

通常,這可以通過有時稱為壓縮"操作的方式來實現:將兩個向量組合成一對向量 - 以及相應的解壓縮".

Generically, this can be achieved with what is sometimes referred to as a "zip" operation: Combining two vectors into a vector of pairs - along with a corresponding "unzip".

一般實現,這可能如下所示:

Implemented generically, this may look as follows:

#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <iterator>

// Fill the zipped vector with pairs consisting of the
// corresponding elements of a and b. (This assumes 
// that the vectors have equal length)
template <typename A, typename B>
void zip(
    const std::vector<A> &a, 
    const std::vector<B> &b, 
    std::vector<std::pair<A,B>> &zipped)
{
    for(size_t i=0; i<a.size(); ++i)
    {
        zipped.push_back(std::make_pair(a[i], b[i]));
    }
}

// Write the first and second element of the pairs in 
// the given zipped vector into a and b. (This assumes 
// that the vectors have equal length)
template <typename A, typename B>
void unzip(
    const std::vector<std::pair<A, B>> &zipped, 
    std::vector<A> &a, 
    std::vector<B> &b)
{
    for(size_t i=0; i<a.size(); i++)
    {
        a[i] = zipped[i].first;
        b[i] = zipped[i].second;
    }
}


int main(int argc, char* argv[])
{
    std::vector<std::string> names {"Karl", "Martin", "Paul", "Jennie"};
    std::vector<int> score {45, 5, 14, 24};

    // Zip the vectors together
    std::vector<std::pair<std::string,int>> zipped;
    zip(names, score, zipped);

    // Sort the vector of pairs
    std::sort(std::begin(zipped), std::end(zipped), 
        [&](const auto& a, const auto& b)
        {
            return a.second > b.second;
        });

    // Write the sorted pairs back to the original vectors
    unzip(zipped, names, score);

    for(size_t i=0; i<names.size(); i++)
    {
        std::cout << names[i] << " : " << score[i] << std::endl;
    }
    return 0;
}

這篇關于C++,基于另一個向量對一個向量進行排序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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影院在线观看 | 91av视频在线 | 日一区二区三区 | 一级a性色生活片久久毛片 午夜精品在线观看 | 久久精品国产亚洲夜色av网站 | 久久久久久国产精品 | 亚洲免费精品 | 亚洲一二三区精品 | a看片 | 欧美午夜精品 | 国产成人精品a视频 | 久久亚洲一区二区 | 在线国产视频观看 | 成人精品一区二区 | 成人免费大片黄在线播放 | 国产精品日韩欧美一区二区三区 | 国产精品久久久久久久午夜 | 欧美日韩精品一区 | 古装人性做爰av网站 | 欧美色性| 中文字幕精品视频 | 久久久久久国产一区二区三区 | 国产久| 国产日韩精品视频 | 亚洲精品视频导航 | 九一视频在线观看 | 一区二区免费看 | 日本特黄a级高清免费大片 国产精品久久性 | 婷婷在线免费 | 超碰在线97国产 | chinese中国真实乱对白 | 欧美日韩手机在线观看 | 九九热精品在线视频 | 成人一区二区三区视频 | 中文字幕第7页 | 久久久这里都是精品 | 999热视频| 色婷婷一区二区三区四区 |