Exercises Section 3.2.2

Ex3.2

// 一次读取一行
#include<iostream>
#include<string>using namespace std;int main()
{string line;while (getline(cin, line)){cout << line << endl;}system("pause");return 0;
}// 一次读取一个 word
#include<iostream>
#include<string>using namespace std;int main()
{string word;while (cin >> word){cout << word << endl;}system("pause");return 0;
}

Ex3.3

在 string 输入操作中, whitespace characters 被忽略;在  getline 函数中,whitespace characters 正常输出。

Ex3.4

#include<iostream>
#include<string>using namespace std;int main()
{string s1;string s2;cout << "Please enter two string: " << endl;getline(cin, s1);getline(cin, s2);//比较 s1 和 s2 的大小if (s1 == s2)       // s1 和 s2 的长度和内容都相等{cout << "s1 and s2 are equal" << endl;}else if (s1 < s2){cout << "The larger string is s2" << endl;}else {cout << "The larger string is s1" << endl;}// 比较 s1 和 s2 长度的大小if (s1.size() == s2.size()){cout << "The length of s1 equals the length of s2" << endl;}else if (s1.size() < s2.size()){cout << "s2 is longer" << endl;}else {cout << "s1 is longer" << endl;}system("pause");return 0;
}

Ex3.5

// 拼接 string
#include<iostream>
#include<string>using namespace std;int main()
{string s;string result = "";while (getline(cin, s)){result += s;}cout << result << endl;system("pause");return 0;
}// 分别输出 string
#include<iostream>
#include<string>
#include<vector>using namespace std;int main()
{string s;vector<string> result;while (getline(cin, s)){result.push_back(s);}for (string i : result){cout << i << " ";}cout << endl;system("pause");return 0;
}

Exercises Section 3.2.3

Ex3.6

#include<iostream>
#include<string>using namespace std;int main()
{string s;getline(cin, s);for (auto &c : s){c = 'X';}cout << s << endl;system("pause");return 0;
}

Ex3.7

不能得到预期的结果,这是因为首先复制了 string 的一个副本,只是对其副本进行了修改。

Ex3.8

#include<iostream>
#include<string>using namespace std;int main()
{string s;getline(cin, s);auto it = s.begin();while (it != s.end()){(*it) = 'X';++it;}cout << s << endl;system("pause");return 0;
}

Ex3.9

    string s;cout << s[0] << endl;

输出 string s 的第一个字符;有效

Ex3.10

#include<iostream>
#include<string>using namespace std;int main()
{string s;getline(cin, s);for (auto &c : s){if (!ispunct(c)){cout << c;}}cout << endl;system("pause");return 0;
}

Ex3.11

    const string s = "Keep out!";for (auto &c : s){ /* ... */ }

合法;c 的类型为 const char&

Exercises Section 3.3.1

Ex3.12

a) vector<vector<int>> ivec; // 合法;ivec 是一个 vector,里面的元素是 存储 int 类型的 vector
b) vector<string> svec = ivec;   // 不合法; 利用一个 vector 给另外一个 vector 赋值,必须保证两个 vector 内的元素类型一样
c) vector<string> svec(10, "null");     // 合法;svec是一个 vector,有 10 个元素,每个元素为字符串值"null"

Ex3.13

a) vector<int> v1; // 没有元素
b) vector<int> v2(10);    // 10 个元素,每个元素值为 0
c) vector<int> v3(10, 42);    // 10 个元素,每个元素值为 42
d) vector<int> v4{10};        // 1 个元素,值为 10
e) vector<int> v5{10, 42};    // 2 个元素,值为 10 和 42
f) vector<string> v6{10}; // 10 个元素,每个元素值为 ""
g) vector<string> v7{10, "hi"}; // 10 个元素,每个元素值为 "hi"

Exercises Section 3.3.2

Ex3.14

#include<iostream>
#include<vector>
using namespace std;int main()
{int value;vector<int> result;while (cin >> value){result.push_back(value);}for (auto i : result){cout << i << " ";}cout << endl;system("pause");return 0;
}

