c++ primer 学习笔记-第六章
2015-08-03 21:33
549 查看
习题6.3:
习题6.4:
习题6.10:
习题6.12:
习题6.17:
函数cpp:
头文件.h:
源文件.cpp:
习题6.21:
习题6.22:
习题6.23:
习题6.27:
习题6.33:
习题6.37:
习题6.38:
习题6.42:
习题6.44:
习题6.54:
习题6.55:
#include <iostream> #include <string> #include <vector> #include <iterator> using std::cin; using std::cout; using std::endl; using std::string; using std::vector; using std::begin; using std::end; int fact(int n) { if (n == 1 || n==0) return 1; return n*fact(n - 1); } int main() { cout << fact(4) << endl; getchar(); getchar(); return 0; }
习题6.4:
#include <iostream> #include <string> #include <vector> #include <iterator> using std::cin; using std::cout; using std::endl; using std::string; using std::vector; using std::begin; using std::end; int fact(void) { cout << "n=?" << endl; int n = 0, f = 1; cin >> n; for (size_t i = 1; i < n+1; i++) { f *= i; } return f; } int main() { cout << fact() << endl; getchar(); getchar(); return 0; }
习题6.10:
#include "标头.h" void exchange(int *a,int *b) { *a = (*a) ^ (*b); *b = (*b) ^ (*a); *a = (*a) ^ (*b); }
习题6.12:
#include "标头.h" void exchange(int &a,int &b) { a ^= b; b ^= a; a ^= b; }
习题6.17:
函数cpp:
#include "标头.h" #include <string> #include <cctype>//用iostream即可使用isupper() using std::string; using std::isupper; bool find_super(const string &s)//判断string对象中是否含有大写字母 { for (auto c : s) if (isupper(c)) return true; return false; } void lower(string &s)//把string对象全都改成小写形式 { for (auto &c : s) c = tolower(c); }
头文件.h:
#ifndef H #define H #include <string> using std::string; bool find_super(const string &s); void lower(string &s); #endif
源文件.cpp:
#include <iostream> #include <string> #include <vector> #include <iterator> #include "标头.h" using std::cin; using std::cout; using std::endl; using std::string; using std::vector; using std::begin; using std::end; int main() { cout << std::boolalpha << find_super("hello,Boy!") << endl; string s("gagaGGG"); lower(s); cout << s << endl; getchar(); getchar(); return 0; }
习题6.21:
#include <iostream> #include <string> #include <vector> #include <iterator> using std::cin; using std::cout; using std::endl; using std::string; using std::vector; using std::begin; using std::end; int compare_int_pointer(const int i, const int *pi) { return (i > *pi) ? i : *pi; } int main() { int i = 8, ii = 10, *pi = ⅈ cout << compare_int_pointer(i, pi) << endl; getchar(); getchar(); return 0; }
习题6.22:
#include <iostream> #include <string> #include <vector> #include <iterator> using std::cin; using std::cout; using std::endl; using std::string; using std::vector; using std::begin; using std::end; void swap_pointer(int *&pi1, int *&pi2) { int *ptemp = pi1; pi1 = pi2; pi2 = ptemp; } int main() { int i1 = 8, i2 = 10, *pi1 = &i1, *pi2 = &i2; swap_pointer(pi1, pi2); cout << *pi1 << " " << *pi2 << endl; getchar(); getchar(); return 0; }
习题6.23:
#include <iostream> #include <string> #include <vector> #include <iterator> using std::cin; using std::cout; using std::endl; using std::string; using std::vector; using std::begin; using std::end; void print(const int *pi)//显示整型变量 { if (pi) cout << *pi << endl; } void print(const char *pc)//显示字符数组 { if (pc) while (pc) cout << *pc++; } void print(const int *beg, const int *end)//利用标准库函数begin、end来显示整型数组 { while (beg != end) cout << *beg++ << endl; } void print(const int ia[], size_t size)//显式传递长度显示整型数组 { for (size_t i = 0; i != size; ++i) cout << ia[i] << endl; } void print(int (&arr)[2])//数组引用形参显示整型数组 { for (auto i : arr) cout << i << endl; } int main() { char ch[5] = "abcd"; int i = 0, j[2] = { 0, 1 }; print(&i); print(ch); print(begin(j), end(j)); print(j, end(j) - begin(j)); //print(j); getchar(); getchar(); return 0; }
习题6.27:
#include <iostream> #include <string> #include <vector> #include <iterator> #include <initializer_list> using std::cin; using std::cout; using std::endl; using std::string; using std::vector; using std::begin; using std::end; using std::initializer_list; int sum_list(initializer_list<int> ini) { int sum = 0; for (auto i : ini) sum += i; return sum; } int main() { initializer_list<int> ini({ 1, 2, 3, 4, 5, 6, 7, 8, 9 }); cout << sum_list(ini) << endl; getchar(); getchar(); return 0; }
习题6.33:
#include <iostream> #include <string> #include <vector> #include <iterator> #include <initializer_list> using std::cin; using std::cout; using std::endl; using std::string; using std::vector; using std::begin; using std::end; using std::initializer_list; using iter = vector<int>::const_iterator; void display(iter beg, iter end) { if (beg != end) { cout << *beg << " "; return display(++beg, end); } cout << endl; return; } int main() { vector<int> ivec{ 1, 2, 3, 4, 5 }; display(ivec.cbegin(),ivec.cend()); getchar(); getchar(); return 0; }
习题6.37:
string(&func1(string(&arrStr)[10]))[10]; using arrS = string(&)[10]; arrS func2(arrS arrStr); auto func3(string(&arrStr)[10])->string(&)[10]; string arrs[10] = { "a" }; decltype(arrs) &func(arrS arrStr);
习题6.38:
#include <iostream> #include <string> #include <vector> #include <iterator> #include <initializer_list> using std::cin; using std::cout; using std::endl; using std::string; using std::vector; using std::begin; using std::end; using std::initializer_list; int odd[] = { 1, 3, 5, 7, 9 }; int even[] = { 0, 2, 4, 6, 8 }; decltype(odd) &arrPtr(int i) { return (i % 2) ? odd : even; } int main() { cout << arrPtr(1)[0] << endl; getchar(); getchar(); return 0; }
习题6.42:
#include <iostream> #include <string> #include <vector> #include <iterator> #include <initializer_list> using std::cin; using std::cout; using std::endl; using std::string; using std::vector; using std::begin; using std::end; using std::initializer_list; string making_plural(size_t ctr, const string &word, const string &ending = "s") { return (ctr > 1) ? word + ending : word; } int main() { cout << making_plural(1, "success", "es") << endl; cout << making_plural(2, "success", "es") << endl; cout << making_plural(1, "failure") << endl; cout << making_plural(2, "failure") << endl; getchar(); getchar(); return 0; }
习题6.44:
#include <iostream> #include <string> #include <vector> #include <iterator> #include <initializer_list> using std::cin; using std::cout; using std::endl; using std::string; using std::vector; using std::begin; using std::end; using std::initializer_list; inline bool isShorter(const string &s1, const string &s2) { return s1.size() < s2.size(); } int main() { cout << std::boolalpha << isShorter("hhhh", "23333"); getchar(); getchar(); return 0; }
习题6.54:
#include <iostream> #include <string> #include <vector> #include <iterator> #include <initializer_list> using std::cin; using std::cout; using std::endl; using std::string; using std::vector; using std::begin; using std::end; using std::initializer_list; int func(int, int) { cout << "calling" << endl; return 0; } using f = int(int, int); int main() { vector<f *> fvec; fvec.push_back(&func); fvec[0](1, 2); getchar(); getchar(); return 0; }
习题6.55:
#include <iostream> #include <string> #include <vector> #include <iterator> #include <initializer_list> using std::cin; using std::cout; using std::endl; using std::string; using std::vector; using std::begin; using std::end; using std::initializer_list; int add(int a, int b) { return a + b; } int minus(int a, int b) { return a - b; } int multiply (int a, int b) { return a * b; } int divide(int a, int b) { return a / b; } using pf = decltype(add) *;//using pf = int (*)(int, int)等都可以 int main() { vector<pf> pfvec{ add, minus, multiply, divide }; for (auto f : pfvec) cout << f(3, 2) << endl; getchar(); getchar(); return 0; }
相关文章推荐
- c++教程
- [C++11 并发编程] 02 - join
- 摆脱VC++6.0,教你如何用visual Studio2010完美运行C++程序
- uva10801 Dijkstra
- 动态规划之Cash Machine
- Android调用C++OpenCV程序的方法(NDK+JNI)
- c++的可视化计算器
- Calculation Of Polynomial(多项式的运算) C++
- 【c++】虚基类
- C++中cin和cin.getline函数连用的问题
- POJ1125Floyd-warshall算法模板题
- C语言深度解剖——读书笔记-12、内存管理
- 《C++编程思想》 第十章 引用和拷贝构造函数(原书代码+知识点+习题+解答)
- C++ 智能指针详解
- 今天在网上看到一个百度的C++面试题目
- JNI_最简单的Java调用C/C++代码(自己整理)
- c++11 中的 move 与 forward
- QString、string相互转换
- C++11 标准新特性: 右值引用与转移语义
- c++实现基于单进程单客户编程模型的echo程序