如何使用 C++ 创建双重排序整数数组?
我有 3 列整数数组,其最后 2 个元素用于排序。例如
10 0 1
11 0 2
12 1 2
13 0 1
我希望他们成为:
10 0 1
13 0 1
11 0 2
12 1 2
数组首先根据第二列排序,然后再次根据第三列排序。
我有超过 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;
}
in main,
int ** lineFilter = AllocateDynamic2DArray(2*numberOfLines,3);
I have 3-column integer arrays, whose last 2 elements are for sorting. For example
10 0 1
11 0 2
12 1 2
13 0 1
I want them to become:
10 0 1
13 0 1
11 0 2
12 1 2
The arrays are first sorted according to the 2nd column, and then again according to 3rd column.
I have over 3000 rows, so I need something also fast. How can you do this in c++?
Note: The array will be allocated dynamically using the following templates:
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;
}
in main,
int ** lineFilter = AllocateDynamic2DArray(2*numberOfLines,3);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 std::sort() ;但是,由于您的数组是二维的,这会变得复杂。
一般来说,
std::sort()
不能吃2D数组;您必须创建一个类来绕过编译器警告和投诉:如果您使用
std::vector
来保存真正属于row_t
类型的项目,则会变得容易得多> 或类似的。向量是动态调整大小和可排序的。you can use
std::sort()
; however, this is complicated by your array being 2D.In general,
std::sort()
can't eat 2D arrays; you have to create a class to cast around the compiler warnings and complaints:It becomes much easier if you use a
std::vector
to hold your items that really are of typerow_t
or such. Vectors are dynamically sized and sortable.我认为这应该可行:
使用函子来实现行之间的比较。排序将获取指向每行开头的指针,并根据行的内容交换它们。这些行将保留在内存中的相同位置。
I think this should work:
Use a functor to implement the comparison between the rows. The sort will take pointers to the beginning of each row and swap them according to the contents of the rows. The rows will stay in the same places in memory.
好的,OP有一个三列整数数组,这并不容易排序,因为你不能分配数组。
一种选择是使用结构数组,其中结构每一列包含一个元素,编写自定义比较例程并使用 std::sort。
另一种选择是假装我们有这样一个结构体数组,并利用
reinterpret_cast
的邪恶之处,如下所示:当然,这是否符合标准是非常有争议的: )
编辑:
由于动态分配矩阵的附加要求,您可以使用
std::vector
数组,或std:: 向量向量:
OK, the OP has a three-column integer arrays, which is not straightforward to sort, because you can't assign arrays.
One option is to have arrays of structs, where the struct contains one element for each column, write a custom compare routine and use
std::sort
.Another option is to pretend we have such an array of structs and employ the evilness of
reinterpret_cast
, like below:Of course, whether or not this is standards compliant is highly debatable :)
EDIT:
With the added requirement for the matrix to by dynamically allocated, you can use an array of
std::vector
, or a vector ofstd::vector
:将其用于第二列,然后用于第三列。现在它适用于单个暗淡阵列
use this for the second column and then for the third. Now it works for single dim arrays
您可以使用冒泡排序算法 (http://en.wikipedia.org/wiki/Bubble_sort)
基本上迭代所有记录,将当前记录与下一条记录进行比较。如果当前记录的第二列更高,则交换这些记录。如果当前记录的第二列相等,但第三列更高,则也交换。
继续迭代,直到不再进行交换。
使用您的示例:
10 0 1
11 0 2
12 1 2(与下一个交换)
13 0 1
10 0 1
11 0 2(与下一个交换)
13 0 1
12 1 2
10 0 1
13 0 1
11 0 2
12 1 2
完成了!
You can do this using the bubble sort algorithm (http://en.wikipedia.org/wiki/Bubble_sort)
Basically iterate through all records, comparing the current record, with the next. If the current record's 2nd column is higher then swap these records. If the current record's 2nd column is equal but the 3rd column is higher, then swap also.
Continue iterating until no more swaps are made.
To use your example:
10 0 1
11 0 2
12 1 2 (swap with next)
13 0 1
10 0 1
11 0 2(swap with next)
13 0 1
12 1 2
10 0 1
13 0 1
11 0 2
12 1 2
And done!