簡體   English   中英

如何在保持原始索引的同時對向量矢量進行排序?

[英]How to sort a vector of vectors while keeping the original indexes?

該代碼生成一個可變大小的矩陣,其列和行大小由用戶定義。 用戶還手動填充第一行,然后自動填充其他行,第一行之后的每一行是原始行除以我們所在的行。

現在我想找到所述矩陣中的N最大元素,其中N是行數 當我打印包含N最大值的數組/矩陣/向量時, 在原始矩陣中顯示元素索引的值

在保持原始索引的同時,對這個2D矢量進行排序的最佳方法是什么?

對於你們來說這似乎是非常基本的,但我已經和它搏斗了一下。

我已經嘗試了sort函數,當我讓它工作時,它會擾亂索引並改變原始矩陣。

int main() 
{
    using namespace std;
    vector<string> header;
    vector<vector<double>> matrice;
    vector<double> temp;

    cout << "How many columns does it have?" << endl;
    cin >> columnsize;
    cout << "How many rows does it have?" << endl;
    cin >> rowsize;
    cout << "Whats the number of votos in order" << endl;
    for (int i = 0; i < columnsize; i++) 
    {
        cin >> ccontent;
        temp.push_back(ccontent);
    }
    matrice.push_back(temp);

    for (int i = 0; i < columnsize; i++) 
    {
        cout << "Qual é o nome da lista:" << i + 1 << endl;
        cin >> Nomelista;
        header.push_back(Nomelista);
    }

    for (int i = 1; i < rowsize; i++) 
    {
        temp.clear();
        for (int j = 0; j < columnsize; j++) 
        {
            temp.push_back((matrice[0][j]) / (i + 1));
        }
        matrice.push_back(temp);
    }
    return 0;
}

如果你的意思是

N = matrice.size() = no. rows of the matrix!

以下應該做的工作,即使這可能不是最好的方法。

  • 提供struct ElementIntex ,其中可以存儲matrix元素及其相應的索引
  • 迭代matrix的元素並將它們存儲到ElementIntex的矢量中。
  • 使用二元謂詞根據struct ElementIntex元素std::vector<ElementIntex>進行排序。 (按降序排列)
  • 返回此排序的std::vector<ElementIntex>的前N個元素,其中N等於no。 matrix的行數。

以下是一個示例代碼:( 查看直播

#include <iostream>
#include <vector>
#include <cstddef>   // std::size_t
#include <algorithm> // std::sort

struct ElementIntex
{
    std::size_t col, row;
    double element;
    ElementIntex(std::size_t cl, std::size_t rw, double ele)
        : col{cl}
        , row{rw}
        , element{ele}
    {}
};

std::vector<ElementIntex> getLargestElements(
                          const std::vector<std::vector<double>>& matrice)
{
    std::vector<ElementIntex> vec;
    // reserve the memory to prevent unwanted reallocations: if you now the size
    // vec.reserve(/*total no. of elements*/)
    std::size_t rowIndex = 0;
    for (const std::vector<double>& row : matrice)
    {
        std::size_t colIndex = 0;
        for (const double element : row)
            vec.emplace_back(rowIndex, colIndex++, element);
        ++rowIndex;
    }
    // sort descending order of elements in the vector of `ElementIntex`
    std::sort(vec.begin(), vec.end(),
            [](const auto & lhs, const auto & rhs) { return lhs.element > rhs.element; });
    // return N largest elements from the sorted vector: where N = matrice.size() = no. rows!
    return { vec.cbegin(), vec.cbegin() + matrice.size() };
}

int main()
{
    // consider the following vector of vectors(matrx in your case)
    std::vector<std::vector<double>> matrice{
        {1.05, -8.05, 1.0, 8.58, 3.04},
        {15.05, 8.05, 7.05, 8.58},
        {11.05, 88.05, 7.06},
        {-12.05, -8.05}
    };

    const auto resultVec{ getLargestElements(matrice) };
    for (const ElementIntex& elementIndex : resultVec)
        std::cout << "The element " << elementIndex.element
                  << " and index [" << elementIndex.row 
                  << "][" << elementIndex.col << "]\n";

    return 0;
}

輸出

The element 88.05 and index [1][2]
The element 15.05 and index [0][1]
The element 11.05 and index [0][2]
The element 8.58 and index [3][0]
#include<bits/stdc++.h>
using namespace std;
int main()
{
    vector<vector<int>> v;
    int n;
    cin>>n;

    //input: 1 5 3 6 8 7 9
    // n =7
    for(int i=0; i<n; i++)
    {
    vector<int> temp1;
    int e;
    cin>>e;
    temp1.push_back(e);
    temp1.push_back(i);
    v.push_back(temp1);

    }

    sort(v.begin(),v.end());
    for(int i=0; i<n; i++)
    {
        cout<<v[i][0]<<" "<<v[i][1]<<endl;
    }

/* output: 
1 0
2 2
5 1
6 3
7 5
8 4
9 6*/
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM