- 论坛徽章:
- 1
|
- #include <iostream>
- #include <algorithm>
- #include <functional>
- #include <iterator>
- #include <vector>
- namespace facu
- {
- template <typename ElementType, size_t SIZE>
- bool arrayLessThan( ElementType (*first)[ SIZE ],
- ElementType (*second)[ SIZE ] )
- {
- return std::lexicographical_compare( *first, *first + SIZE,
- *second, *second + SIZE,
- std::less<ElementType>() );
- }
- }
- int main()
- {
- using namespace std;
- using namespace facu;
-
- int arrays[ 5 ][ 8 ] = {
- { 10, 20,-12, 23, 55, 60,-51, 19 },
- { 10, 20, 8, 10, 7, -7, 44, 55 },
- { 30, 41, 56, 61,103,-10, 0, 12 },
- { 8, 10, 99, 0, -5, 11, 88, 33 },
- { 10, 8, 11, -8, 0, 0, 77, 52 }
- };
-
- typedef int (*ArrayPointer)[ 8 ];
- vector<ArrayPointer> pointers;
- for( ArrayPointer begin = &arrays[ 0 ], end = begin + 5;
- begin != end; ++begin )
- pointers.push_back( begin );
- sort( pointers.begin(), pointers.end(),
- arrayLessThan<int,8> );
- for( int row = 0; row != 5; ++row )
- {
- copy( *pointers[ row ], *pointers[ row ] + 8,
- ostream_iterator<int>( cout, " " ) );
-
- cout << endl;
- }
-
- cout << endl;
-
- cout << "The largest one is "
- << distance( &arrays[ 0 ], pointers.back() )
- << "th element." << endl;
-
- cout << "The smallest one is "
- << distance( &arrays[ 0 ], pointers.front() )
- << "th element." << endl;
-
- }// end of function 'main'
复制代码 上面是别人用C++写的 , 但我想要用纯C写 , 不知道有人写过吗?
谢谢 |
|