int delta[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
random_shuffle(&delta[0],&delta[4]);
编译错误:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h: In function 'void std::swap(_Tp&, _Tp&) [with _Tp = int [2]]':
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:127: instantiated from 'static void std::__iter_swap<true>::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = int (*)[2], _ForwardIterator2 = int (*)[2]]'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:163: instantiated from 'void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = int (*)[2], _ForwardIterator2 = int (*)[2]]'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:1906: instantiated from 'void std::random_shuffle(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = int (*)[2]]'main.cpp:150: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:97: error: array must be initialized with a brace-enclosed initializer
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:98: error: ISO C++ forbids assignment of arrays
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:99: error: ISO C++ forbids assignment of arrays
在Windows g ++上编译相同的代码。
答案 0 :(得分:2)
std::random_shuffle
通过交换工作。交换(通常)需要分配。但阵列不可分配。在C ++ 11中,对于不使用直接赋值的数组,存在std::swap
的特化。因此,您需要启用C ++ 11才能使代码正常工作。添加编译器选项-std=c++11
(如果您的gcc版本支持它)。