Ex3.15

#include<iostream>
#include<vector>
#include<string>
using namespace std;int main()
{string value;vector<string> result;while (getline(cin, value)){result.push_back(value);}for (auto s : result){cout << s << " ";}cout << endl;system("pause");return 0;
}

Exercises Section 3.3.3

Ex3.17

#include<iostream>
#include<vector>
#include<string>using namespace std;int main()
{string word;vector<string> result;while (cin >> word){result.push_back(word);}for (auto & s : result){for (auto &c : s){c = toupper(c);}}for (auto i = 0; i != result.size(); ++i){if (i % 8 != 0)cout << result[i] << " ";else {cout << endl;cout << result[i] << " ";}}cout << endl;system("pause");return 0;
}

Ex3.18

vector<int> ivec;ivec[0] = 42;

不合法;修改如下:

vector<int> ivec;
ivec.push_back(42);

Ex3.19

vector<int> v1 = {42, 42, 42, 42, 42, 42, 42, 42, 42, 42};
vector<int> v2(10, 42);
vector<int> v3 = v2;

Ex3.20

#include<iostream>
#include<vector>using namespace std;int main()
{int value;vector<int> result;int sum = 0;while (cin >> value){result.push_back(value);}for (auto i = result.begin(), j = i + 1; i != result.end(); ++i){sum = (*i) + (*j);cout << sum << " ";}cout << endl;for (auto i = result.begin(), j = result.end() - 1; i <= j; ++i, --j){if (i != j){sum = (*i) + (*j);cout << sum << " ";}else {sum = (*i);cout << sum << " ";}}cout << endl;system("pause");return 0;
}

Exercises Section 3.4.1

Ex3.23

#include<iostream>
#include<vector>using namespace std;int main()
{int value;vector<int> result;for (int i = 0; i < 10; ++i){cin >> value;result.push_back(2 * value);}for (auto item = result.begin(); item != result.end(); ++item){cout << *item << " ";}cout << endl;system("pause");return 0;
}

Exercises Section 3.4.2

Ex3.26

mid = beg + (end - beg) / 2;   // 推荐
mid = (beg + end) / 2;        // 不推荐

原因:第二种可能造成整数溢出

Exercises Section 3.5.1

Ex3.27

unsigned buf_size = 1024;
a) int ia[buf_size];        // 非法;buf_size 不是一个常量
b) int ia[4 * 7 - 14];      // 合法
c) int ia[txt_size()];      // 如果 txt_size() 返回的是常量 int 类型则合法
d) char st[11] = "fundamental";      // 非法;最后的'\0'占一个字符,故而数组大小不够

Ex3.28

string sa[10];           // 元素值全为空字符串
int ia[10];             // 元素值全为0
int main()
{string sa2[10];        // 元素值全为空字符串int ia2[10];        // 元素值不确定,取决于编译器
}

Ex3.29

array 大小确定,而 vector 大小可以不确定。

Exercises Section 3.5.2

Ex3.30

    constexpr size_t array_size = 10;       int ia[array_size];for (size_t ix = 1; ix <= array_size; ++ix)       // 下标为0-9,没有 10 下标ia[ix] = ix;

Ex3.31

#include<iostream>using namespace std;int main()
{int a[10];for (int i = 0; i < 10; ++i){a[i] = i;}for (int i = 0; i < 10; ++i){cout << a[i] << " ";}cout << endl;system("pause");return 0;
}

Ex3.32

#include<iostream>
#include<vector>using namespace std;int main()
{int a[10], b[10];vector<int> s1;vector<int> s2;for (int i = 0; i < 10; ++i){a[i] = i;}for (int i = 0; i < 10; ++i){b[i] = a[i];}for (auto i = 0; i < 10; ++i){s1.push_back(i);}for (vector<int>::const_iterator it = s1.begin(); it != s1.end(); ++it){s2.push_back(*it);}for (vector<int>::const_iterator it = s2.begin(); it != s2.end(); ++it){cout << *it << " ";}cout << endl;system("pause");return 0;
}

