免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: 〇〇
打印 上一主题 下一主题

[C++] c++11新功能测试 [复制链接]

论坛徽章:
0
21 [报告]
发表于 2012-09-10 15:40 |只看该作者
返回向量的函数

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. vector<string> process(string expected,string actual);
  5. vector<string> process(string expected,string actual)
  6. {
  7.     if (expected.empty())
  8.         return {};
  9.     else if (expected == actual)
  10.         return {"functionX", "okay"};
  11.     else
  12.         return {"functionX", expected, actual};
  13. }
  14. int main()
  15. {
  16. vector<string> v;
  17. string a{"1"};
  18. string b{"2"};
  19. v=process(a,b);
  20. for (auto i : v)
  21. cout <<i <<endl;
  22. //
  23. a="2";
  24. v=process(a,b);
  25. for (auto i : v)
  26. cout <<i <<endl;
  27. //
  28. a="";
  29. v=process(a,b);
  30. for (auto i : v)
  31. cout <<i <<endl;

  32. return 0;
  33. }
复制代码
C:\MinGW>g++ c12.cpp -std=c++11

C:\MinGW>a
functionX
1
2
functionX
okay

论坛徽章:
0
22 [报告]
发表于 2012-09-10 15:56 |只看该作者
成员为向量的向量的声明,最后2个>>中间不要空格

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. vector<string> process(string expected,string actual);
  5. vector<string> process(string expected,string actual)
  6. {
  7.     if (expected.empty())
  8.         return {};
  9.     else if (expected == actual)
  10.         return {"functionX", "okay"};
  11.     else
  12.         return {"functionX", expected, actual};
  13. }
  14. int main()
  15. {
  16. vector<string> v1,v2;
  17. string a{"1"};
  18. string b{"2"};
  19. v1=process(a,b);
  20. //
  21. a="2";
  22. v2=process(a,b);
  23. //
  24. vector<vector<string>> lines{v1,v2};
  25. for (auto i : lines)
  26. for (auto j : i)
  27. cout <<j <<endl;

  28. return 0;
  29. }
复制代码
C:\MinGW>g++ c13.cpp -std=c++11

C:\MinGW>a
functionX
1
2
functionX
okay

论坛徽章:
0
23 [报告]
发表于 2012-09-10 16:04 |只看该作者
本帖最后由 KCAaD17aqPXs0 于 2012-09-10 16:05 编辑

手头没有这么多编译器,楼主帮我测试一下这个函数在各个编译器下的表现:
  1. namespace one_of_private
  2. {

  3.     template< typename Predicate, typename First_Input_Iterator, typename ... Rest_Input_Iterators >
  4.     std::size_t _one_of_count( Predicate predict, First_Input_Iterator first_, First_Input_Iterator last_, Rest_Input_Iterators ... rest_ )
  5.     {
  6.         if ( first_ == last_ ) return 0;
  7.         return ( predict( *first_++, *rest_++...) ? 1 : 0 ) +
  8.                _one_of_count( predict, first_, last_, rest_...);
  9.     }

  10.     struct dummy{};

  11.     template<typename Predicate, typename ... Input_Iterators>
  12.     std::size_t rotate_one_of_count_impl( Predicate predict, dummy, Input_Iterators ... inputs )
  13.     {
  14.         return _one_of_count( predict, inputs ... );
  15.     }
  16.    
  17.     template<typename Anonymous_Arg1, typename ... Anonymous_Argn>
  18.     std::size_t rotate_one_of_count_impl( Anonymous_Arg1 arg1, Anonymous_Argn ... argn )
  19.     {
  20.         return rotate_one_of_count_impl( argn ..., arg1 );
  21.     }

  22. }//namespace one_of_private

  23.     //example:
  24.     //      int a[4] = { 1, 2, 3, 4 };
  25.     //      int A[4] = { 2, 4, 6, 8 };
  26.     //      bool b = one_of( a, a+4, A, [](int a_, int A_){ return a_+a_ == A_;} );
  27.     template<typename ... Input_Iterators_and_Predict>
  28.     bool one_of( Input_Iterators_and_Predict ... all_args )
  29.     {
  30.         static_assert( sizeof ... ( all_args ) > 2, "one_of requires at least 3 arguments" );
  31.         return 1 == one_of_private::rotate_one_of_count_impl( all_args ..., one_of_private::dummy() );
  32.     }

