![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++ primer5 第三章
米兰的枫
这个作者很懒,什么都没留下…
展开
-
C++ primer5 3.43/3.44/3.45
3.43 输出多维数组的元素,版本1:使用范围for语句,版本2:使用普通for语句+下标运算符,版本3:使用普通for语句+指针。3.44 修改上一个程序,使用类型别名代替循环控制变量的类型。 3.45 再次改写上一个程序,使用auto关键字代替循环控制变量的类型。//3.43输出多维数组的元素#include <iostream>#include <vector>using namespace std;int main(){ int arr[3][4] =原创 2021-02-28 15:11:17 · 109 阅读 · 0 评论 -
C++ primer5 3.42
将含有整型元素的vector对象拷贝给一个整型数组//3.42#include <iostream>#include <vector>using namespace std;int main(){ vector<int> vec = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int arr[10] = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; for (auto i : arr)原创 2021-02-28 12:10:44 · 49 阅读 · 0 评论 -
C++ primer5 3.41
用整型数组初始化一个vector对象//3.41#include <iostream>#include <vector>using namespace std;int main(){ int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for (auto i : arr) cout << i << " "; //输出数组中的元素内容 cout << endl;原创 2021-02-28 12:17:11 · 70 阅读 · 0 评论 -
C++ primer5 3.40
定义两个字符数组并用字符串字面值初始化它们:接着再定义一个字符数组存放前两个数组连接后的结果//3.40#include <iostream>#include <cstring> //C风格字符串const char cstr1[]="Hello";const char cstr2[]="world!";int main(){ //C++11中的constexpr指定的函数返回值和参数必须要保证是字面值, //等于说是告诉编译器 “请大胆地将我看成编译时原创 2021-02-27 21:49:46 · 95 阅读 · 0 评论 -
C++ primer5 3.39
比较两个string对象,再比较两个C风格字符串的内容相等性//3.36#include <iostream>#include <string>#include <cstring> //C语言头文件string.h的C++版本-C风格字符串using namespace std;int main(){ //使用C++ string string s1("huang"), s2("dong"); if(s1 == s2) //比较标准库stri原创 2021-02-27 21:24:19 · 70 阅读 · 0 评论 -
C++ primer5 3.36
比较两个数组是否相等,再比较两个vector对象是否相等//3.36#include <iostream>#include <string>#include <vector>using namespace std;int main(){ int a[10] = {0,1,2,3,4,5,6,7,8,9}; int b[10] = {0,1,2,3,4,5,6,7,8,10}; int Equalflag = 0; if (sizeof(a) ==原创 2021-02-27 21:00:03 · 71 阅读 · 0 评论 -
C++ primer5 3.35
利用指针将数组中的元素置0//3.32#include <iostream>#include <vector>using namespace std;int main(){ const int size = 10; int arr[size]; for (auto ptr = arr; ptr != arr + size; ++ptr) *ptr = 0; //指针遍历将数组中的元素置0 for (auto i : arr)原创 2021-02-27 17:23:41 · 192 阅读 · 0 评论 -
C++ primer5 3.31/3.12
定义一个含有10个int的数组,令数组中每个元素的值都是其下标值//3.31#include <iostream>#include <string>#include <vector>using namespace std;int main(){ int arr[10]; for (auto i = 0; i < 10; ++i) arr[i] = i; //数组每个元素值 = 下标值 for (auto i : arr)原创 2021-02-27 16:56:41 · 68 阅读 · 0 评论 -
C++ primer5 3.25
读入一组成绩值,成绩取值在0~100,以10分作为一个分数段,统计各个分数段上各有多少个成绩,用迭代器实现//3.25#include <iostream>#include <string>#include <vector>using namespace std;int main(){ vector<int> grade(10,0);//vector对象grade中存入各区间段分数个数的初始值:都为0个 int number; while原创 2021-02-27 14:40:06 · 67 阅读 · 0 评论 -
C++ primer5 3.24
读入一组整数并存入vector对象,将每对相邻整数的和输出,然后依次对称输出前后两端两个元素的和。注意:用迭代器实现//3.23#include <iostream>#include <string>#include <vector>using namespace std;int main(){ vector<int> vec; //vector对象vec中存入一组整数 for (int i; cin >> i; vec.原创 2021-02-27 14:13:43 · 73 阅读 · 0 评论 -
C++ primer5 3.23
创建一个含有10个整数的vector对象,然后使用迭代器将所有元素的值都变成原来的两倍并输出vector对象的内容//3.23#include <iostream>#include <string>#include <vector>using namespace std;int main(){ //vector<int> text(10,2); //vector对象text中存入10个数字,值都为2 vector<int> te原创 2021-02-27 13:28:00 · 62 阅读 · 0 评论 -
C++ primer5 3.17
从cin读入一组词并把它们存入vector对象,把所有的词都改为大写形式并输出转换后的结果,每个词占一行//3.17 #include <iostream>#include <vector>#include <string>using namespace std;int main(){ vector<string> vec; for (string word; cin >> word; vec.push_back(wo原创 2021-02-27 13:17:22 · 152 阅读 · 0 评论 -
C++primer5 3.20a
这里写自定义目录标题//3.20 #include <iostream>#include <vector>using namespace std;int main(){ vector<int> ivec; for (int i; cin >> i; ivec.push_back(i));//读入一组整数存入vector对象,循环体为空 if (ivec.empty()) { cout <&l原创 2021-02-27 12:43:43 · 88 阅读 · 0 评论