Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.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++;_C++_C_Sorting_For Loop - Fatal编程技术网

C++ 如何在C/C++;

C++ 如何在C/C++;,c++,c,sorting,for-loop,C++,C,Sorting,For Loop,在多次尝试使用sort但未成功后,下面是我的实际代码: #include <algorithm> #define N 5 int a[N] = { 3, 6, 2, 4, 1 }; int b[N] = { 6, 3, 1, 2, 9 }; int c[N][3]; for ( size_t i = 0; i < N; i++ ) { c[i][0] = a[i]; c[i][1] = b[i]; c[i][2

在多次尝试使用sort但未成功后,下面是我的实际代码:

#include <algorithm>
#define N   5


    int a[N] = { 3, 6, 2, 4, 1 }; 
    int b[N] = { 6, 3, 1, 2, 9 };
    int c[N][3];

    for ( size_t i = 0; i < N; i++ )
    {
        c[i][0] = a[i]; c[i][1] = b[i]; c[i][2] = i;
    }

    for ( size_t i = 0; i < N; i++ ) printf( "%d,%d, %d\n", c[i][0], c[i][1], c[i][2] ); 
我需要用第一个键进行排序,输出应该是这样的:

1,9,4
2,1,2
3,6,0
4,2,3
6,3,1

我尝试了一些绝望的
排序(c,c+N)

c-array是不可分配的,因此不满足
std::sort
要求

正如建议的那样,在C++11中,您可以使用
std::array

std::array<int, 3> c[5] = {
    {3, 6, 0},
    {6, 3, 1},
    {2, 1, 2},
    {4, 2, 3},
    {1, 9, 4}
};
std::sort(std::begin(c), std::end(c));
std::数组c[5]={
{3, 6, 0},
{6, 3, 1},
{2, 1, 2},
{4, 2, 3},
{1, 9, 4}
};
std::sort(std::begin(c),std::end(c));
使用qsort版本

#include <cstdio>
#include <cstdlib>

#define N   5

using namespace std;

int cmp(const void  *a, const void *b){
    int *x = (int*)a;
    int *y = (int*)b;
    return (*x > *y) - (*x < *y);
}

int main(){
    int a[N] = { 3, 6, 2, 4, 1 }; 
    int b[N] = { 6, 3, 1, 2, 9 };
    int c[N][3];

    for ( size_t i = 0; i < N; i++ )
    {
        c[i][0] = a[i]; c[i][1] = b[i]; c[i][2] = i;
    }
    qsort(c, N, sizeof(*c), cmp);
    for ( size_t i = 0; i < N; i++ ) printf( "%d,%d, %d\n", c[i][0], c[i][1], c[i][2] ); 
}
#包括
#包括
#定义n5
使用名称空间std;
int cmp(常数无效*a,常数无效*b){
int*x=(int*)a;
int*y=(int*)b;
返回(*x>*y)-(*x<*y);
}
int main(){
inta[N]={3,6,2,4,1};
intb[N]={6,3,1,2,9};
国际货币基金组织[N][3];
对于(大小i=0;i
向我们展示您如何尝试对数组进行排序,并解释为什么没有看到预期的结果。您的代码中没有排序的内容。这就是你的问题。为
qsort
编写比较函数,然后在C.i尝试排序(C,C+N)中使用
qsort
;我要编辑我的question@BLUEPIXY有什么例子吗?!您可以使用
std::vector
,或创建自己的类数组。@user4406273或使用
std::tr1::array
boost::array
#include <cstdio>
#include <cstdlib>

#define N   5

using namespace std;

int cmp(const void  *a, const void *b){
    int *x = (int*)a;
    int *y = (int*)b;
    return (*x > *y) - (*x < *y);
}

int main(){
    int a[N] = { 3, 6, 2, 4, 1 }; 
    int b[N] = { 6, 3, 1, 2, 9 };
    int c[N][3];

    for ( size_t i = 0; i < N; i++ )
    {
        c[i][0] = a[i]; c[i][1] = b[i]; c[i][2] = i;
    }
    qsort(c, N, sizeof(*c), cmp);
    for ( size_t i = 0; i < N; i++ ) printf( "%d,%d, %d\n", c[i][0], c[i][1], c[i][2] ); 
}