复制代码

论坛徽章:
0
24 [报告]
发表于 2012-09-10 16:14 |只看该作者
insert的返回值

  1. #include <iostream>
  2. #include <list>
  3. using namespace std;

  4. int main()
  5. {
  6. list<string> lst;
  7. string word;
  8. auto iter = lst.begin();
  9. while (cin >> word)
  10.    iter = lst.insert(iter, word);
  11. for(auto i:lst)
  12.    cout << i;
  13. return 0;
  14. }
复制代码
C:\MinGW>g++ c14.cpp -std=c++11

C:\MinGW>a
1 2 3

321^C

论坛徽章:
0
25 [报告]
发表于 2012-09-10 16:29 |只看该作者
shrink_to_fit回收空间

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;

  4. int main()
  5. {
  6. vector<int> ivec;

  7. cout << "ivec: size: " << ivec.size()
  8.      << " capacity: "  << ivec.capacity() << endl;

  9. for (vector<int>::size_type ix = 0; ix != 24; ++ix)
  10.      ivec.push_back(ix);

  11. cout << "ivec: size: " << ivec.size()
  12.      << " capacity: "  << ivec.capacity() << endl;

  13. ivec.shrink_to_fit(); // ask for the memory to be returned

  14. cout << "ivec: size: " << ivec.size()
  15.      << " capacity: "  << ivec.capacity() << endl;
  16. return 0;
  17. }
复制代码
C:\MinGW>g++ c15.cpp -std=c++11

C:\MinGW>a
ivec: size: 0 capacity: 0
ivec: size: 24 capacity: 32
ivec: size: 24 capacity: 24

论坛徽章:
0
26 [报告]
发表于 2012-09-10 16:39 |只看该作者
stod失败

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;

  4. int main()
  5. {

  6.   string s2 = "pi = 3.14";

  7.   double d = std::stod(s2.substr(s2.find_first_of("+-.0123456789")));

  8.   cout<< d << endl;

  9.   return 0;
  10. }
复制代码
C:\MinGW>g++ c16.cpp -std=c++11
c16.cpp: 在函数‘int main()’中:
c16.cpp:10:63: 错误:‘stod’在此作用域中尚未声明

加了std::后
C:\MinGW>g++ c16.cpp -std=c++11
c16.cpp: 在函数‘int main()’中:
c16.cpp:10:14: 错误:‘stod’不是‘std’的成员

论坛徽章:
0
27 [报告]
发表于 2012-09-10 19:13 |只看该作者
KCAaD17aqPXs0 发表于 2012-09-10 16:04
手头没有这么多编译器,楼主帮我测试一下这个函数在各个编译器下的表现:

你加个main函数和#include<>

论坛徽章:
0
28 [报告]
发表于 2012-09-10 19:24 |只看该作者
vc2012对头文件的要求比gcc4.7严格,所有用到string的地方都要#include <string>
{}初始化基本不能用,唯一比gcc先进的是stod函数

C:\>cl c10.cpp /EHsc
用于 x86 的 Microsoft (R) C/C++ 优化编译器 17.00.50727.1 版版权所有(C) Microsoft
Corporation。保留所有权利。

