如何在保留原始索引的同时对向量进行排序?

时间:2019-06-03 20:00:58

标签: c++ algorithm sorting multidimensional-array stdvector

代码生成一个可变大小的矩阵,其列和行的大小由用户定义。用户还可以手动填充第一行,然后自动填充其他行,第一行之后的每一行都是原始行除以我们所在的行。

现在,我想在上述矩阵中找到最大的N个元素,其中 N是行数。当我打印包含最大N个值的数组/矩阵/向量时,要在原始矩阵中与该值一起显示元素的索引

在保留其原始索引的同时对2D向量进行排序的最佳方法是什么?

这对你们来说似乎真的很基础,但是我一直在努力。

我已经尝试过排序功能,当我使它起作用时,它会加扰索引并更改原始矩阵。

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;
}

2 个答案:

答案 0 :(得分:2)

如果你是故意的

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

即使这可能不是最佳方法,也应执行以下操作。

  • 提供一个结构ElementIntex,其中可以存储matrix元素和它们的相应索引
  • 遍历matrix中的元素,并将它们存储到ElementIntex的向量中。
  • 使用二进制谓词根据结构std::vector<ElementIntex>中的元素ElementIntex进行排序。 (按降序排列)
  • 从排序后的N返回第std::vector<ElementIntex>个元素,其中N等于no。 matrix中的行数。

以下是示例代码:See Live

#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]

答案 1 :(得分:-3)

#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*/
}