Ex3.33

未初始化的数组的元素值取决于编译器

Exercises Section 3.5.3

Ex3.34

p1 += p2 - p1; // 将指针 p2 的值赋给指针 p1;如果 p1 被 const 修饰则非法

Ex3.35

#include<iostream>using namespace std;int main()
{int a[10];int *p = &a[10];for (int *it = a; it != p; ++it){*it = 0;}for (int x : a){cout << x << " ";}cout << endl;system("pause");return 0;
}

Ex3.36

// 比较数组是否相等
#include<iostream>using namespace std;int main()
{int arr1[10];int arr2[10];int size_arr1 = end(arr1) - begin(arr1);int size_arr2 = end(arr2) - begin(arr2);if (size_arr1 != size_arr2) {cout << "Differen. " << endl;return 0;}for (int i = 0; i != size_arr1; ++i) {if (arr1[i] != arr2[i]) {cout << "Different. " << endl;return 0;}}cout << "Same arr. " << endl;system("pause");return 0;
}// 比较vector 是否相等
#include<iostream>
#include<vector>using namespace std;int main()
{vector<int> ivec1{1, 2, 3};vector<int> ivec2{1, 2, 3};if (ivec1 == ivec2) {cout << "Same vector. " << endl;}else {cout << "Different. " << endl;}system("pause");return 0;
}

Exercises Section 3.5.4

Ex3.37

#include<iostream>using namespace std;int main()
{const char ca[] = {'h', 'e', 'l', 'l', 'o'};const char *cp = ca;while (*cp){cout << *cp << endl;++cp;}system("pause");return 0;
}

Ex3.38

C++ 中没有指针加法

Ex3.39

// C char[]
#include<iostream>
#include<cstring>using namespace std;int main()
{const char ca1[] = "string-A";const char ca2[] = "string-B";if (strcmp(ca1 , ca2) > 0) {cout << "string-A big" << endl;} else {cout << "string-B big" << endl;}system("pause");return 0;
}// C++ string
#include<iostream>
#include<string>using namespace std;int main()
{string s1 = "string-A";string s2 = "string-B";cout << "Big one is: ";if (s1 > s2) {cout << s1 << endl;} else {cout << s2 << endl;}system("pause");return 0;
}

Ex3.40

#include<cstring>
#include<iostream>using namespace std;int main()
{char s1[100] = "stringA";char s2[] = "stringB";strcat(s1 , s2);char s[100];strcpy(s , s1);cout << s << endl;system("pause");return 0;
}

Exercises Section 3.5.5

Ex3.41

#include<iostream>
#include<vector>using namespace std;int main()
{int value;vector<int> v1;int a[10];for (int i = 0; i < 10; ++i){cin >> value;a[i] = value;}for (int i = 0; i < 10; ++i){v1.push_back(a[i]);}for (auto i : v1){cout << i << " ";}cout << endl;system("pause");return 0;
}

Ex3.42

#include<iostream>
#include<vector>using namespace std;int main()
{int value;vector<int> v;while (cin >> value){v.push_back(value);}int a[v.size()];for (int i = 0; i < v.size(); ++i){a[i] = v[i];}for (int i = 0; i < v.size(); ++i){cout << a[i] << " ";}system("pause");return 0;
}