c10.cpp
c10.cpp(7) : error C2601: “v”: 本地函数定义是非法的
        c10.cpp(6): 此行有一个“{”没有匹配项
c10.cpp(7) : error C2143: 语法错误 : 缺少“;”(在“}”的前面)
c10.cpp( : error C2065: “v”: 未声明的标识符

C:\>cl c11.cpp /EHsc
用于 x86 的 Microsoft (R) C/C++ 优化编译器 17.00.50727.1 版版权所有(C) Microsoft
Corporation。保留所有权利。

c11.cpp
Microsoft (R) Incremental Linker Version 11.00.50727.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:c11.exe
c11.obj

C:\>c11
0
1
2
3
4


C:\>cl c12.cpp /EHsc
用于 x86 的 Microsoft (R) C/C++ 优化编译器 17.00.50727.1 版版权所有(C) Microsoft
Corporation。保留所有权利。

c12.cpp
c12.cpp(9) : error C2059: 语法错误:“{”
c12.cpp(9) : error C2143: 语法错误 : 缺少“;”(在“{”的前面)
c12.cpp(10) : error C2181: 没有匹配 if 的非法 else
c12.cpp(11) : error C2059: 语法错误:“{”
c12.cpp(11) : error C2143: 语法错误 : 缺少“;”(在“{”的前面)
c12.cpp(11) : error C2143: 语法错误 : 缺少“;”(在“}”的前面)
c12.cpp(12) : error C2181: 没有匹配 if 的非法 else
c12.cpp(13) : error C2059: 语法错误:“{”
c12.cpp(13) : error C2143: 语法错误 : 缺少“;”(在“{”的前面)
c12.cpp(13) : error C2143: 语法错误 : 缺少“;”(在“}”的前面)
c12.cpp(1 : error C2601: “a”: 本地函数定义是非法的
        c12.cpp(16): 此行有一个“{”没有匹配项
c12.cpp(1 : error C2143: 语法错误 : 缺少“;”(在“}”的前面)
c12.cpp(19) : error C2470: “b”: 看起来像函数定义,但没有参数列表;跳过明显的函
数体
c12.cpp(20) : error C2065: “v”: 未声明的标识符
c12.cpp(20) : error C2065: “a”: 未声明的标识符
c12.cpp(20) : error C2065: “b”: 未声明的标识符
c12.cpp(21) : error C2065: “v”: 未声明的标识符

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int main()
  5. {

  6. string a{"1"};
  7. string b{"2"};
  8. vector<string> v1{a,b};
  9. //
  10. a="2";
  11. vector<string> v2{a,b};
  12. //
  13. vector<vector<string>> lines{v1,v2};
  14. for (auto i : lines)
  15. for (auto j : i)
  16. cout <<j <<endl;

  17. return 0;
  18. }
复制代码
C:\>cl c13.cpp /EHsc
用于 x86 的 Microsoft (R) C/C++ 优化编译器 17.00.50727.1 版版权所有(C) Microsoft
Corporation。保留所有权利。

c13.cpp
c13.cpp(7) : error C2601: “a”: 本地函数定义是非法的
        c13.cpp(5): 此行有一个“{”没有匹配项
c13.cpp(7) : error C2143: 语法错误 : 缺少“;”(在“}”的前面)
c13.cpp( : error C2470: “b”: 看起来像函数定义,但没有参数列表;跳过明显的函
数体
c13.cpp(9) : error C2470: “v1”: 看起来像函数定义,但没有参数列表;跳过明显的函
数体
c13.cpp(11) : error C2065: “a”: 未声明的标识符
c13.cpp(12) : error C2470: “v2”: 看起来像函数定义,但没有参数列表;跳过明显的
函数体
c13.cpp(14) : error C2470: “lines”: 看起来像函数定义,但没有参数列表;跳过明显
的函数体
c13.cpp(15) : error C2065: “lines”: 未声明的标识符



C:\>cl c14.cpp /EHsc
用于 x86 的 Microsoft (R) C/C++ 优化编译器 17.00.50727.1 版版权所有(C) Microsoft
Corporation。保留所有权利。

c14.cpp
Microsoft (R) Incremental Linker Version 11.00.50727.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:c14.exe
c14.obj

C:\>c14
1 2 3


321

C:\>cl c15.cpp /EHsc
用于 x86 的 Microsoft (R) C/C++ 优化编译器 17.00.50727.1 版版权所有(C) Microsoft
Corporation。保留所有权利。

c15.cpp
Microsoft (R) Incremental Linker Version 11.00.50727.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:c15.exe
c15.obj

C:\>c15
ivec: size: 0 capacity: 0
ivec: size: 24 capacity: 28
ivec: size: 24 capacity: 24

C:\>cl c16.cpp /EHsc
用于 x86 的 Microsoft (R) C/C++ 优化编译器 17.00.50727.1 版版权所有(C) Microsoft
Corporation。保留所有权利。

c16.cpp
Microsoft (R) Incremental Linker Version 11.00.50727.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:c16.exe
c16.obj

C:\>c16
3.14

不加std::
C:\>cl c16.cpp /EHsc
用于 x86 的 Microsoft (R) C/C++ 优化编译器 17.00.50727.1 版版权所有(C) Microsoft
Corporation。保留所有权利。

c16.cpp
Microsoft (R) Incremental Linker Version 11.00.50727.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:c16.exe
c16.obj

C:\>c16
3.14

论坛徽章:
0
29 [报告]
发表于 2012-09-11 19:14 |只看该作者
〇〇 发表于 2012-09-10 19:13
你加个main函数和#include


这个:
  1. #include <cstddef>
  2. #include <iostream>

  3. namespace one_of_private
  4. {

  5.     template< typename Predicate, typename First_Input_Iterator, typename ... Rest_Input_Iterators >
  6.     std::size_t _one_of_count( Predicate predict, First_Input_Iterator first_, First_Input_Iterator last_, Rest_Input_Iterators ... rest_ )
  7.     {
  8.         if ( first_ == last_ ) return 0;
  9.         return ( predict( *first_++, *rest_++...) ? 1 : 0 ) +
  10.                _one_of_count( predict, first_, last_, rest_...);
  11.     }

  12.     struct dummy{};

  13.     template<typename Predicate, typename ... Input_Iterators>
  14.     std::size_t rotate_one_of_count_impl( Predicate predict, dummy, Input_Iterators ... inputs )
  15.     {
  16.         return _one_of_count( predict, inputs ... );
  17.     }
  18.    
  19.     template<typename Anonymous_Arg1, typename ... Anonymous_Argn>
  20.     std::size_t rotate_one_of_count_impl( Anonymous_Arg1 arg1, Anonymous_Argn ... argn )
  21.     {
  22.         return rotate_one_of_count_impl( argn ..., arg1 );
  23.     }

  24. }//namespace one_of_private

  25. //example:
  26. //      int a[4] = { 1, 2, 3, 4 };
  27. //      int A[4] = { 2, 4, 6, 8 };
  28. //      bool b = one_of( a, a+4, A, [](int a_, int A_){ return a_+a_ == A_;} );
  29. template<typename ... Input_Iterators_and_Predict>
  30. bool one_of( Input_Iterators_and_Predict ... all_args )
  31. {
  32.     static_assert( sizeof ... ( all_args ) > 2, "one_of requires at least 3 arguments" );
  33.     return 1 == one_of_private::rotate_one_of_count_impl( all_args ..., one_of_private::dummy() );
  34. }

  35. int main()
  36. {
  37.     int a[4] = { 1, 2, 3, 4 };
  38.     int b[4] = { 2, 3, 4, 5 };
  39.     int c[4] = { 3, 4, 5, 6 };
  40.     int d[4] = { 4, 5, 6, 8 };

  41.     bool bo = one_of( a, a+4, b, c, d, []( int a_, int b_, int c_, int d_ ) { return b_+c_ != a_ + d_; } );

  42.     if ( bo ) std::cout << "true\n";
  43.     else std::cout << "false\n";

  44.     return 0;
  45. }
复制代码
谢了

论坛徽章:
0
30 [报告]
发表于 2012-09-11 20:46 |只看该作者
KCAaD17aqPXs0 发表于 2012-09-11 19:14
这个:谢了

C:\>cl test.cpp /EHsc
用于 x86 的 Microsoft (R) C/C++ 优化编译器 17.00.50727.1 版版权所有(C) Microsoft
Corporation。保留所有权利。

test.cpp
test.cpp(7) : error C2143: 语法错误 : 缺少“,”(在“...”的前面)
test.cpp( : error C2061: 语法错误: 标识符“Rest_Input_Iterators”
test.cpp(17) : error C2143: 语法错误 : 缺少“,”(在“...”的前面)
test.cpp(1 : error C2061: 语法错误: 标识符“Input_Iterators”
test.cpp(23) : error C2143: 语法错误 : 缺少“,”(在“...”的前面)
test.cpp(24) : error C2061: 语法错误: 标识符“Anonymous_Argn”
test.cpp(35) : error C2143: 语法错误 : 缺少“,”(在“...”的前面)
test.cpp(36) : error C2065: “Input_Iterators_and_Predict”: 未声明的标识符
test.cpp(36) : error C2143: 语法错误 : 缺少“)”(在“...”的前面)
test.cpp(36) : error C2059: 语法错误:“)”
test.cpp(43) : error C2143: 语法错误 : 缺少“;”(在“{”的前面)
test.cpp(43) : error C2447: “{”: 缺少函数标题(是否是老式的形式表?)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP