练习3.1
#include <iostream> using namespace std; int main() { int sum = 0, val = 50; while (val <= 100) { sum += val; ++val; } cout << "Sum of 50 to 100 inclusive is " << sum << std::endl; return 0; }
#include <iostream> using namespace std; int main() { int val = 10; while (val >= 0) { cout << val-- << std::endl; } return 0; }
#include <iostream> using namespace std; int main() { cout << "Please enter the two integers" << endl; int start, end; //我这里没有对传入格式进行判断,如果大家有需要可以添加这个功能 cin >> start >> end; if (start > end) { swap(start, end); } while (start <= end) { cout << start++ << endl; } return 0; }
#include<iostream> using namespace std; struct Sales_data { string bookNo; unsigned units_sold = { 0 }; double revenue = { 0.0 }; }; int main() { Sales_data data; double price; while (cin >> data.bookNo >> data.units_sold >> price) { data.revenue = data.units_sold * price; cout << "Record:" << data.bookNo << " " << data.units_sold << " " << data.revenue << endl; } }
#include <iostream> using namespace std; struct Sales_data { string bookNo; unsigned units_sold = { 0 }; double revenue = { 0.0 }; }; int main() { Sales_data data1, data2; double price; if (cin >> data1.bookNo >> data1.units_sold >> price) { data1.revenue = data1.units_sold * price; } if (cin >> data2.bookNo >> data2.units_sold >> price) { data2.revenue = data2.units_sold * price; } if (data1.bookNo == data2.bookNo) { unsigned totalCnt = data1.units_sold + data2.units_sold; double totalRevenue = data1.revenue + data2.revenue; cout << data1.bookNo << " " << totalCnt << " " << totalRevenue << endl; return 0; } else { return -1; } }
#include <iostream> using namespace std; struct Sales_data { string bookNo; unsigned units_sold = { 0 }; double revenue = { 0.0 }; }; int main() { Sales_data data1, data2; unsigned totalCnt = 0; double totalRevenue = 0.0; double price; while (cin >> data1.bookNo >> data1.units_sold >> price) { data1.revenue = data1.units_sold * price; totalCnt += data1.units_sold; totalRevenue += data1.revenue; } cout << data1.bookNo << " " << totalCnt << " " << totalRevenue << endl; return 0; }
#include <iostream> using namespace std; struct Sales_data { string bookNo; unsigned units_sold = { 0 }; double revenue = { 0.0 }; }; int main() { Sales_data data1, data2; double price; if (cin >> data1.bookNo >> data1.units_sold >> price) { data1.revenue = data2.units_sold * price; int cnt = 1; while (cin >> data2.bookNo >> data2.units_sold >> price) { data1.revenue = data2.units_sold * price; if (data1.bookNo == data2.bookNo) cnt++; else { cout << data1.bookNo << " " << cnt << endl; cnt = 1; data1 = data2; } } cout << data1.bookNo << " " << cnt << endl; } return 0; }
练习3.2
#include <iostream> #include<string> using std::cin; using std::cout; using std::string; int main() { string str,line; //一次读入一整行 while (getline(cin, line)) { str = str + line; } cout << str; return 0; }
#include <iostream> #include<string> using std::cin; using std::cout; using std::string; int main() { string str,line; //一次读入一个词 while (cin>>line) { str = str + line; } cout << str; return 0; }
练习3.3
输入运算符会自动忽略开头的空白并从第一个真正的字符读起,直到遇见下一处空白为止
getline会从给定的输入流中读入内容,保留输入时的空白符,直到遇到换行符为止
练习3.4
#include <iostream> #include<string> using std::cin; using std::cout; using std::string; int main() { string str1,str2; //一次读入一整行 cin >> str1 >> str2; if (str1 == str2)cout << "相等"; else { cout << (str1 > str2 ? str1 : str2); } return 0; }
#include <iostream> #include<string> using std::cin; using std::cout; using std::string; int main() { string str1,str2; //一次读入一整行 cin >> str1 >> str2; if (str1.size() == str2.size())cout << "相等"; else { cout << (str1.size() > str2.size() ? str1 : str2); } return 0; }
#include <iostream> #include<string> using std::cin; using std::cout; using std::string; int main() { string str1,str2; //一次读入一整行 cin >> str1 >> str2; if (str1.size() == str2.size())cout << "相等"; else { cout << (str1.size() > str2.size() ? str1 : str2); } return 0; }
练习3.5
#include <iostream> #include<string> using std::cin; using std::cout; using std::string; int main() { string str1,str2; while (cin >> str1) { str2 += str1; } cout << str2; return 0; }
#include <iostream> #include<string> using std::cin; using std::cout; using std::string; int main() { string str1,str2; while (cin >> str1) { str1 += " "; str2 += str1; } cout << str2; return 0; }
练习3.6
#include <iostream> #include<string> using std::cout; using std::string; int main() { string str="0123456789ABCDEF"; for (auto& c : str) { c = 'X'; } cout << str; return 0; }
练习3.7
没有变化
#include <iostream> #include<string> using std::cout; using std::string; int main() { string str="0123456789ABCDEF"; for (char& c : str) { c = 'X'; } cout << str; return 0; }
练习3.8
#include <iostream> #include<string> using std::cout; using std::string; int main() { string str="0123456789ABCDEF"; decltype(str.size()) i = 0; while (i < str.size()) { str[i++] = 'X'; } cout << str; return 0; }
#include <iostream> #include<string> using std::cout; using std::string; int main() { string str="0123456789ABCDEF"; for (auto i = 0; i < str.size();i++) { str[i] = 'X'; } cout << str; return 0; }
练习3.9
输出默认初始化字符串s的第一个元素。不合法,因为默认初始化的字符串是空串,所以s[0]越界了。
练习3.10
#include <iostream> #include<string> using std::cout; using std::string; int main() { string str="123()_+asad"; for (string::size_type i = 0; i < str.size(); i++) { if (!ispunct(str[i])) cout << str[i]; } return 0; }
练习3.11
for循环体不看后面花括号的内容,肯定是合法的,但是如果在for循环中花括号中给C赋值,修改C的值,那么此时的for语句肯定是不合法的。C的类型是const char&
练习3.12
知识点:vector的初始化:
1:引用不可以成为vector的元素,因为其不是对象。
2:可以用花括号初始化每一个值。
3:可以用括号指定元素个数或相同的元素值。
4:只能使用直接初始化,不可以使用拷贝初始化(vector之间的拷贝是可行的,但要保证类型相同)
答案:
(a)正确,创建了一个元素为vector的vector对象
(b)不正确,类型不一致
(c)正确,十个“null”对象
练习3.13
(a)空vector
(b)10个重复执行值初始化对象0
(c)10个重复的元素42
(d)1个元素初始值为10
(e)两个元素,初始分别为10,42
(f)10个字符串,初始化为“ ”(空)
(g)10个字符串,初始化为“hi ”
练习3.14
#include <iostream> #include<string> #include <vector> using std::cin; using std::cout; using std::vector; int main() { int num; vector<int> v; while (cin >> num) { v.push_back(num); } return 0; }
练习3.15
#include <iostream> #include<string> #include <vector> using std::cin; using std::cout; using std::vector; using std::string; int main() { string str; vector<string> v; while (cin >> str) { v.push_back(str); } return 0; }
练习3.16
#include <iostream> using std::cin; using std::cout; using std::endl; #include <vector> using std::vector; #include<string> using std::string; int main() { vector<int> v1; vector<int> v2(10); vector<int> v3(10, 42); vector<int> v4{ 10 }; vector<int> v5{ 10,42 }; vector<string> v6{ 10 }; vector<string> v7(10, "hi"); cout << v1.size()<<" "; for (auto v1_item = v1.begin(); v1_item != v1.end(); v1_item++) cout << *v1_item << " "; cout << endl << v2.size() << " "; for (auto v2_item = v2.begin(); v2_item != v2.end(); v2_item++) cout << *v2_item << " "; cout << endl << v1.size() << " "; for (auto v3_item = v3.begin(); v3_item != v3.end(); v3_item++) cout << *v3_item << " "; cout << endl << v1.size() << " "; for (auto v4_item = v4.begin(); v4_item != v4.end(); v4_item++) cout << *v4_item << " "; cout << endl << v5.size() << " "; for (auto v5_item = v5.begin(); v5_item != v5.end(); v5_item++) cout << *v5_item << " "; cout << endl << v6.size() << " "; for (auto v6_item = v6.begin(); v6_item != v6.end(); v6_item++) cout << *v6_item << " "; cout << endl << v7.size() << " "; for (auto v7_item = v7.begin(); v7_item != v7.end(); v7_item++) cout << *v7_item << " "; return 0; }
0
10 0 0 0 0 0 0 0 0 0 0
0 42 42 42 42 42 42 42 42 42 42
0 10
2 10 42
10
10 hi hi hi hi hi hi hi hi hi hi
练习3.17
#include <iostream> using std::cin; using std::cout; using std::endl; #include <vector> using std::vector; #include<string> using std::string; int main() { vector<string> v; string str; while (cin >> str) { v.push_back(str); } for (auto i = v.begin(); i != v.end(); i++) { str = *i; for (auto&c:str) { if (islower(c)) c = toupper(c); } *i = str; } for (auto v_item : v) { cout << v_item << endl; } return 0; }
练习3.18
不合法。
vector<int> ivec; ivec.push_back(42);
练习3.19
vector<int> v1(10, 42); vector<int> v2{ 42, 42,42, 42,42, 42,42, 42, 42, 42 }; vector<int> v3 = v2;
第一种最好,比较简洁
练习3.20
#include <iostream> using std::cin; using std::cout; using std::endl; #include <vector> using std::vector; int main() { vector<int> v; int num; while (cin >> num) { v.push_back(num); } for (decltype(v.size()) i = 0; i < v.size() - 1; i++) { cout << (v[i] + v[i + 1]) << " "; } return 0; }
#include <iostream> using std::cin; using std::cout; using std::endl; #include <vector> using std::vector; int main() { vector<int> v; int num; while (cin >> num) { v.push_back(num); } decltype(v.size()) j = v.size() - 1; for (decltype(v.size()) i = 0; i < v.size(); i++) { if (i < j - i) cout << (v[i] + v[j - i]) << " "; } return 0; }
练习3.21
#include <iostream> using std::cin; using std::cout; using std::endl; #include <vector> using std::vector; #include<string> using std::string; int main() { vector<int> v1; vector<int> v2(10); vector<int> v3(10, 42); vector<int> v4{ 10 }; vector<int> v5{ 10,42 }; vector<string> v6{ 10 }; vector<string> v7(10, "hi"); cout << v1.size()<<" "; for (auto v1_item = v1.begin(); v1_item != v1.end(); v1_item++) cout << *v1_item << " "; cout << endl << v2.size() << " "; for (auto v2_item = v2.begin(); v2_item != v2.end(); v2_item++) cout << *v2_item << " "; cout << endl << v1.size() << " "; for (auto v3_item = v3.begin(); v3_item != v3.end(); v3_item++) cout << *v3_item << " "; cout << endl << v1.size() << " "; for (auto v4_item = v4.begin(); v4_item != v4.end(); v4_item++) cout << *v4_item << " "; cout << endl << v5.size() << " "; for (auto v5_item = v5.begin(); v5_item != v5.end(); v5_item++) cout << *v5_item << " "; cout << endl << v6.size() << " "; for (auto v6_item = v6.begin(); v6_item != v6.end(); v6_item++) cout << *v6_item << " "; cout << endl << v7.size() << " "; for (auto v7_item = v7.begin(); v7_item != v7.end(); v7_item++) cout << *v7_item << " "; return 0; }
练习3.22
#include <iostream> using std::cin; using std::cout; #include <vector> using std::vector;int main() { vector<string> v; string str; if (getline(cin,str)) { v.push_back(str); } for (auto i = v.begin(); i != v.end(); i++) { str = *i; for (auto& c : str) { if (islower(c)) c = toupper(c); } *i = str; } for (auto v_item : v) { cout << v_item << endl; } return 0; }
练习3.23
#include <iostream> using std::cin; using std::cout; #include <vector> using std::vector; int main() { vector<int> v; int num; for (unsigned i = 0; i < 10; i++) { cin >> num; v.push_back(num); } for (auto& v_item : v) { v_item *= 2; } for (auto v_item : v) { cout << v_item << " "; } return 0; }
练习3.24
#include <iostream> using std::cin; using std::cout; #include <vector> using std::vector; int main() { vector<int> v; int num; while (cin >> num) { v.push_back(num); } for (auto i = v.begin(), j = v.end() - 1; i < j; i++,j--) { cout << (*i + *j) << " "; } return 0; }
练习3.25
#include <iostream> using std::cin; using std::cout; #include <vector> using std::vector; int main() { vector<unsigned> scores(11,0); unsigned grade; while (cin >> grade) { if (grade <= 100) { auto it = (scores.begin() + grade / 10); (*it)++; } } return 0; }
练习3.26
迭代器不支持除法运算
练习3.27
(a)非法,buf_size不是常量表达式
(c)非法,txt_size返回值是int而不是constexpr
练习3.28
在函数体外初始化
sa数组为空。
ia数组含有10个整数,皆为0。
在函数体内初始化
sa2数组为空。
ia2数组含有10个整数,值不确定。
练习3.29
数组的大小确定不变,不能随意向数组中增加元素,因此损失了一些灵活性
练习3.30
第三行"ix<=array_size"数组下标越界
练习3.31
#include <iostream> int main() { int array_int[10]; for (size_t i = 0; i < 10; i++) { array_int[i] = i; } return 0; }
练习3.32
#include <iostream> using std::cout; int main() { int array_int[10]; for (size_t i = 0; i < 10; i++) { array_int[i] = i; } int array_int2[10]; for (size_t i = 0; i < 10; i++) { array_int2[i] = array_int[i]; } return 0; }
#include <iostream> #include<vector> using std::vector; int main() { vector<int> array_int(10); auto array_int_item = array_int.begin(); for (size_t i = 0; i < 10; i++) { *(array_int_item+i) = i; } vector<int> array_int2(array_int); return 0; }
练习3.33
在函数体内不初始化值就是未知的。
练习3.34
将p1指向的元素修改为p2指向的元素,由于p1和p2指向同一个数组中的元素,所以没有改变。当p1或p2其中一个越界时该程序是非法的
练习3.35
#include <iostream> #include<iterator> using std::begin; using std::end; int main() { int array_int[10]; auto* pend = end(array_int); for (auto pben = begin(array_int); pben != pend; pben++) { *pben = 0; } return 0; }
练习3.36
template <typename _Container,unsigned N,unsigned M> bool comptr(_Container(&ptr1)[N], _Container(&ptr2)[M]) { if (N == M) {//比较数组长度 auto pend = end(ptr1); for (auto ptr1_item = ptr1, ptr2_item = ptr2; ptr1_item != pend; ptr1_item++, ptr2_item++) { if (*ptr1_item != *ptr2_item)return false; } return true; } return false; }
template <typename _Container> bool comptr(_Container(&ptr1), _Container(&ptr2)) { if (ptr1.size()==ptr2.size()) { for (auto ptr1_item = ptr1.begin(), ptr2_item = ptr2.begin(); ptr1_item != ptr1.end(); ptr1_item++, ptr2_item++) { if (*ptr1_item != *ptr2_item)return false; } return true; } return false; }
练习3.37
h
e
l
l
o
练习3.38
指针指代的是对象的地址,相加是地址相加,难以区分到底什么意义。
练习3.39
C++风格的字符串比较是字符串本身的比较
C风格的字符串比较是字符串首地址的比较
练习3.40
#include <iostream> #include <string> #include <vector> using namespace std; void main() { char a[100] = "Hello World"; char b[100] = "I want an apple"; char c[200]; strcpy(c,a); strcat(c," "); strcat(c,b); cout<<c; }
练习3.41
#include <iostream> using std::cout; using std::endl; #include<iterator> using std::begin; using std::end; #include <vector> using std::vector; int main() { int array_int[] = { 0,1,2,3,4 }; vector<int> v(begin(array_int), end(array_int)); }
练习3.42
void vector_to_array(vector<int>::iterator it, size_t& size) { int* array_int = new int[size]; for (auto i = 0; i < size; i++) { array_int[i] = *(it + i); cout << array_int[i]<<" "; } }
练习3.43
for (int(&p)[4] : ia) { for (int q : p) { cout << q << " "; } cout << endl; } for (int p = 0; p < 3; p++) { for (int q = 0; q < 4; q++) { cout << ia[p][q] << " "; } cout << endl; } for (int(*p)[4] = ia; p < ia + 3; p++) { for (int* q = *p; q < *p + 4; q++) { cout << *q << " "; } cout << endl; }
练习3.44
using int_array = int[4]; for (int_array(&p) : ia) { for (int q : p) { cout << q << " "; } cout << endl; } for (int p = 0; p < 3; p++) { for (int q = 0; q < 4; q++) { cout << ia[p][q] << " "; } cout << endl; } for (int_array(*p) = ia; p < ia + 3; p++) { for (int* q = *p; q < *p + 4; q++) { cout << *q << " "; } cout << endl; }
练习3.45
for (auto (&p) : ia) { for (auto q : p) { cout << q << " "; } cout << endl; } for (auto p = 0; p < 3; p++) { for (auto q = 0; q < 4; q++) { cout << ia[p][q] << " "; } cout << endl; } for (auto (*p) = ia; p < ia + 3; p++) { for (auto* q = *p; q < *p + 4; q++) { cout << *q << " "; } cout << endl; }