C++中的typeof与auto
typeof
/* * File Name: test_typeof.cpp * Operating Environment: ubuntu linux * Author: Jiang * Email: ligelaige@gmail.com * Created Time: 2014年04月25日 星期五 18时26分58秒 */ #include <iostream> using namespace std; void out_line(int n = 0, string outstr = "-") { for (int i = 0; i < n; ++i) { cout << outstr; } cout << endl; } int main (int argc, char const* argv[]) { int f1 = 0; typeof(f1) f2 = 1; typeof(f1) f = f1 + f2; for (int i = 0; i < 10; ++i) { f = f1 + f2; f1 = f2; f2 = f; out_line(f, "*"); } return 0; }
编译与运行:
➜ 25.4.2014 g++ test_typeof.cpp ➜ 25.4.2014 ./a.out * ** *** ***** ******** ************* ********************* ********************************** ******************************************************* ***************************************************************************************** ➜ 25.4.2014
auto
/* * File Name: test_typeof.cpp * Operating Environment: ubuntu linux * Author: Jiang * Email: ligelaige@gmail.com * Created Time: 2014年04月25日 星期五 18时26分58秒 */ #include <iostream> using namespace std; void out_line(int n = 0, string outstr = "-") { for (int i = 0; i < n; ++i) { cout << outstr; } cout << endl; } int main (int argc, char const* argv[]) { int f1 = 0; auto f2 = 1; auto f = f1 + f2; for (int i = 0; i < 10; ++i) { f = f1 + f2; f1 = f2; f2 = f; out_line(f, "*"); } return 0; }
编译与运行:
➜ 25.4.2014 g++ test_typeof.cpp -std=c++11 ➜ 25.4.2014 ./a.out * ** *** ***** ******** ************* ********************* ********************************** ******************************************************* ***************************************************************************************** ➜ 25.4.2014

浙公网安备 33010602011771号