Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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++_Sorting_Multidimensional Array_Bubble Sort - Fatal编程技术网

C++ 排序字符C+的二维数组+;

C++ 排序字符C+的二维数组+;,c++,sorting,multidimensional-array,bubble-sort,C++,Sorting,Multidimensional Array,Bubble Sort,我有一个2d字符数组,在每一行中我存储一个名称。。。例如: J O H N P E T E R S T E P H E N A R N O L D J A C K 我应该如何对数组进行排序,以便最终得到 A R N O L D J A C K J O H N P E T E R S T E P H E N 这是一个2d字符数组。。。。。没有字符串或字符点 不要冒泡排序-点1 第二点: 比较每个子数组的第一个字符(即数组[x][0]),如果需要移位,则使用while循环移位子数组x中的所有字符

我有一个2d字符数组,在每一行中我存储一个名称。。。例如:

J O H N
P E T E R
S T E P H E N
A R N O L D
J A C K
我应该如何对数组进行排序,以便最终得到

A R N O L D
J A C K
J O H N
P E T E R
S T E P H E N

这是一个2d字符数组。。。。。没有字符串或字符点

不要冒泡排序-点1

第二点:

比较每个子数组的第一个字符(即数组[x][0]),如果需要移位,则使用while循环移位子数组x中的所有字符…或保存子数组并将其按此方式移位

#define MAX_NAME 8

char names[][MAX_NAME] = {"JOHN", "PETER", "STEPHEN", "ARNOLD", "JACK"};
 // strcmp is really (int (*)(const char *, const char *)), so we cast.
qsort(names, sizeof(names) / MAX_NAME, MAX_NAME, 
  (int (*)(const void *, const void *)) strcmp);

请注意,这可能不是冒泡排序

C++不支持复制或比较C样式数组,但它支持在包装非常薄的C样式数组上执行此类操作。尝试
boost::array
,它与C++0x中的
tr1::array
std::array
相同

或者,您可以自己滚动:

#include <algorithm>

template< class T, size_t s >
struct array {
    T arr[s]; // public data, no destructor, inheritance, virtuals, etc
              // => type is aggregate
    operator T const *() const { return arr; }
    operator T *() { return arr; } // as close as we can get to array emulation

    friend bool operator< ( array const &l, array const &r )
        { return std::lexicographical_compare( l, l+s, r, r+s ); }
};

array< char, 10 > names[] // aggregate initialization — this is standard C++
    = { "JOHN", "PETER", "ARNOLD", "JACK" };

#include <iostream>
using namespace std;

int main() {
sort( names, names + sizeof names / sizeof *names );

for ( array<char,10> *s = names; s != names + sizeof names/sizeof*names; ++ s )
    cerr << *s << endl;
}

字符串数组和字符数组之间的最大区别是什么?排序算法也应该在这方面起作用。只需将数组洗牌,而不是交换两个字符串的位置。或者指针。好吧,总得有人说出来。不要以这种方式存储字符串。错误:从'int()(const char,const char*)throw()'到'int()(const void,const void*)的转换无效。|我添加了一个cast,可以解决这个错误。基本上,strcmp接受
const char*
,而qsort需要一个接受
const void*
的函数。我现在没有收到任何错误,但是当我打印数组时,它会执行一些bc操作,什么都不会被打印。但是在调用这个方法之前,所有的东西都被打印出来了,我做了一个简单的例子。它显示它在打印之前和之后。是的,但这是一个字符指针数组。。。。。我有一个2d字符数组,所以这真的不起作用。
template< class T, size_t s >
array< T, s > &wrap_arr( T (&a)[s] ) {
    return reinterpret_cast< array<T,s> & >( a );

        // make sure the compiler isn't really wacky...
        // I would call this optional:
    BOOST_STATIC_ASSERT( sizeof( T[s] ) == sizeof( array<T,s> ) );
}

char names_c[][10] // or whatever C input from wherever
    = { "JOHN", "PETER", "ARNOLD", "JACK" };

array<char, 10> *names = &wrap_arr( names_c[0] );