c++ - 如何在保持原始索引的同时对 vector 的 vector 进行排序?

标签 c++ algorithm sorting multidimensional-array stdvector

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

现在我想找到所述矩阵中的 N 个最大元素,其中 N 是行数。当我打印包含那些 N 最大值的数组/矩阵/vector 时,在值旁边显示元素在原始矩阵中的索引

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

这对你们来说可能看起来很基础,但我已经研究了一段时间。

我已经尝试了排序功能,当我让它工作时,它打乱了索引并改变了原始矩阵。

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!

以下应该可以完成这项工作,即使这可能不是最好的方法。

  • 提供结构ElementIntex ,其中 matrix元素并且可以存储它们的相应索引
  • 遍历 matrix 中的元素并将它们存储到 ElementIntex 的 vector 中.
  • std::vector<ElementIntex> 进行排序根据结构中的元素 ElementIntex使用二元谓词。 (降序排列)
  • 返回第一个N此排序的元素数量 std::vector<ElementIntex> , 其中N等于没有。 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]

关于c++ - 如何在保持原始索引的同时对 vector 的 vector 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56433823/

相关文章:

c++ - 使用strcpy_s将字符串复制到char *

javascript - Websocket 握手不起作用

java - 选择算法以在 O(n) 中查找出现次数超过 n/2 的元素

java - 在链表中查找大写字母并返回包含找到的元素的新链表?

algorithm - 在 N x M 矩阵中找到最大邻居数等于它们在最佳时间的邻居数

sorting - unix排序与混合数字和非数字不一致

c++ - 如何在一个函数中定义一个变量并在另一个函数中访问和更改它?(c++)

c++ - 访问冲突读取位置 0xC0000005 C++

java - 如何使用stream#sorted()按Java 8中两个字段的乘积进行排序

algorithm - 对较小的(子)数组进行排序如何使快速排序更快?