【问题标题】:How can I sort an array according to values of its corresponding vector?如何根据对应向量的值对数组进行排序?
【发布时间】:2018-07-25 17:51:23
【问题描述】:

我想根据对应向量的值对我的row 数组进行排序。这意味着,我有一个向量,其中包含相同长度的整数向量。 std::vector<std::vector<int>> vec。 根据vect 的值,我想对数组row 进行排序。 row 包含 N 元素,其中第一个元素给出了我们应该为我们的行考虑的数字。如果row[0]==2,那么我们应该只关心row[0]之后的2个元素。

例子:

Input:   vect = {{0,2,3},{2,1,5},{1,2,4}} 
         row  = {3,0,1,2,-1,-2,15}

请注意,在row 中,我们只关心第 2 到第 4 个元素,因为它的第一个元素是 3(表示 vect 的大小。即 row[0] = vect.size())。

我想根据值023, 215 and 124 对我的数组和向量进行排序。也就是说,在对我的向量进行排序之后,根据它的元素的新位置,我应该通过考虑它的第一个元素来对我的数组进行排序。 (只需对所需的进行排序。)

所以我想得到的是:

Output: vect = {{0,2,3},{1,2,4},{2,1,5}}
        row  = {3,0,2,1,-1,-2,15}

非常感谢您的帮助。以下是代码:

std::vector<int> seq;
int pair;
while(pair!=-1) {
    seq.push_back(letter[pair--]);
}

.
.
.
int* row = new int[N]; // N is the input
std::vector<std::vector<int>> vect;
vect.resize(row[0]);
for(int e=0;e<row[0];e++){
    for(int elm=0;elm<seq.size();elm++)
        vect[e].push_back(outputs[seq[elm]][row[e+1]]);
}
sort(vect,row); // sort both?

【问题讨论】:

  • 您对问题的解释措辞混乱,难以理解您的问题和问题所在。您应该编辑您的问题以包含清晰、明确的陈述,解释您的代码的用途您期望的输出以及您的错误输出得到。如果您这样做,您将更有可能得到有用的答案。
  • 我认为我们需要一个输入、过程和期望输出的清晰示例。我读到的问题是,您想根据row 中的索引对vect 中的vectors 进行排序,但是面对row 中的负数,这没有任何意义。
  • 这个问题中的许多短语让我停下来。我不确定我是否理解所说的话。 “期望的”、“我们只关心”、“即”、“相同长度的整数”,以及四个不同的“我想要……”也许是因为描述问题有困难,所以编写这段代码有困难?跨度>
  • 没有得到全部,但据我了解,我想知道;如果row[0]==5 怎么办?你的数组中有一些负值,你的二维向量数组中没有那么多行。
  • 恐怕更新后我还是听不懂你在问什么。看起来好像给定 3,您想要对元素 3、4 和 5 进行排序,但 vect 的样本没有 3、4 和 5。

标签: c++ algorithm sorting stdvector


【解决方案1】:

以下将提供您想要的输出或至少放弃(可能效率不高)来解决您的问题。希望 cmets 可以指导您完成。

See Output

#include <vector>
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
   std::vector<std::vector<int>>  vect = {{0,2,3},{2,1,5},{1,2,4}};
   std::vector<int> row = {3,0,1,2,-1,-2,15};
   // new vector:  pair of integer -> representing the rows of vect and index
   std::vector<std::pair<std::string, int>> newVect;
   newVect.reserve(vect.size());

   int index = 0;
   for(const std::vector<int>& each_row: vect)
   {
      std::string str;             // each row of vect to a single integer string
      for(const int Integer: each_row) str += std::to_string(Integer);
      newVect.emplace_back(std::make_pair(str, index));
      ++index;
   }
   // sort the new vector, according to the whole 2D vector row(which is a single string)
   std::sort(newVect.begin(), newVect.end(), [](const auto& lhs, const auto& rhs)
   {
      return lhs.first < rhs.first;
   });
   // now you can actually store the sorted vect
   vect.clear();
   for(auto index = 1; index <= row[0]; ++index)
   {
      row[index] = newVect[index-1].second;  // replace the row indexes which are sorted
      std::vector<int> vect_row;
      // change each chars of string back to corresponding row elements
      for(const char Integer: newVect[index-1].first)
         vect_row.emplace_back(static_cast<int>(Integer - '0'));
      // store to original vector
      vect.emplace_back(vect_row);
   }

   // to print
   for(const std::vector<int>& each_row: vect)
   {
      for(const int Intger: each_row)
         std::cout << Intger << " ";
      std::cout << std::endl;
   }
   for(const int it: row) std::cout << it << " ";
   return 0;
}

【讨论】:

  • @Tripoli 希望对您有所帮助。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
  • 1970-01-01
  • 1970-01-01
  • 2011-10-09
  • 2013-11-25
相关资源
最近更新 更多