|
发表于 2020-10-25 00:52:50
|
显示全部楼层
本楼为最佳答案
本程序需要在DEV_C++的菜单--->工具--->编译选项--->编译时加入命令前打勾,并在下面的加入框中加入:-std=c++11
- #include <iostream>
- #include <iterator>
- #include <algorithm>
- void foo_c( int a[], size_t n )
- {
- int* q = a;
- for( int *p=a; p!=a+n; ++p )
- if( *p != 0 )
- *q++ = *p;
- for( ; q!=a+n; ++q )
- *q = 0;
- }
- template<typename T,size_t N> void foo_cpp( T (&a)[N] )
- {
- std::stable_partition( a, a+N, [](T n){return n>0;} );
- }
- template<typename T,size_t N> void print( const T (&a)[N] )
- {
- std::copy( a, a+N, std::ostream_iterator<T>(std::cout," ") );
- std::cout << std::endl;
- }
- int main( void )
- {
- {
- int nums[] = { 9,0,0,0,10,0,0,0,11,0,12,0,13,0,14,0,15,0,16,0 };
- foo_c( nums, sizeof(nums)/sizeof(*nums) );
- print( nums );
- }
- }
复制代码 |
|