Chinaunix首页 | 论坛 | 博客
  • 博客访问: 34470
  • 博文数量: 18
  • 博客积分: 485
  • 博客等级: 下士
  • 技术积分: 125
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-28 11:55
文章分类
文章存档

2011年(18)

我的朋友

分类: C/C++

2011-05-11 14:50:52

看到新闻后很纠结,犹豫要不要跟进,因为最近几年都一直挺简单就是美。一、C++0x的新特性C++0x有着一系列新特性,以下3个我个人比较中意
1. lambda表达式
[](int x, int y) { return x + y; }

2. auto类型推导
auto plus = [](int x, int y) { return x + y; }

3. 串型初使化
vector v={1,2,3,4,5}

上述三个特性的测试,gcc 4.5.2 下编译通过
  1.     #include <vector>
  2.     #include <iostream>
  3.     #include <algorithm>
  4.     #include <iterator>
  5.     using namespace std;
  6.       
  7.     ostream & operator <<(ostream &out, const vector<int> &numbers)
  8.     {
  9.         copy(numbers.begin(), numbers.end(), ostream_iterator<int>(out, " "));
  10.         return out;
  11.     }
  12.     int main(int argc, char *argv[])
  13.     {
  14.         vector<int> numbers = {1, 2, 3, 4, 5};
  15.         cout << numbers << endl;
  16.       
  17.         transform(numbers.begin(), numbers.end(), numbers.begin(), [](int x) { return x *= 2; });
  18.         cout << numbers << endl;
  19.       
  20.         auto increase = [](int x) {
  21.             return x+1;
  22.         };
  23.         transform(numbers.begin(), numbers.end(), numbers.begin(), increase);
  24.         cout << numbers << endl;
  25.       
  26.         return 0;
  27.     }
存为main.cc 编译并运行
  1. $ g++ -std=c++0x main.cc && ./a.out
  2. 1 2 3 4 5
  3. 2 4 6 8 10
  4. 3 5 7 9 11
lambda的引入,大大增加了代码的灵活性。

另一个例子 boost::asio 与 C++0x二、C++0x的缺点1、编译速度慢,但至少比boost模拟要快。
2、代码移植性差。三、相关链接

阅读(189) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~