在我自学C++过程中,我选择了C++Primer这本书,并对部分代码习题进行了求解以及运行结果。接下来几个月我将为大家定时按章节更新习题答案与运行结果,运行环境(Visual Studio Code,windows 11):
#includeusing std::cin; using std::cout; using std::endl; int main() { int sum = 0; for (int val = 1; val <= 10; ++val) sum += val; cout << "Sum of 1 to 10 inclusive is " << sum << endl; return 0; }
#include#include using std :: cin; using std :: cout; using std :: endl; using std :: string; struct Sale_data { string bookNo; unsigned units_sold = 0; double revenue = 0.0; }; int main() { Sale_data total; double totalprice; if(cin >> total.bookNo >> total.units_sold >> totalprice) { total.revenue = total.units_sold*totalprice; Sale_data nextdata; double nextprice; while(cin >> nextdata.bookNo >> nextdata.units_sold >> nextprice) { nextdata.revenue = nextprice*nextdata.units_sold; if(total.bookNo == nextdata.bookNo) { total.units_sold += nextdata.units_sold; total.revenue += nextdata.revenue; } else { cout << total.bookNo << ":" << total.units_sold << " " << total.revenue << " "; if(total.units_sold != 0) cout << total.revenue/total.units_sold << endl; else cout << "No Sales!" << endl; total.bookNo = nextdata.bookNo; total.units_sold =nextdata.units_sold; total.revenue = nextdata.revenue; } } cout << total.bookNo << ":" << total.units_sold << " " << total.revenue << " "; if(total.units_sold != 0) cout << total.revenue/total.units_sold << endl; else cout << "No Sales!" << endl; } else cout << "No data!" << endl; return 0; }
#include#include using std :: cin; using std :: cout; using std :: endl; using std :: string; using std :: getline; int main() { string l; while(getline(cin,l)) { cout << l << endl; } return 0; }
#include#include using std :: cin; using std :: cout; using std :: endl; using std :: string; int main() { string w; while(cin >> w) { cout << w << endl; } return 0; }
#include#include using std :: cin; using std :: cout; using std :: endl; using std :: string; int main() { string w1, w2; cin >> w1; cin >> w2; if(w1 == w2) cout << "相等!" << endl; else if(w1 > w2) cout << w1 << endl; else cout << w2 << endl; return 0; }
#include#include using std :: cin; using std :: cout; using std :: endl; using std :: string; int main() { string w1, w2; cin >> w1; cin >> w2; if(w1.length() == w2.length()) cout << "相等!" << endl; else if(w1.length() > w2.length()) cout << w1 << endl; else cout << w2 << endl; return 0; }
#include#include using std :: cin; using std :: cout; using std :: endl; using std :: string; int main() { string w1, w2, w; cin >> w1; cin >> w2; w = w1 + w2; cout << w << endl; return 0; }
#include#include using std :: cin; using std :: cout; using std :: endl; using std :: string; int main() { string w1, w2, w; cin >> w1; cin >> w2; w = w1 + " " + w2; cout << w << endl; return 0; }
#include#include using std::string; using std::cin; using std::cout; using std::endl; int main() { string s = "Hello World!"; for(auto &c : s) c = 'X'; cout << s << endl; return 0; }
#include#include using std::string; using std::cin; using std::cout; using std::endl; int main() { string s = "Hello World!"; for(char &c : s) c = 'X'; cout << s << endl; return 0; }
#include#include using std::string; using std::cin; using std::cout; using std::endl; int main() { string s = "Hello World!"; decltype (s.size()) i = 0; while(i != s.size()) { s[i] = 'X'; i++; } cout << s << endl; for(i = 0;i != s.size();i++) { s[i] = 'X'; } cout << s << endl; return 0; }
string s; cout << s[0] << endl;
不合法。使用下标访问空字符串是非法行为。
#include#include using std::string; using std::cin; using std::cout; using std::endl; int main() { string s = "Hello , World!"; string re; for(auto x : s) { if(!ispunct(x)) re = re + x; } cout << re << endl; return 0; }
#include#include #include using std::cin; using std::cout; using std::endl; using std::vector; int main() { vector v; int i; while (cin >> i) { v.push_back(i); } return 0; }
#include#include #include using std::cin; using std::cout; using std::endl; using std::vector; using std::string; int main() { vector v; string i; while (cin >> i) { v.push_back(i); } return 0; }
#include#include #include using std::cin; using std::cout; using std::endl; using std::vector; using std::string; int main() { vector v1; vector v2(10); vector v3(10, 42); vector v4{ 10 }; vector v5{ 10, 42 }; vector v6{ 10 }; vector v7{ 10, "hi" }; cout << "v1 size:" << v1.size() << endl; cout << "v1 is :" << endl; for (auto i : v1) { cout << i << ","; } cout << endl; cout << "v2 size:" << v2.size() << endl; cout << "v2 is :" << endl; for (auto i : v2) { cout << i << ","; } cout << endl; cout << "v3 size:" << v3.size() << endl; cout << "v3 is :" << endl; for (auto i : v3) { cout << i << ","; } cout << endl; cout << "v4 size:" << v4.size() << endl; cout << "v4 is :" << endl; for (auto i : v4) { cout << i << ","; } cout << endl; cout << "v5 size:" << v5.size() << endl; cout << "v5 is :" << endl; for (auto i : v5) { cout << i << ","; } cout << endl; cout << "v6 size:" << v6.size() << endl; cout << "v6 is :" << endl; for (auto i : v6) { cout << i << ","; } cout << endl; cout << "v7 size:" << v7.size() << endl; cout << "v7 is :" << endl; for (auto i : v7) { cout << i << ","; } cout << endl; return 0; }
#include#include #include using std::cin; using std::cout; using std::endl; using std::vector; using std::string; int main() { vector v; string s; while (cin >> s) { v.push_back(s); } for (auto& c1 : v) { for (auto& c2 : c1) { c2 = toupper(c2); } } for (auto i : v) { cout << i << endl; } return 0; }
1:
vectorivec1(10, 42);
2:
vectorivec2{ 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 };
3:
vectorivec3; for (int i = 0; i < 10; ++i) ivec3.push_back(42);
方法1最好。
#include#include #include using std::cin; using std::cout; using std::endl; using std::vector; using std::string; int main() { vector v0, v1, v2; int i; while (cin >> i) { v0.push_back(i); } for (decltype(v0.size()) index = 0; index < v0.size(); index++) { cout << v0[index] << " "; } cout << endl; for (decltype(v0.size()) index = 0; index < v0.size() - 1; index++) { v1.push_back(v0[index] + v0[index + 1]); } for (decltype(v1.size()) index = 0; index < v1.size(); index++) { cout << v1[index] << " "; } cout << endl; for (decltype(v0.size()) index = 0; index < v0.size() / 2 + 1 && index<= v0.size()-1-index; index++) { v2.push_back(v0[index] + v0[v0.size() - 1 - index]); } for (decltype(v2.size()) index = 0; index < v2.size(); index++) { cout << v2[index] << " "; } cout << endl; return 0; }
#include#include #include using std::cin; using std::cout; using std::endl; using std::vector; using std::string; int main() { vector v1; cout << v1.size() << endl; for (auto it = v1.begin(); it != v1.end(); ++it) { cout << *it << ","; } cout << endl; vector v2(10); cout << v2.size() << endl; for (auto it = v2.begin(); it != v2.end(); ++it) { cout << *it << ","; } cout << endl; vector v3(10, 42); cout << v3.size() << endl; for (auto it = v3.begin(); it != v3.end(); ++it) { cout << *it << ","; } cout << endl; vector v4{ 10 }; cout << v4.size() << endl; for (auto it = v4.begin(); it != v4.end(); ++it) { cout << *it << ","; } cout << endl; vector v5{ 10, 42 }; cout << v5.size() << endl; for (auto it = v5.begin(); it != v5.end(); ++it) { cout << *it << ","; } cout << endl; vector v6{ 10 }; cout << v6.size() << endl; for (auto it = v6.begin(); it != v6.end(); ++it) { cout << *it << ","; } cout << endl; vector v7{ 10,"hi" }; cout << v7.size() << endl; for (auto it = v7.begin(); it != v7.end(); ++it) { cout << *it << ","; } cout << endl; return 0; }
#include#include #include using std::cin; using std::cout; using std::endl; using std::vector; using std::string; int main() { vector v1{ "aa","bb","cc" }; for (auto it = v1.begin(); it != v1.end() && !it->empty(); it++) { for(auto &c:*it) { c = toupper(c); } } for (auto it = v1.begin(); it != v1.end() && !it->empty(); it++) { cout << *it << ","; } cout << endl; return 0; }
#include#include #include using std::cin; using std::cout; using std::endl; using std::vector; using std::string; int main() { vector v1{ 0,1,2,3,4,5,6,7,8,9 }; for (auto it = v1.begin(); it != v1.end(); it++) { *it = *it * 2; } for (auto it = v1.begin(); it != v1.end(); it++) { cout << *it << ","; } cout << endl; return 0; }
#include#include #include using std::cin; using std::cout; using std::endl; using std::vector; using std::string; int main() { vector v0, v1, v2; int i; while (cin >> i) { v0.push_back(i); } for (auto it = v0.begin(); it != v0.end() - 1; it++) { v1.push_back(*it + *(it + 1)); } for (auto ic : v1) { cout << ic << ","; } cout << endl; if (v0.size() % 2 == 0) { for (auto it = v0.begin(); it != v0.begin() + v0.size() / 2; it++) { v2.push_back(*it + *(v0.end() - (it - v0.begin()) - 1)); } } else { for (auto it = v0.begin(); it != v0.begin() + v0.size() / 2 + 1; it++) { v2.push_back(*it + *(v0.end() - (it - v0.begin()) - 1)); } } for (auto ic : v2) { cout << ic << ","; } cout << endl; return 0; }
#include#include #include using std::cin; using std::cout; using std::endl; using std::vector; using std::string; int main() { vector sorces(11, 0); unsigned grade; auto it = sorces.begin(); while (cin >> grade) { (*(it + grade / 10))++; } for (auto it = sorces.begin(); it != sorces.end(); it++) { cout << *it << ","; } cout << endl; return 0; }
#include#include #include using std::cin; using std::cout; using std::endl; using std::vector; using std::string; int main() { int va[10]; for (size_t index = 0; index < 10; index++) { va[index] = index; } for (auto i : va) { cout << va[i] << ","; } cout << endl; return 0; }
#include#include #include using std::cin; using std::cout; using std::endl; using std::vector; using std::string; int main() { //数组 int va[10],va1[10]; for (size_t index = 0; index < 10; index++) { va[index] = index; } for (size_t index = 0; index < 10; index++) { va1[index] = va[index]; } for (auto i : va1) { cout << va1[i] << ","; } cout << endl; //vector vector ve; for (decltype(ve.size()) index = 0; index < 10; index++) { ve.push_back(index); } vector ve2(ve); for (auto i : ve2) { cout << i << ","; } cout << endl; return 0; }
p1 += p2 - p1;
将p1移动(p2-p1)个位置;p1或p2是非法的,该程序就是非法的。
#include#include #include using std::cin; using std::cout; using std::endl; using std::vector; using std::string; int main() { //数组 int va[10],va1[10]; for (size_t index = 0; index < 10; index++) { va[index] = index; } for (auto i : va) { cout << i << ","; } cout << endl; int* p = va; for (size_t index = 0; index < 10; index++ , p++) { *p = 0; } for (auto i : va) { cout << i << ","; } cout << endl; return 0; }
#include#include #include #include using namespace std; bool compareva(int* const vb1,int* const ve1,int* const vb2,int* const ve2) { if ((ve1 - vb1) == (ve2 - ve1)) { return false; } else { for (int* i1 = vb1, *i2 = vb2; i1 != ve1 && i2 != ve2; i1++, i2++) { if (*i1 != *i2) { return false; } } } return true; } int main() { //数组 int v1[6] = { 0,23,66 }, v2[2]; if (compareva(v1, end(v1), v2, end(v2))) { cout << "equal!" << endl; } else { cout << "No equal!" << endl; } //vector vector m1 = { 1,2,3,4,5,6 }, m2 = { 1,2,3,4,5,6 }; if (m1 == m2) { cout << "equal!" << endl; } else { cout << "No equal!" << endl; } return 0; }
#include#include #include #include using namespace std; int main() { string s1 = "aabbcc", s2 = "aabbcc"; if (s1 == s2) { cout << "Equal!" << endl; } else { cout << "No equal!" << endl; } char v1[] = { 'a','b','c' }, v2[] = "abc"; if (strcmp(v1, v2)) { cout << "Equal!" << endl; } else { cout << "No equal!" << endl; } return 0; }
#include#include const char cstr1[] = "Hello"; const char cstr2[] = "world!"; int main() { char cstr3[100]; strcpy(cstr3, cstr1); strcat(cstr3, " "); strcat(cstr3, cstr2); std::cout << cstr3 << std::endl; }
Hello world!
#include#include #include #include using namespace std; int main() { int v1[] = {1,2,3,4,5,6,7,8,9,10}; vector x1(begin(v1), end(v1)); for (auto i : x1) { cout << i << ","; } cout << endl; return 0; }
#include#include #include #include using namespace std; int main() { vector v1 = {1,2,3,4,5,6,7,8,9,10}; int v2[10]; for (int index = 0; index < v1.size(); index++) { v2[index] = v1[index]; } for (auto i : v2) { cout << i << ","; } cout << endl; return 0; }
#include#include #include #include using namespace std; int main() { int v1[2][3] = { 1,2,3,4,5,6 }; //1 for (int (&i)[3] : v1) { for (int j : i) { cout << j << ","; } } cout << endl; //2 for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cout << v1[i][j] << ","; } } cout << endl; //3 for (int(*p)[3] = v1; p != v1 + 2; p++) { for (int* q = *p; q != *p + 3; q++) { cout << *q << ","; } } cout << endl; return 0; }
#include#include #include #include using namespace std; int main() { using int_row = int[3]; int v1[2][3] = { 1,2,3,4,5,6 }; //1 for (int_row &i : v1) { for (int j : i) { cout << j << ","; } } cout << endl; //2 for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cout << v1[i][j] << ","; } } cout << endl; //3 for (int_row *p = v1; p != v1 + 2; p++) { for (int* q = *p; q != *p + 3; q++) { cout << *q << ","; } } cout << endl; return 0; }
#include#include #include #include using namespace std; int main() { int v1[2][3] = { 1,2,3,4,5,6 }; //1 for (auto &i : v1) { for (auto j : i) { cout << j << ","; } } cout << endl; //2 for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { cout << v1[i][j] << ","; } } cout << endl; //3 for (auto *p = v1; p != v1 + 2; p++) { for (int* q = *p; q != *p + 3; q++) { cout << *q << ","; } } cout << endl; return 0; }
上一篇:rabbitmq知识梳理