赞
踩
前两章习题较简单,这里就不作整理了,直接从第三章开始(持续更新):
part 1
- #include <iostream>
-
- using std::cin;
- using std::cout;
- using std::endl;
-
- int main()
- {
- int sum = 0, val = 1;
- while (val <= 10) {
- sum += val;
- ++val;
- }
- cout << "Sum of 1 to 10 inclusive is " << sum << endl;
- return 0;
- }
part 2
- #include <iostream>
-
- using std::cin;
- using std::cout;
- using std::endl;
- using std::cerr;
-
- struct Sales_data {
- std::string bookNo;
- unsigned unit_sold = 0;
- double revenue = 0.0;
- };
-
- int main()
- {
- Sales_data data1, data2;
- double price = 0;
- cin >> data1.bookNo >> data1.unit_sold >> price;
- data1.revenue = data1.unit_sold * price;
- cin >> data2.bookNo >> data2.unit_sold >> price;
- data2.revenue = data2.unit_sold * price;
- if (data1.bookNo == data2.bookNo) {
- unsigned totalCnt = data1.unit_sold + data2.unit_sold;
- double totalRevenue = data1.revenue + data2. revenue;
- cout << data1.bookNo << " " << totalCnt << " " << totalRevenue << " ";
- if (totalCnt != 0)
- cout << totalRevenue/totalCnt << endl;
- else
- cout << "(no sale)" << endl;
- return 0;
- }
- else{
- cerr << "Data must refer to the same ISBN" << endl;
- return -1;
- }
- }
part 1
- #include <iostream>
- #include <string>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
-
- int main()
- {
- string line;
- while (getline(cin, line)) {
- cout << line << endl;
- }
- return 0;
- }
part 2
- #include <iostream>
- #include <string>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
-
- int main()
- {
- string word;
- while (cin >> word)
- cout << word << endl;
- return 0;
- }
string类输入运算符(>>):忽视一切空格,从第一个非空格字符开始,到第一个空格结束;
getline():包含换行符前的所有空格;
part 1
- #include <iostream>
- #include <string>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
-
- int main()
- {
- string st1, st2;
- cin >> st1 >> st2;
- if (st1 == st2)
- cout << "The two strings are equal" << endl;
- else if (st1 > st2)
- cout << "The maximum string is " << st1 << endl;
- else
- cout << "The maximum string is " << st2 << endl;
- return 0;
- }
part 2
- #include <iostream>
- #include <string>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
-
- int main()
- {
- string st1, st2;
- cin >> st1 >> st2;
- if (st1.size() == st2.size())
- cout << "The two strings have the same length " << endl;
- else if (st1.size() > st2.size())
- cout << "The longest string is " << st1 << endl;
- else
- cout << "The longest string is " << st2 << endl;
- return 0;
- }
part 1
- #include <iostream>
- #include <string>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
-
- int main()
- {
- string total_str, str;
- while (cin >> str)
- total_str += str;
- cout << "The concatenated string is " << total_str << endl;
- return 0;
- }
part 2
- #include <iostream>
- #include <string>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
-
- int main()
- {
- string total_str, str;
- while (cin >> str)
- if (total_str.empty())
- total_str += str;
- else
- total_str += " " +str;
- cout << "The concatenated string is " << total_str << endl;
- return 0;
- }
- #include <iostream>
- #include <string>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
-
- int main()
- {
- string str("I wanna go home!");
- for (auto &c : str) {
- c = 'X';
- }
-
- cout << str << endl;
- return 0;
- }
- #include <iostream>
- #include <string>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
-
- int main()
- {
- string str("I wanna go home!");
- for (char c : str) {
- c = 'X';
- }
- cout << str << endl;
- return 0;
- }
- //输出仍为I wanna go home!
本质上是把string中的每一个字符拷贝给循环控制变量c,此时对c更改只是改变了c字符,原string并没有任何变化,想要通过c改变string的值,就必须把c定义为对字符的引用。
part 1
- #include <iostream>
- #include <string>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
-
- int main()
- {
- string str("I wanna go home!");
- decltype(str.size()) n = 0; //这里注意必须初始化
- while ( n < str.size()) {
- str[n] = 'X';
- ++n;
- }
- cout << str << endl;
- cout << n << endl;
- return 0;
- }
part 2
- #include <iostream>
- #include <string>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
-
- int main()
- {
- string str("I wanna go home!");
- for ( decltype(str.size()) index = 0; index < str.size(); ++index) {
- str[index] = 'X';
- }
- cout << str << endl;
- return 0;
- }
使用范围for语句更简洁,逻辑也更清晰。
不合法,不能用下标访问空字符串。
- #include <iostream>
- #include <string>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
-
- int main()
- {
- cout << "Please input a string with punctuation: " << endl;
- string str, result;
- cin >> str;
- if (!str.empty()) {
- for (auto &c : str) {
- if ( !ispunct(c))
- result += c;
- }
- cout << result << endl;
- return 0;
- }
- cout << "Your string is fucking empty!" << endl;
- return -1;
- }
注:这里使用ispunct()但不Include <cctype>是因为iostream中间接包含了cctype。
c为常量字符串的引用(底层const),只读取数据——合法,通过其修改数据——不合法。
(a)正确,元素为向量,元素的元素为int;
(b)错误,拷贝初始化类型必须统一;
(c)正确,向量拥有10个空字符串元素;
(a)无元素;
(b)10个元素,值均为0;
(c)10个元素,值均为42;
(d)一个元素,值为10;
(e)两个元素,值分别为10、42;
(f)10个元素,均为空字符串;
(g)10个元素,均为"hi";
- #include <iostream>
- #include <string>
- #include <vector>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
- using std::vector;
-
- int main()
- {
- vector<int> ivec;
- int i;
- while (cin >> i) {
- ivec.push_back(i);
- return 0;
- }
- }
- #include <iostream>
- #include <string>
- #include <vector>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
- using std::vector;
-
- int main()
- {
- vector<string> svec;
- string str;
- while (cin >> str) {
- svec.push_back(str);
- return 0;
- }
- }
- #include <iostream>
- #include <string>
- #include <vector>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
- using std::vector;
-
- int main()
- {
- //v1
- vector<int> v1;
- cout << "The size is: "<< v1.size() << endl;
- cout << "The value is: ";
- for (auto i : v1)
- cout << i << " ";
- cout << endl;
- //v2
- vector<int> v2(10);
- cout << "The size is: "<< v2.size() << endl;
- cout << "The value is: ";
- for (auto i : v2)
- cout << i << " ";
- cout << endl;
- //后续同理
- }
- #include <iostream>
- #include <string>
- #include <vector>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
- using std::vector;
-
- int main()
- {
- vector<string> text;
- string word;
- while (cin >> word) {
- text.push_back(word);
- }
- cout << "The original text is: " << endl;
- for (auto i : text) {
- cout << i << endl;
- }
- cout << "The changed text is: " << endl;
- for (auto str : text) {
- for (auto &c : str) {
- c = toupper(c);
- }
- cout << str << endl;
- }
- return 0;
- }
不合法,不能使用无效下标访问不存在的元素。修改如下:
- #include <iostream>
- #include <string>
- #include <vector>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
- using std::vector;
-
- int main()
- {
- vector<int> ivec;
- ivec.push_back(42);
- cout << ivec[0] << endl;
- return 0;
- }
- #include <iostream>
- #include <string>
- #include <vector>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
- using std::vector;
-
- int main()
- {
- vector<int> ivec1(10, 42);
- vector<int> ivec2{42, 42, 42, 42, 42, 42, 42, 42, 42, 42};
- vector<int> ivec3;
- for (auto i = 0; i != 10; ++i)
- ivec3.push_back(42);
- vector<string> svec1{10, "42"};
- vector<string> svec2(10, "42");
- return 0;
- }
ivec1的定义方式简洁且高效。
part 1:
- #include <iostream>
- #include <string>
- #include <vector>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
- using std::vector;
-
- int main()
- {
- vector<int> ivec;
- int i = 0, sum = 0;
- while (cin >> i)
- ivec.push_back(i);
- if (ivec.empty()) { //因为后续用到下标,这里必须验证是否为空!!
- cout << "Please input at least one integer"<< endl;
- return -1;
- }
- cout << "The sum is: " << endl;
- for (decltype(ivec.size()) index = 0; index < ivec.size() - 1; ++index) {
- sum = ivec[index]+ ivec[index + 1];
- cout << sum << " ";
- }
- cout << endl;
- return 0;
- }
part 2:
- #include <iostream>
- #include <string>
- #include <vector>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
- using std::vector;
-
- int main()
- {
- vector<int> ivec;
- int i = 0, sum = 0;
- while (cin >> i)
- ivec.push_back(i);
- if (ivec.empty()) {
- cout << "Please input at least one integer"<< endl;
- return -1;
- }
- cout << "The sum is: " << endl;
- for (decltype(ivec.size()) index = 0; index <= ivec.size()/2; ++index) {
- if (index < ivec.size() - 1 - index) {
- sum = ivec[index]+ ivec[ivec.size() - 1 - index];
- cout << sum << " ";
- }
- else if (index == ivec.size() - 1 - index) { //这里一定注意为双等号!!
- cout << ivec[index];
- }
- }
- cout << endl;
- return 0;
- }
- #include <iostream>
- #include <string>
- #include <vector>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
- using std::vector;
-
- int main()
- {
- //以v2为例,其他同理
- vector<int> v2(10);
- if (v2.empty()) {
- cout << "This is a empty vector " << endl;
- return -1;
- }
- cout << "The size of the vector is: " << v2.size() << endl;
- cout << "The values of the elements are: ";
- for (auto it = v2.cbegin(); it != v2.cend(); ++it) {
- cout << *it << " ";
- }
- cout << endl;
- return 0;
- }
- #include <iostream>
- #include <string>
- #include <vector>
-
- using std::string;
- using std::cin;
- using std::cout;
- using std::endl;
- using std::vector;
-
- int main()
- {
- cout << "Please enter a text: " << endl;
- vector<string> svec;
- string str;
- while (getline(cin, str))
- svec.push_back(str);
- auto it = svec.begin();
- for (auto &c : *it)
- c = toupper(c);
- for (it;it != svec.cend(); ++it) //注意这里it可不重新定义,但不能省略!!
- cout << *it << endl;
- return 0;
- }
- #include <iostream>
- #include <vector>
- #include <string>
-
- using std::vector;
- using std::string;
- using std::cout;
- using std::cin;
- using std::endl;
-
- int main()
- {
- vector<int> ivec(10);
- int i;
- cout << "Please enter ten integers: " << endl;
- for (auto it = ivec.begin(); it != ivec.cend(); ++it) {
- cin >> *it;
- *it *= 2;
- }
- for (auto i : ivec)
- cout << i << " ";
- cout << endl;
- return 0;
- }
part 1
- #include <iostream>
- #include <vector>
- #include <string>
-
- using std::vector;
- using std::string;
- using std::cout;
- using std::cin;
- using std::endl;
-
- int main()
- {
- vector<int> ivec;
- int i;
- while (cin >> i)
- ivec.push_back(i);
- cout << "The sum is: " << endl;
- for (auto it = ivec.cbegin(); it != ivec.cend() - 1; ++it)
- cout << *it + *(it + 1) << " ";
- cout << endl;
- return 0;
- }
part 2
法I:
- #include <iostream>
- #include <vector>
- #include <string>
-
- using std::vector;
- using std::string;
- using std::cout;
- using std::cin;
- using std::endl;
-
- int main()
- {
- vector<int> ivec;
- int i, sum;
- while (cin >> i)
- ivec.push_back(i);
- if (ivec.empty())
- cout << "Your vector is empty!" << endl;
- cout << "The sum is: " << endl;
- for (auto it = ivec.cbegin(); it != ivec.cbegin() + ivec.size()/2 +1; ++it) {
- if (it == ivec.cbegin() + (ivec.cend() - 1 - it)) {
- cout << *it;
- }
- else if (it < ivec.cbegin() + (ivec.cend() - 1 - it)) {
- sum = *it + *(ivec.cbegin() + (ivec.cend() - 1 - it));
- cout << sum << " ";
- }
- }
- cout << endl;
- return 0;
- }
法II:
- #include <iostream>
- #include <vector>
- #include <string>
-
- using std::vector;
- using std::string;
- using std::cout;
- using std::cin;
- using std::endl;
-
- int main()
- {
- vector<int> ivec;
- int i, sum;
- while (cin >> i)
- ivec.push_back(i);
- if (ivec.empty())
- cout << "Your vector is empty!" << endl;
- cout << "The sum is: " << endl;
- for (auto beg = ivec.cbegin(), end = ivec.cend() - 1; beg <= end; ++beg, --end) { //注意方法!!
- if (beg == end)
- cout << *beg;
- else
- cout << *beg + *end << " ";
- }
- cout << endl;
- return 0;
- }
- #include <iostream>
- #include <vector>
- #include <string>
-
- using std::vector;
- using std::string;
- using std::cout;
- using std::cin;
- using std::endl;
-
- int main()
- {
- vector<unsigned> scores(11, 0);
- unsigned grade;
- auto it = scores.begin();
- while (cin >> grade) {
- if (grade <= 100) {
- ++*(scores.begin() + grade/10);
- }
- }
- for (auto i : scores)
- cout << i << " ";
- cout << endl;
- return 0;
- }
两个迭代器间只定义了相见,未定义相加;
所以可通过利用iterator - iterator将iterator +iterator转化为iterator + n。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。