Chapter 3: Strings、Vectors And Arrays相关推荐

  1. Chapter 3. Strings, Vectors and Arrays -C++ Primer 5 notes

    Chapter 3. Strings, Vectors and Arrays What does built-in types include? chapter 2 array maybe more? ...

  2. R语言数据类型:Logical、Numeric、Integer、Complex、Character、Vectors、Lists、Matrices、Arrays、Factors、DataFrames

    R语言数据类型:Logical.Numeric.Integer.Complex.Character.Vectors.Lists.Matrices.Arrays.Factors.DataFrames 通 ...

  3. Python 编程导论 Chapter 4 —— 函数、作用域与抽象

    typora-copy-images-to: Risk Management and Financial Institution typora-copy-images-to: Python 编程导论 ...

  4. Python爬虫之string、strings、stripped_strings、get_text和text用法区别

    Python爬虫获取html中的文本方法多种多样,这里主要介绍一下string.strings.stripped_strings和get_text用法 string:用来获取目标路径下第一个非标签字符 ...

  5. halcon算子盘点:Chapter 13:对象、Chapter 14 区域

    Chapter 13:Object 13.1 Information 1. count_obj  功能:统计一个元组中的对象. 2. get_channel_info  功能:一幅目标图像组成部分的信 ...

  6. Learn Java - Chapter 1 变量(Variables)-数组(Arrays)

    为什么80%的码农都做不了架构师?>>>    Java数组在被创建的时候确定数组长度.索引下标从0开始. 1.数组定义及初始化 int[] anArray;//定义 anArray ...

  7. java arraycollection_Java集合(三)--Collection、Collections和Arrays

    Collection: Collection是集合类的顶级接口,提供了对集合对象进行基本操作的通用接口方法.Collection接口的意义是为各种具体的集合提供了最大化 的统一操作方式,其直接继承接口 ...

  8. halcon的算子清点: Chapter 2-3-4 控制、开发、文件操作

    Chapter 2  Control 1.assign功能:为一个控制变量分配一个新值. 2.break  功能:终止循环执行. 3. comment  功能:向程序添加一行注释. 4. contin ...

  9. Strings、bytes and runes -- 就要学习Go语言

    原创文章,若需转载请注明出处! 欢迎扫码关注公众号「Golang来了」或者移步 www.seekload.net,查看更多精彩文章. Go 中的字符串值得特别关注,与其他语言相比,Go 中的字符串实现 ...

最新文章

  1. 北漂程序员的心酸:北漂六年了,没住过4000的房子
  2. 大数据工具篇之flume1.4-安装部署指南
  3. 正则截取指定字符串 php,php截取指定字符串除了正则还有什么方法
  4. 电脑桌面锁屏怎么设置_华为手机总是莫名多出照片?这两个设置不关闭,内存再大也不够用...
  5. 关于QSerialPort的使用说明(Qt实现串口工具)
  6. Codeforces Round #493 (Div. 2) C. Convert to Ones 乱搞_构造_好题
  7. LINQ是死是活?——很奇怪为什么会有这样的话题?
  8. 为什么不是基址寻址适用于数组_微机原理--8种寻址方式
  9. 如何自学python-如何自学Python编程呢?老男孩Python学习方法
  10. python随机森林变量重要性_随机森林如何评估特征重要性【机器学习面试题详解】...
  11. STL源码剖析(三)
  12. php存sqlite图片,SQLite数据库如何存储图片/语音
  13. 大华平台linux命令,大华平台软件简介.docx
  14. 最最牛的SQL客户端软件
  15. ffplay播放器原理学习
  16. Xcode5 symbolicatecrash文件路径
  17. 创意字体设计中主题类别有哪些呢?
  18. Github上Python超越Java,应届人工智能程序员年薪30w+
  19. Android N Idle模式分析
  20. Pure Virtual Function

热门文章

  1. 重磅资讯:《数据安全法》颁布,国家支持数据开发利用和数据安全技术研究
  2. 用python画雪人-萌系新潮玩,唱多多小雪人麦克风天猫独家首发
  3. LaTeX \genfrac 分式命令
  4. java 发送客服消息,Java调用微信客服消息实现发货通知的方法详解
  5. Kubernetes控制平面组件:Controller-Manager控制器管理
  6. python opencv写视频——cv2.VideoWriter()
  7. DMA PL330相关总结
  8. 将数组修改为峰谷相间
  9. 我是如何将Pluto作为library分享到jCenter
  10. uniapp文字穿插表情消息处理