练习3.1
略,前述就是用的using
练习3.2
(1)
#include "source.h"
#include <iostream>
using namespace std;
int main()
{
string S_test;
while(getline(cin,S_test))
{
cout << S_test << endl;
}
system("pause");
return 0;
}
(2)
#include <iostream>
using namespace std;
int main()
{
string S_test;
while(cin >> S_test)
{
cout << S_test << endl;
}
system("pause");
return 0;
}
练习3.3
getline会保留空白符,getline会在遇到输入回车时会结束,然后把
换行符之前的内容读到string里。
而cin遇到空白符或者回车都会结束。把之前的内容读到string里。
练习3.4
#include <iostream>
using namespace std;
#define testNum 1
int main()
{
string S_test1,S_test2;
cout << "please input the frist string" << endl;
cin >> S_test1;
cout << "please input the Second string" << endl;
cin >> S_test2;
#if testNum == 0
if (S_test1 == S_test2)
{
cout << S_test1 << endl;
}
else
{
if (S_test1 > S_test2)
{
cout << S_test1 << endl;
}
else
{
cout << S_test2 << endl;
}
}
#elif testNum == 1
if (S_test1.length() == S_test2.length())
{
cout << S_test1 << endl;
}
else
{
if (S_test1.length() > S_test2.length())
{
cout << S_test1 << endl;
}
else
{
cout << S_test2 << endl;
}
}
#endif
system("pause");
return 0;
}
练习3.5
#include <iostream>
using namespace std;
int main()
{
string S_test, S_testTotal;
while (getline(cin, S_test))
{
S_testTotal += S_test;
S_testTotal += " ";
cout << S_testTotal << endl;
}
system("pause");
return 0;
}
练习3.6
#include <iostream>
using namespace std;
int main()
{
string S_test;
S_test = "asdfghjkl";
for (auto& c : S_test)
{
c = 'x';
}
cout << S_test << endl;
system("pause");
return 0;
}
疑问:引用不是只能在初始化绑定一次吗?问什么能依次绑定序列的元素上
经过实验,发现语法中declaration 部分,每进入依次循环,都会定义一个新的变量
并初始化。
练习3.7
字符串没有被改变
练习3.8
#include <iostream>
using namespace std;
int main()
{
string S_test("asdfghjkl");
decltype(S_test.size()) i = 0;
while (i < S_test.size())
{
S_test[i++] = 'x';
}
cout << S_test << endl;
for (decltype(S_test.size()) i = 0; i < S_test.size(); i++)
{
S_test[i++] = 'x';
}
cout << S_test << endl;
system("pause");
return 0;
}
练习3.9
打印出s的第一个字符,但s没有初始化,打印无效,如果打印第二个
就会越界。
练习3.10
#include <iostream>
using namespace std;
int main()
{
string S_test;
cout << "please input a string" << endl;
getline(cin, S_test);
for (auto c : S_test)
{
if (!ispunct(c)) cout << c;
}
cout << endl;
system("pause");
return 0;
}
练习3.11
合法,const char&
练习3.12
b错误,svec和ivec的类型不同。
练习3.13
vector<int> v1; // 包含0个 ,元素无;
vector<int> v2(10); // 包含10个 ,元素无;
vector<int> v3(10, 42); //包含10个 ,元素42;
vector<int> v4{10}; // 包含1个 ,元素42;
vector<int> v5{10, 42}; //包含10个 ,元素42;
vector<string> v6{10}; //包含1个 ,元素10;
练习3.14
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> map;
int i;
while (cin >> i)
{
map.push_back(i);
cout << map.size() << endl;
}
system("pause");
return 0;
}
练习3.15
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> map;
string i;
while (getline(cin,i))
{
map.push_back(i);
cout << map.size() << endl;
}
system("pause");
return 0;
}
练习3.16
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1(10,42);
cout << "The size of vector is " << v1.size() << endl;
for (decltype(v1.size()) i = 0; i < v1.size(); i++)
{
cout << v1[i] << endl;
}
system("pause");
return 0;
}
练习3.17
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> v1;
string s;
while (cin >> s)
{
for (auto& c : s)
{
c = toupper(c);
}
v1.push_back(s);
for (decltype(v1.size()) i = 0; i < v1.size(); i++)
{
cout << v1[i] << endl;
}
}
system("pause");
return 0;
}
练习3.18
不合法,会导致越界。修改如下
vector<int> v1;
v1.push_back(42);
练习3.19
vector<int> v1(10);
vector<int> v2(v1);
vector<int> v3{...};
练习3.20
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;
int i;
cout << "please use world \'end\' to end input" << endl;
while (cin >> i && (getchar() != '\n'))
{
v1.push_back(i);
}
// the first question answer
for (decltype(v1.size()) a = 0; a < v1.size() - 1; a++)
{
cout << v1[a] + v1[a + 1] << ' ';
}
cout << endl;
// the second question answer
for (decltype(v1.size()) a = 0; a < v1.size()/2 + 1; a++)
{
cout << v1[a] + v1[v1.size() - 1 - a]<< ' ';
}
cout << endl;
system("pause");
return 0;
}
练习3.21
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> v1{"you are my gril" };
for (auto it = v1.begin(); it != v1.end(); it++)
{
cout << *it;
}
cout << endl;
system("pause");
return 0;
}
练习3.22
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> v1{"you are my gril" };
for (auto it = v1.begin(); it != v1.end(); it++)
{
for (auto it1 = (*it).begin(); it1 != (*it).end(); it1++)
{
*it1 = toupper(*it1);
}
cout << *it;
}
cout << endl;
system("pause");
return 0;
}
练习3.23
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1(10,1);
for (auto it = v1.begin(); it != v1.end(); it++)
{
*it *= 2;
cout << *it << ' ';
}
cout << endl;
system("pause");
return 0;
}
练习2.24
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;
int i;
cout << "please use world \'end\' to end input" << endl;
while (cin >> i && (getchar() != '\n'))
{
v1.push_back(i);
}
// the first question answer
for (auto it = v1.begin(); it != v1.end() - 1; it++)
{
cout << *it + *(it + 1) << ' ';
}
cout << endl;
// the second question answer
for (auto it = v1.begin(); it != (v1.begin() + (v1.end() - v1.begin()) / 2) + 1; it++)
{
auto dis = (v1.end() - v1.begin()) - 2 * (it - v1.begin()) - 1;
cout << *it + *(it + dis) << ' ';
}
cout << endl;
system("pause");
return 0;
}
练习2.25
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1(11,0);
int i;
auto it = v1.begin();
cout << "please use world \'end\' to end input" << endl;
while (cin >> i && (getchar() != '\n'))
{
if (i <= 100)
{
*(it + i / 10) += 1;
}
}
for (auto it = v1.begin(); it != v1.end(); it++)
{
cout << *it << ' ';
}
cout << endl;
system("pause");
return 0;
}
练习2.26
因为容器迭代器的 起点是begin,而不是0;
练习2.27
unsigned buf_size = 1024;
int ia[buf_size]; // 不合法,buf_size并不是常量值
int ia[4 * 7 - 14]; // 合法
int ia[txt_size()]; // 不合法txt_size()并不是常量值
char st[11] = "fundamental"; // 该string包含一个\n,大小并不是11而是12
练习3.28
sa 是空,ia是0,sa2,ia2都是未定义。
练习3.29
数组并不可以动态添加。
其他优点都算是vector的功能了。。。详情请看vector介绍
比如:vector初始化方便等等
练习3.30
ia的索引是0 - 9 ,当ix = 10 时就会越界。
练习3.31
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int arry[10];
int i = 0;
while (i < 10)
{
arry[i++] = i;
cout << arry[i -1] << endl;
}
system("pause");
return 0;
}
练习3.32
(a)
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int arry[10];
int arry1[10];
int i = 0;
while (i < 10)
{
arry[i++] = i;
}
i = 0;
while (i < 10)
{
arry1[i] = arry1[i];
i++;
}
system("pause");
return 0;
}
(b)
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> arry(10);
size_t i = 0;
while (i < 10)
{
arry[i++] = i;
}
vector<int> arry1(arry);
system("pause");
return 0;
}
练习3.33
不初始化,在全局就是0,在局部就是未定义。
练习3.34
略
练习3.35
略
练习3.36
#include <iostream>
#include <vector>
#include <iterator>
using std::begin;
using std::end;
using std::cout;
using std::endl;
using std::vector;
// pb point to begin of the array, pe point to end of the array.
bool compare(int* const pb1, int* const pe1, int* const pb2, int* const pe2)
{
if ((pe1 - pb1) != (pe2 - pb2)) // have different size.
return false;
else {
for (int *i = pb1, *j = pb2; (i != pe1) && (j != pe2); ++i, ++j)
if (*i != *j) return false;
}
return true;
}
int main()
{
int arr1[3] = {0, 1, 2};
int arr2[3] = {0, 2, 4};
if (compare(begin(arr1), end(arr1), begin(arr2), end(arr2)))
cout << "The two arrays are equal." << endl;
else
cout << "The two arrays are not equal." << endl;
cout << "==========" << endl;
vector<int> vec1 = {0, 1, 2};
vector<int> vec2 = {0, 1, 2};
if (vec1 == vec2)
cout << "The two vectors are equal." << endl;
else
cout << "The two vectors are not equal." << endl;
return 0;
}
练习3.37
输出结果不规则,这样定义数组,没有/0结束符,导致的情况
而直接赋值string则没有该情况。
练习3.38
得到的地址不可预料
练习3.39
#include <iostream>
#include <string>
#include <cstring>
using std::cout;
using std::endl;
using std::string;
int main()
{
// use string.
string s1("Mooophy");
string s2("Pezy");
if (s1 == s2)
cout << "same string." << endl;
else if (s1 > s2)
cout << "Mooophy > Pezy" << endl;
else
cout << "Mooophy < Pezy" << endl;
cout << "=========" << endl;
// use C-Style character strings.
const char* cs1 = "Wangyue";
const char* cs2 = "Pezy";
auto result = strcmp(cs1, cs2);
if (result == 0)
cout << "same string." << endl;
else if (result < 0)
cout << "Wangyue < Pezy" << endl;
else
cout << "Wangyue > Pezy" << endl;
return 0;
}
练习3.40
略
练习3.41
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int int_arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
vector<int> ivec(begin(int_arr), end(int_arr));
for (auto i : ivec)
{
cout << i << " ";
}
cout << endl;
system("pause");
return 0;
}
练习3.42
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int ia[3][4] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
// a range for to manage the iteration
for (const int(&p)[4] : ia)
for (int q : p) cout << q << " ";
cout << endl;
// ordinary for loop using subscripts
for (size_t i = 0; i != 3; ++i)
for (size_t j = 0; j != 4; ++j) cout << ia[i][j] << " ";
cout << endl;
// using pointers.
for (int(*p)[4] = ia; p != ia + 3; ++p)
for (int* q = *p; q != *p + 4; ++q) cout << *q << " ";
cout << endl;
system("pause");
return 0;
}
部分解决方法借鉴于
https://github.com/pezy/CppPrimer/blob/master/ch03/ex3_45.cpp