C++ 11 学习2:空指针(nullptr) 和 基于范围的for循环(Range-based for loops)

本文介绍了C++11中引入的两个重要特性:nullptr关键字和基于范围的for循环。nullptr将空指针与整数0的概念分离,增强了代码的安全性和清晰度。基于范围的for循环简化了容器和数组的遍历过程。

3.空指针(nullptr)

早在 1972 年,C语言诞生的初期,常数0带有常数及空指针的双重身分。 C 使用 preprocessor macroNULL 表示空指针, 让 NULL 及 0 分别代表空指针及常数 0。 NULL 可被定义为 ((void*)0) 或是 0

C++ 并不采用 C 的规则,不允许将 void* 隐式转换为其他类型的指针。 为了使代码 char* c = NULL; 能通过编译,NULL 只能定义为0。 这样的决定使得函数重载无法区分代码的语义:

void foo(char *);
void foo(int);

C++ 建议 NULL 应当定义为 0,所以foo(NULL); 将会调用 foo(int), 这并不是程序员想要的行为,也违反了代码的直观性。0 的歧义在此处造成困扰。

C++11 引入了新的关键字来代表空指针常数:nullptr,将空指针和整数 0 的概念拆开。 nullptr 的类型为nullptr_t,能隐式转换为任何指针或是成员指针的类型,也能和它们进行相等或不等的比较。 而nullptr不能隐式转换为整数,也不能和整数做比较。

为了向下兼容,0 仍可代表空指针常数。

char* pc = nullptr;     // OK
int * pi = nullptr;     // OK
int    i = nullptr;     // error
 
foo(nullptr);           // 呼叫 foo(char *)

 4.基于范围的for循环(Range-based for loops)

C++11给for循环定义了"range"的概念,这样可以使for循环可以使用类似java的简化的for循环,可以用于遍历数组,容器,string以及由begin和end函数定义的序列(有迭代器Iterator),示例代码如下:

int my_array[5] = {1, 2, 3, 4, 5};
for (int &x : my_array)
{
  x *= 2;
}
map<string, int> m{{"a", 1}, {"b", 2}, {"c", 3}};  
for (auto p : m){  
    cout<<p.first<<" : "<<p.second<<endl;  
} 

C++11使用":"来定义range的概念,":"左边的变量为被遍历序列的单个元素,":"右边的变量为序列本身。

 

转载于:https://www.cnblogs.com/andyidea/p/4785243.html

g++ snake.cpp -o snake -lncurses snake.cpp:7:0: warning: "KEY_UP" redefined [enabled by default] #define KEY_UP 65 ^ In file included from snake.cpp:1:0: /usr/include/curses.h:1410:0: note: this is the location of the previous definition #define KEY_UP 0403 /* up-arrow key */ ^ snake.cpp:8:0: warning: "KEY_DOWN" redefined [enabled by default] #define KEY_DOWN 66 ^ In file included from snake.cpp:1:0: /usr/include/curses.h:1409:0: note: this is the location of the previous definition #define KEY_DOWN 0402 /* down-arrow key */ ^ snake.cpp:9:0: warning: "KEY_RIGHT" redefined [enabled by default] #define KEY_RIGHT 67 ^ In file included from snake.cpp:1:0: /usr/include/curses.h:1412:0: note: this is the location of the previous definition #define KEY_RIGHT 0405 /* right-arrow key */ ^ snake.cpp:10:0: warning: "KEY_LEFT" redefined [enabled by default] #define KEY_LEFT 68 ^ In file included from snake.cpp:1:0: /usr/include/curses.h:1411:0: note: this is the location of the previous definition #define KEY_LEFT 0404 /* left-arrow key */ ^ snake.cpp: In member function ‘void SnakeGame::generate_food(): snake.cpp:25:23: error: ISO C++ forbids declaration of ‘p’ with no type [-fpermissive] for(auto& p : snake) ^ snake.cpp:25:27: error: range-basedforloops are not allowed in C++98 mode for(auto& p : snake) ^ snake.cpp:26:22: error: request for member ‘x’ in ‘p’, which is of non-class type ‘int’ if(p.x == food.x && p.y == food.y) continue; ^ snake.cpp:26:39: error: request for member ‘y’ in ‘p’, which is of non-class type ‘int’ if(p.x == food.x && p.y == food.y) continue; ^ snake.cpp: In constructor ‘SnakeGame::SnakeGame(): snake.cpp:33:20: error:nullptr’ was not declared in this scope srand(time(nullptr)); ^ snake.cpp:34:24: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default] snake.push_back({LINES/2, COLS/2}); ^ snake.cpp:34:42: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default] snake.push_back({LINES/2, COLS/2}); ^ snake.cpp: In member function ‘void SnakeGame::update(): snake.cpp:69:19: error: ISO C++ forbids declaration of ‘p’ with no type [-fpermissive] for(auto& p : snake) ^ snake.cpp:69:23: error: range-basedforloops are not allowed in C++98 mode for(auto& p : snake) ^ snake.cpp:70:18: error: request for member ‘x’ in ‘p’, which is of non-class type ‘int’ if(p.x == head.x && p.y == head.y) { ^ snake.cpp:70:35: error: request for member ‘y’ in ‘p’, which is of non-class type ‘int’ if(p.x == head.x && p.y == head.y) { ^ snake.cpp: In member function ‘void SnakeGame::draw(): snake.cpp:93:19: error: ISO C++ forbids declaration of ‘p’ with no type [-fpermissive] for(auto& p : snake) ^ snake.cpp:93:23: error: range-basedforloops are not allowed in C++98 mode for(auto& p : snake) ^ In file included from snake.cpp:1:0: snake.cpp:94:23: error: request for member ‘x’ in ‘p’, which is of non-class type ‘int’ mvaddch(p.x, p.y, '*'); ^ snake.cpp:94:28: error: request for member ‘y’ in ‘p’, which is of non-class type ‘int’ mvaddch(p.x, p.y, '*'); ^ snake.cpp: In function ‘int main(): snake.cpp:124:49: error: ‘class SnakeGame’ has no member named ‘get_score’ printf("Game Over! Final Score: %d\n", game.get_score()); ^ [root@localhost ~]#
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值