繁体   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