【问题标题】:2D vector sort unwanted result2D 矢量排序不需要的结果
【发布时间】:2015-03-14 16:51:27
【问题描述】:

我正在尝试对二维向量进行排序。不是元素,而是只移动列而不是行元素。在我的向量中,我保留了列索引的第一位,因此即使对二维数组进行排序,我也知道原始列号。所以我写了这段代码:

   // Sorting the rows.
   sort(numberlist[i].begin(), numberlist[i].end());

   // Adding the indexes.
   for(int e = 0; e < boxnum; e++)
   {
       numberlist[e][0] = e + 1;
   }

   //Sorting the columns.
   sort(numberlist.begin(), numberlist.end(), [](const vector<int>& a,
                                                 const vector<int>& b){
                                                 return a < b; });

我对程序的输入:

3 7
8 10
5 2
9 11
21 18

我期待这个结果:

3 2 5
1 3 7
2 8 10
4 9 11
5 18 21

我得到这个输出:

1 3 7
2 8 10
3 2 5
4 9 11
5 18 21

非常出乎意料。即使它们已排序(列),索引也会奇怪地增加 1。

我尝试这样设置索引:

   // Adding the indexes.
   for(int e = 0; e < boxnum; e++)
   {
       numberlist[e][0] = e + 104;
   }

但它以一种奇怪的方式递增:

104 3 7
105 8 10
106 2 5
107 9 11
108 18 21
107 106 105 104

你们能帮帮我吗?我真的很需要这个。

【问题讨论】:

  • sort(numberlist[i].begin(), numberlist[i].end()); 这个语句中的i 来自哪里?您需要发布MCVE。从您的预期输入和输出中也完全不清楚您正在尝试进行哪种排序,因为您似乎也在以某种方式转换数据(添加一个额外的列,其中看似任意值) ,并且您的过程描述不佳。

标签: c++ arrays sorting vector


【解决方案1】:

第一个排序,对每一行的列进行排序(我假设你每一行都以元素 0 开头,或者每一行都有 0 作为它的元素之一)。然后用递增的数字替换每行的第一个元素。第二次排序中的 lambda 函数是比较从第一列开始的向量,而不是从第二列开始。

我创建了一个更符合您的描述的示例。它将迭代器向量排序到二维向量的“行”(第一列不作为索引的向量向量)。首先对二维向量的列进行排序,然后对“行”的迭代器向量进行排序,而不实际对二维向量的行进行排序(如果需要,可以直接对二维向量进行排序)。

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <vector>

#define rows 5
#define columns 7

void displayvir(std::vector<std::vector<std::vector<int>>::iterator> &vir,
                std::vector<std::vector<int>> &vv)
{
std::vector<int>::iterator ic;
std::vector<std::vector<int>>::iterator ir;
    std::cout << std::endl;
    for(size_t i = 0; i < vir.size(); i++){
        ir = vir[i];
        std::cout << std::setw(3) << ir - vv.begin() << ':';
        for (ic = ir->begin(); ic < ir->end(); ic++){
            std::cout << std::setw(3) << *ic;
        }
        std::cout << std::endl;
    }
}

int main()
{
    // create vector of vectors (2d vector)
    std::vector<std::vector<int>> vv(rows, std::vector<int>(columns));
    // create iterator to column
    std::vector<int>::iterator ic;
    // create iterator to row
    std::vector<std::vector<int>>::iterator ir;
    // create vector of iterators to row
    std::vector<std::vector<std::vector<int>>::iterator> vir(rows);
    // initialize vv and vir
    for(ir = vv.begin(); ir < vv.end(); ir++){
        vir[ir-vv.begin()] = ir;
        for(ic = ir->begin(); ic < ir->end(); ic++){
            *ic = rand() % 10;
        }
    }
    displayvir(vir, vv);
    // Sort columns in each row
    for (ir = vv.begin(); ir < vv.end(); ir++)
        std::sort(ir->begin(), ir->end());
    displayvir(vir, vv);
    // Sort vir
    std::sort(vir.begin(), vir.end(),
        []( const std::vector<std::vector<int>>::iterator &a,
            const std::vector<std::vector<int>>::iterator &b)
            {return(*a < *b);});
    displayvir(vir, vv);
    return 0;
}

C 版:

#include <memory.h>
#include <stdio.h>
#include <stdlib.h>

#define rows 5
#define columns 7

void displayapr(int (*apr[rows])[columns], int a2d[rows][columns])
{
size_t i, j;
    printf("\n");
    for(j = 0; j < rows; j++){
        printf("%3d: ", apr[j]-a2d);
        for(i = 0; i < columns; i++){
            printf("%3d", (*apr[j])[i]);
        }
        printf("\n");
    }
}

int compareint(const int *pa, const int *pb)
{
    return(memcmp(pa, pb, sizeof(int)));
}

int comparerows(const int (**ppr0)[columns], const int (**ppr1)[columns])
{
const int *p0 = **ppr0;
const int *p1 = **ppr1;
size_t i;
    for(i = 0; i < columns; i++)
        if(p0[i] != p1[i])
            return p0[i] - p1[i];
    return 0;
}

int main()
{
int a2d[rows][columns];         /* 2d array */
int (*apr[rows])[columns];      /* array of ptrs to rows */
size_t i, j;

    /* init a2d, apr */
    for(j = 0; j < rows; j ++){
        apr[j] = &a2d[j];
        for(i = 0; i < columns; i++){
            a2d[j][i] = rand() % 10;
        }
    }
    displayapr(apr, a2d);
    /* Sort columns in each row of a2d */
    for(j = 0; j < rows; j ++)
        qsort(apr[j], columns, sizeof(int), compareint);
    displayapr(apr, a2d);
    /* Sort apr */
    qsort(apr, rows, sizeof(int *), comparerows);
    displayapr(apr, a2d);
    return 0;
}

【讨论】:

  • 谢谢伙计!我不是专业人士......所以你的代码让我有点困惑......你能想出更简单的代码吗?
  • @Boomrang - (注意 vv 是 v v,向量到向量,而不是字母 w,迭代器本质上是指针) vir 是 vv 中行的迭代器的向量 {iterator to row 0, iterator to第 1 行,... 到第 rows-1 行的迭代器}。表达式 (ir-vv.begin()) 将迭代器转换为 vv 中的行到普通索引 {0, 1, ... rows-1}。这可以使用 c 类型代码、使用 c 类型 2d 数组和指向整数的指针和/或指向行的指针来重做。
  • 谢谢你!欣赏它,伙计……但可以帮我把它变成 C 类型的……如果这对你来说不是问题……
  • @Boomrang - 我添加了一个 C 版本。在 C++ 示例中使用 vir 和在 C 示例中使用 apr 的原因是要排序的数组的元素需要是指向 2d 向量或数组的指针。另一种方法是将索引数组排序到二维数组的行,但比较参数不会有任何指向二维数组的指针,需要 C 版本的全局数组或包含二维数组的类和 C++ 版本的比较函数运算符(函子)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-07
  • 1970-01-01
  • 2016-04-25
  • 2013-10-21
  • 1970-01-01
  • 2021-10-01
  • 2022-01-22
相关资源
最近更新 更多