intmain() { string name; string address; cout << "Please input your name: "; getline(cin, name); cout << "Please input your address: "; getline(cin, address); cout << "Your name is " << name << " and address is " << address << endl; return0; }
执行程序:
1 2 3 4 5 6
(base) phoenine@EvandeMacBook-Pro C_Study_1 % g++ -std=c++17 cpp_chapter_2.cpp -o test (base) phoenine@EvandeMacBook-Pro C_Study_1 % ./test Please input your name: Evan Yang Please input your address: Comba Street 64, cot Your name is Evan Yang and address is Comba Street 64, cot (base) phoenine@EvandeMacBook-Pro C_Study_1 %
intmain() { int n_int = INT_MAX; short n_short = SHRT_MAX; long n_long = LONG_MAX; longlong n_llong = LLONG_MAX;
cout << "int is " << sizeof(int) << " bytes" << endl; cout << "short is " << sizeof(short) << " bytes" << endl; cout << "long is " << sizeof(long) << " bytes" << endl; cout << "long long is " << sizeof(longlong) << " bytes" << endl;
cout << "int is " << n_int << endl; cout << "short is " << n_short << endl; cout << "long is " << n_long << endl; cout << "long long is " << n_llong << endl; return0; }
输出:
1 2 3 4 5 6 7 8 9 10
(base) phoenine@EvandeMacBook-Pro C_Study_1 % ./test int is 4 bytes short is 2 bytes long is 8 bytes long long is 8 bytes int is 2147483647 short is 32767 long is 9223372036854775807 long long is 9223372036854775807 (base) phoenine@EvandeMacBook-Pro C_Study_1 %
3.2. 整型的字面值
C++能以三种不同的计数方式来书写整数:
基数为10
基数为8(老式UNIX版本):0
基数为16(硬件黑客的最爱): 0x或者0X
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<iostream>
usingnamespace std;
intmain() { int chest = 42; int waist = 42; int inseam = 42;
constint code = 66; int x = 66; char c1 {31325}; // narrowing, not allowed char c2 {66}; //allowed because char can hold 66 char c3 {code}; //ditto char c4 = {x}; // not allowed, cause x is not constant x = 32325; char c5 = x; //allowed by this form of initialization
编译:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
(base) phoenine@EvandeMacBook-Pro C_Study_1 % g++ -std=c++17 cpp_chapter_2.cpp -o test cpp_chapter_2.cpp:9:14: error: constant expression evaluates to 31325 which cannot be narrowed to type 'char' [-Wc++11-narrowing] char c1 {31325}; // narrowing, not allowed ^~~~~ cpp_chapter_2.cpp:9:14: note: insert an explicit cast to silence this issue char c1 {31325}; // narrowing, not allowed ^~~~~ static_cast<char>( ) cpp_chapter_2.cpp:12:16: error: non-constant-expression cannot be narrowed from type 'int' to 'char' in initializer list [-Wc++11-narrowing] char c4 = {x}; // not allowed, cause x is not constant ^ cpp_chapter_2.cpp:12:16: note: insert an explicit cast to silence this issue char c4 = {x}; // not allowed, cause x is not constant ^ static_cast<char>( ) cpp_chapter_2.cpp:9:14: warning: implicit conversion from 'int' to 'char' changes value from 31325 to 93 [-Wconstant-conversion] char c1 {31325}; // narrowing, not allowed ~^~~~~ 1 warning and 2 errors generated.
3.4. auto声明
C++11新增一个工具,让编译器能够根据初始化值的类型推断变量的类型。重新定义了auto的含义。
auto n = 100 // n is int
auto x = 1.5; // x is double
auto y = 1.3e12L // y is long double
处理复杂类型,比如STL中的类型时,自动类型的优势才能显现出来:
1 2
std::vector<double> scores; auto pv = scores.begin()
(base) phoenine@EvandeMacBook-Pro C_Study_1 % g++ -std=c++17 cpp_chapter_2.cpp -o test cpp_chapter_2.cpp:7:29: error: type 'double' cannot be narrowed to 'long' in initializer list [-Wc++11-narrowing] long plifs[] = {25, 29, 3.0}; ^~~ cpp_chapter_2.cpp:7:29: note: insert an explicit cast to silence this issue long plifs[] = {25, 29, 3.0}; ^~~ static_cast<long>( ) cpp_chapter_2.cpp:8:32: error: constant expression evaluates to 1122011 which cannot be narrowed to type 'char' [-Wc++11-narrowing] char slifs[4] = {'h', 'i', 1122011, '\0'}; ^~~~~~~ cpp_chapter_2.cpp:8:32: note: insert an explicit cast to silence this issue char slifs[4] = {'h', 'i', 1122011, '\0'}; ^~~~~~~ static_cast<char>( ) cpp_chapter_2.cpp:8:32: warning: implicit conversion from 'int' to 'char' changes value from 1122011 to -37 [-Wconstant-conversion] char slifs[4] = {'h', 'i', 1122011, '\0'}; ~ ^~~~~~~ 1 warning and 2 errors generated.
cout << "Enter your name: \n"; cin.get(name ,ArSize).get(); cout << "Enter your favorite dessert: \n"; cin.get(dessert, ArSize).get(); cout << "I have some delicious " << dessert; cout << " for you, " << name << ".\n"; return0; }
输出:
1 2 3 4 5 6 7
(base) phoenine@EvandeMacBook-Pro C_Study_1 % g++ -std=c++17 cpp_chapter_2.cpp -o test (base) phoenine@EvandeMacBook-Pro C_Study_1 % ./test Enter your name: Evan Yang Enter your favorite dessert: Chocolate Mouses I have some delicious Chocolate Mouses for you, Evan Yang.
ofstream outFile; outFile.open("carinfo.txt"); cout << "Enter the mark and model of automobile: "; cin.getline(automobile, 50); cout << "Enter the model year: "; cin >> year; cout << "Enter the original asking price: "; cin >> a_price; d_price = DISCOUNT * a_price; cout << fixed; //表示用一般的方式输出浮点数,而不是科学计数法 cout.precision(2); //表示浮点数的精度 cout.setf(ios_base::showpoint); //强制显示浮点数小数点后的0 cout << "Make the model: " << automobile << endl; cout << "Year: " << year << endl; cout << "Was asking: " << a_price << endl; cout << "Now asking: " << d_price << endl; outFile << fixed; outFile.precision(2); outFile.setf(ios_base::showpoint); outFile << "Make the model: " << automobile << endl; outFile << "Year: " << year << endl; outFile << "Was asking: " << a_price << endl; outFile << "Now asking: " << d_price << endl; outFile.close(); return0; }
输出:
1 2 3 4 5 6 7 8 9
(base) phoenine@EvandeMacBook-Pro C_Study_1 % g++ -std=c++17 cpp_chapter_2.cpp -o test (base) phoenine@EvandeMacBook-Pro C_Study_1 % ./test Enter the mark and model of automobile: Flitz Perky Enter the model year: 2011 Enter the original asking price: 15600 Make the model: Flitz Perky Year: 2011 Was asking: 15600.00 Now asking: 14242.80
intmain() { char fileName[SIZE]; ifstream inFile; cout << "Enter name of data file: "; cin.getline(fileName, SIZE); inFile.open(fileName); if (!inFile.is_open()) { cout << "Could not open the file " << fileName << endl; cout << "Program terminating.\n"; exit(EXIT_FAILURE); } double value; double sum = 0.0; double count = 0.0; inFile >> value; //方法good()指出最后一次读取输入的操作是否成功 while (inFile.good()) { ++count; sum += value; inFile >> value; } if (inFile.eof()) { cout << "End of file reached.\n"; } elseif (inFile.fail()) { cout << "Input terminted by data mismatch.\n"; } else { cout << "Input terminated for unknown reason.\n"; } if (0 == count) { cout << "No data processed.\n"; } else { cout << "items read: " << count << endl; cout << "Sum: " << sum << endl; cout << "Average: " << sum / count << endl; } inFile.close(); return0; }
输出:
1 2 3 4 5 6
(base) phoenine@EvandeMacBook-Pro C_Study_1 % ./test Enter name of data file: carinfo.txt End of file reached. items read: 5 Sum: 38810.7 Average: 7762.14
intmain() { usingnamespace std; double a, b; double c = 13.0; a = square(5.0); b = square(a + 7.5); cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c square is: " << square(c++) << endl; return0; }
输出:
1 2 3 4 5
(base) phoenine@EvandeMacBook-Pro C_Study_1 % g++ -std=c++17 cpp_chapter_4.cpp -o test (base) phoenine@EvandeMacBook-Pro C_Study_1 % ./test a = 25 b = 1056.25 c square is: 169
namespace Jill { doublebucket(double n); double fetch; structHill {...}; } char fetch; intmain(){ using Jill::fetch; cin >> fetch; //read a value into Jill::fetch cin >> ::fetch; //read a value into global fetch }