久久久久久久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 轉換單一顏色)
主站蜘蛛池模板: 亚洲最大av网站 | 日韩精品在线一区 | 一级中国毛片 | 欧美三级免费观看 | 成年网站在线观看 | 请别相信他免费喜剧电影在线观看 | 亚洲情综合五月天 | 97精品国产 | 欧美在线视频一区二区 | 国产原创视频 | 欧美成人自拍 | 日韩中文字幕在线视频 | 日韩成人在线播放 | 精品免费观看 | 日韩三级在线 | 亚洲精品日韩在线 | 欧美中文在线 | 色资源站 | 日韩视频在线播放 | 国产精品久久久久久久久久 | 亚洲另类春色偷拍在线观看 | 99re国产 | 久久久久久久久久影视 | 精品在线一区二区 | 午夜网站视频 | 国产精品无码久久久久 | 国产亚洲精品精品国产亚洲综合 | 伊人久久大香线 | 国产日韩欧美 | 日韩视频一区 | 又爽又黄axxx片免费观看 | 成人性视频免费网站 | 免费成人av | 日本五月婷婷 | 日韩电影中文字幕 | 欧美激情综合色综合啪啪五月 | 1000部精品久久久久久久久 | 精品不卡 | 国产视频线观看永久免费 | 中文字幕在线视频免费观看 | 日韩欧美一级精品久久 |