Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用C++;?_C++_Arrays_Sorting - Fatal编程技术网

C++ 如何使用C++;?

C++ 如何使用C++;?,c++,arrays,sorting,C++,Arrays,Sorting,我有3列整数数组,其最后2个元素用于排序。比如说 101 1102 12 1 2 1301 我希望他们成为: 101 1301 1102 12 1 2 数组首先根据第2列进行排序,然后再根据第3列进行排序 我有3000多行,所以我需要一些快速的东西。在C++中如何做到这一点? 注意:将使用以下模板动态分配阵列: template <typename T> T **AllocateDynamic2DArray(int nRows, int nCols){ T **dynamic

我有3列整数数组,其最后2个元素用于排序。比如说

101

1102

12 1 2

1301

我希望他们成为:

101

1301

1102

12 1 2

数组首先根据第2列进行排序,然后再根据第3列进行排序

我有3000多行,所以我需要一些快速的东西。在C++中如何做到这一点?

注意:将使用以下模板动态分配阵列:

template <typename T>
T **AllocateDynamic2DArray(int nRows, int nCols){
    T **dynamicArray;

    dynamicArray = new T*[nRows];
    for( int i = 0 ; i < nRows ; i++ ){
        dynamicArray[i] = new T[nCols];
        for ( int j=0; j<nCols;j++){
            dynamicArray[i][j]= 0;
        }
    }
    return dynamicArray;
}
模板
T**AllocatedDynamic2DArray(整数nRows,整数nCols){
T**卡雷;
dynamicArray=新的T*[nRows];
对于(int i=0;i对于(int j=0;j将其用于第二列,然后用于第三列。现在它适用于单尺寸阵列

   int *toplace(int *start, int *end)
{
    int *i = start+1, *j= end-1;

    while(i<=j)
    {
    while(*i<=*start && i<=j) {i++;}
    while(*j>=*start && i<=j) {j--;}    
    if (i<j) std::swap(*i++,*j--); 
    }

    std::swap(*start,*(i-1)); 
    return i-1;
}



void quicksort(int *start, int *end)
{
    if (start >= end) return;

    int *temp = start;
    temp = toplace(start,end);

    quicksort(start,temp);
    quicksort(temp+1,end);

}
int*toplace(int*start,int*end)
{
int*i=start+1,*j=end-1;

而(i将其用于第二列,然后用于第三列。现在它适用于单dim阵列

   int *toplace(int *start, int *end)
{
    int *i = start+1, *j= end-1;

    while(i<=j)
    {
    while(*i<=*start && i<=j) {i++;}
    while(*j>=*start && i<=j) {j--;}    
    if (i<j) std::swap(*i++,*j--); 
    }

    std::swap(*start,*(i-1)); 
    return i-1;
}



void quicksort(int *start, int *end)
{
    if (start >= end) return;

    int *temp = start;
    temp = toplace(start,end);

    quicksort(start,temp);
    quicksort(temp+1,end);

}
int*toplace(int*start,int*end)
{
int*i=start+1,*j=end-1;

while(i您可以使用
std::sort()
;但是,由于数组是二维的,所以这很复杂

通常,
std::sort()
不能吃2D数组;您必须创建一个类来转换编译器警告和投诉:

#include <iostream>
#include <algorithm>

int data[4][3] = {
    {10,0,1},
    {11,0,2},
    {12,1,2},
    {13,0,1}
};

struct row_t { // our type alias for sorting; we know this is compatible with the rows in data
    int data[3];
    bool operator<(const row_t& rhs) const {
        return (data[1]<rhs.data[1]) || ((data[1]==rhs.data[1]) && (data[2]<rhs.data[2]));
    }
};              

int main() {
    std::sort((row_t*)data,(row_t*)(data+4));
    for(int i=0; i<4; i++)
        std::cout << i << '=' << data[i][0] << ',' << data[i][1] << ',' << data[i][2] << ';' << std::endl; 
    return 0;
}
#包括
#包括
国际数据[4][3]={
{10,0,1},
{11,0,2},
{12,1,2},
{13,0,1}
};
struct row\u t{//我们用于排序的类型别名;我们知道这与数据中的行兼容
int数据[3];

bool操作符您可以使用
std::sort()
;但是,由于数组是二维的,这一点很复杂

通常,
std::sort()
不能吃2D数组;您必须创建一个类来转换编译器警告和投诉:

#include <iostream>
#include <algorithm>

int data[4][3] = {
    {10,0,1},
    {11,0,2},
    {12,1,2},
    {13,0,1}
};

struct row_t { // our type alias for sorting; we know this is compatible with the rows in data
    int data[3];
    bool operator<(const row_t& rhs) const {
        return (data[1]<rhs.data[1]) || ((data[1]==rhs.data[1]) && (data[2]<rhs.data[2]));
    }
};              

int main() {
    std::sort((row_t*)data,(row_t*)(data+4));
    for(int i=0; i<4; i++)
        std::cout << i << '=' << data[i][0] << ',' << data[i][1] << ',' << data[i][2] << ';' << std::endl; 
    return 0;
}
#包括
#包括
国际数据[4][3]={
{10,0,1},
{11,0,2},
{12,1,2},
{13,0,1}
};
struct row\u t{//我们用于排序的类型别名;我们知道这与数据中的行兼容
int数据[3];

bool操作符您可以使用冒泡排序算法执行此操作(http://en.wikipedia.org/wiki/Bubble_sort)

基本上遍历所有记录,将当前记录与下一个记录进行比较。如果当前记录的第二列较高,则交换这些记录。如果当前记录的第二列相等,但第三列较高,则也交换这些记录。

继续迭代,直到不再进行交换

要使用您的示例:

10 0 1
1102
12 1 2(与下一个互换)
1301

10 0 1
11 0 2(与下一个交换)
1301
12.12

10 0 1
1301
1102
12.12


完成了!

您可以使用气泡排序算法完成此操作(http://en.wikipedia.org/wiki/Bubble_sort)

基本上遍历所有记录,将当前记录与下一个记录进行比较。如果当前记录的第二列较高,则交换这些记录。如果当前记录的第二列相等,但第三列较高,则也交换这些记录。

继续迭代,直到不再进行交换

要使用您的示例:

10 0 1
1102
12 1 2(与下一个互换)
1301

10 0 1
11 0 2(与下一个交换)
1301
12.12

10 0 1
1301
1102
12.12


完成了!

好的,OP有一个三列整数数组,这不容易排序,因为您不能分配数组

一种选择是使用结构数组,其中结构包含每列一个元素,编写自定义比较例程并使用
std::sort

另一种选择是假装我们有这样一个结构数组,并使用
reinterpret\u cast
的邪恶,如下所示:

#include <algorithm>
#include <iostream>

struct elt_t
{
  int e0;
  int e1;
  int e2;
};

int
compare (const elt_t &a, const elt_t &b)
{
  if (a.e1 == b.e1)
    return a.e2 < b.e2;
  else
    return a.e1 < b.e1;
}

int a [10][3] = 
{
  { 10, 0, 1 },
  { 11, 0, 2 },
  { 12, 1, 2 },
  { 13, 0, 1 }
};


int
main ()
{
  std::sort (reinterpret_cast<elt_t *>(&a[0]),
             reinterpret_cast<elt_t *>(&a[4]), compare);

  int i, j;

  for (i = 0; i < 4; ++i)
    std::cout << a [i][0] << ", " << a [i][1] << ", " << a [i][2] << std::endl;

  return 0;
}

好的,OP有一个三列整数数组,这不容易排序,因为您不能分配数组

一种选择是使用结构数组,其中结构包含每列一个元素,编写自定义比较例程并使用
std::sort

另一种选择是假装我们有这样一个结构数组,并使用
reinterpret\u cast
的邪恶,如下所示:

#include <algorithm>
#include <iostream>

struct elt_t
{
  int e0;
  int e1;
  int e2;
};

int
compare (const elt_t &a, const elt_t &b)
{
  if (a.e1 == b.e1)
    return a.e2 < b.e2;
  else
    return a.e1 < b.e1;
}

int a [10][3] = 
{
  { 10, 0, 1 },
  { 11, 0, 2 },
  { 12, 1, 2 },
  { 13, 0, 1 }
};


int
main ()
{
  std::sort (reinterpret_cast<elt_t *>(&a[0]),
             reinterpret_cast<elt_t *>(&a[4]), compare);

  int i, j;

  for (i = 0; i < 4; ++i)
    std::cout << a [i][0] << ", " << a [i][1] << ", " << a [i][2] << std::endl;

  return 0;
}

我认为这应该奏效:

template<typename T>
struct compareRows {
    bool operator() (T * const & a, T * const & b) {
        if (a[1] == b[1])
            return a[2] < b[2];
        else
            return a[1] < b[1];
    }
};

std::sort(dynamicArray, dynamicArray+nrows, compareRows<int>());
模板
结构比较程序{
布尔运算符()(T*常数和a,T*常数和b){
如果(a[1]==b[1])
返回a[2]

使用functor实现行之间的比较。排序将获取指向每行开头的指针,并根据行的内容交换它们。行将保留在内存中的相同位置。

我认为这应该可以:

template<typename T>
struct compareRows {
    bool operator() (T * const & a, T * const & b) {
        if (a[1] == b[1])
            return a[2] < b[2];
        else
            return a[1] < b[1];
    }
};

std::sort(dynamicArray, dynamicArray+nrows, compareRows<int>());
模板
结构比较程序{
布尔运算符()(T*常数和a,T*常数和b){
如果(a[1]==b[1])
返回a[2]

使用函子实现行之间的比较。排序将获取指向每行开头的指针,并根据行的内容交换它们。行将保留在内存中的相同位置。

3000行非常小;您在传递到
std::sort
的比较器中执行的任何操作都将执行trick.在您的示例中,记录
10 0 1
13 0 1
根据排序条件比较相等。您已选择将它们按特定顺序放置在输出中,但尚未说明该顺序是如何选择的。
std::sort
不一定会按您选择的顺序放置。@Steve Jessop顺序不是imPoTANTION。你必须给出数组的类型签名,并描述如何分配它。使用QSUTE()可接受的C代替C++,但是它很容易,并且可以在C++代码上使用。