3.1、
3.2、
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string str;
//while(getline(cin, str)) //读取整行
while(cin >> str) //读取整列
cout << str << endl;
return 0;
}
3.3
string 类的输入运算符:在遇到第一个可打印字符前忽略所有的空白字符;获得输入遇到换行符或第一个空格字符读取结束。
getline将输入的空白字符读入并存储,并在遇到第一个换行符读入并丢弃;
3.4
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
string str1, str2;
cout << "Please enter string1 : " << endl;
getline(cin, str1);
cout << "Please enter string2 : " << endl;
getline(cin, str2);
if(str1.size() == str2.size())
cout << "length of str1 same as str2 " << endl;
else if (str1.size() > str2.size())
cout << "str1: " << str1 << endl;
else
cout << "str2: " << str2 << endl;
return 0;
}
3.5
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
string str1("\0");
string str2("\0");
while(cin >> str2)
str1 += str2 + " ";
cout << str1 << endl;
return 0;
}
3.6
VS 2019 comunity 编译通过
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
string str1;
getline(cin, str1);
cout << str1 << endl;
for (auto &c : str1)
c = 'X';
cout << str1 << endl;
return 0;
}
{
string str1;
getline(cin, str1);
cout << str1 << endl;
for (auto &c : str1)
c = 'X';
cout << str1 << endl;
return 0;
}
3.7
char 类型引用可以
3.8
3.9
3.10
VS 2019 comunity 编译通过
//p3_10.cpp
#include <iostream>
#include <string>
#include <cctype>
using std::cout;
using std::cin;
using std::endl;
using std::string;
#include <iostream>
#include <string>
#include <cctype>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
string result;
string str1 = "Hellow i'm a world!!! with some punctions.";
for (auto& c : str1)
{
if (!ispunct(c))
result += c;
}
{
string result;
string str1 = "Hellow i'm a world!!! with some punctions.";
for (auto& c : str1)
{
if (!ispunct(c))
result += c;
}
cout << str1 << endl;
cout << result << endl;
cout << result << endl;
return 0;
}
3.11
3.12
a) 正确; 元素int的vector的vector;
b) 错误;类型不匹配
c) 正确:10 个 "null" string;
3.13
a 空int b 10个int
c 10 个int 42; d 一个10
e 两个 10,42; f 10个string
g 10个“hi"
3.14
//p3_14.cpp -- 读入整数放入vector
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
vector<int> valu;
int n;
while(cin>>n)
valu.push_back(n);
for(const int &c : valu)
cout << c << endl;
return 0;
}
3.15
//p3_15.cpp -- 读入字符串放入vector
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
int main()
{
vector<string> str;
string temp;
while(getline(cin,temp))
valu.push_back(temp);
for (const string &str1 : str)
cout << str1 << endl;
return 0;
}
3.16
//p3_16.cpp
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
int main()
{
vector<int> v1; //a
vector<int> v2(10); //b
vector<int> v3(10, 42); //c
vector<int> v4{10}; //d
vector<int> v5{10, 42}; //e
vector<string> v6{10}; //f
vector<string> v7{10, "hi"};//g
if (!v1.empty())
{
cout << "size of v1 " << v1.size() << endl;
for(auto &c : v1)
cout << c << " ";
cout << endl;
}
else
{
cout << "size of v1 " << v1.size() << endl;
}
if (!v2.empty())
{
cout << "size of v2 " << v2.size() << endl;
for(auto &c : v2)
cout << c << " ";
cout << endl;
}
else
{
cout << "size of v2 " << v2.size() << endl;
}
if (!v3.empty())
{
cout << "size of v3 " << v3.size() << endl;
for(auto &c : v3)
cout << c << " ";
cout << endl;
}
else
{
cout << "size of v3 " << v3.size() << endl;
}
if (!v4.empty())
{
cout << "size of v4 " << v4.size() << endl;
for(auto &c : v4)
cout << c << " ";
cout << endl;
}
else
{
cout << "size of v4 " << v4.size() << endl;
}
if (!v5.empty())
{
cout << "size of v5 " << v5.size() << endl;
for(auto &c : v5)
cout << c << " ";
cout << endl;
}
else
{
cout << "size of v5 " << v5.size() << endl;
}
if (!v6.empty())
{
cout << "size of v6 " << v6.size() << endl;
for(auto &c : v6)
cout << c << " ";
cout << endl;
}
else
{
cout << "size of v6 " << v6.size() << endl;
}
if (!v7.empty())
{
cout << "size of v7 " << v7.size() << endl;
for(auto &c : v7)
cout << c << " ";
cout << endl;
}
else
{
cout << "size of v7 " << v7.size() << endl;
}
return 0;
}
3.17
VS 2019 Comunity 编译通过并运行成功
//p3_17.cpp -- 转成大写输出
#include <iostream>
#include <cctype>
#include <string>
#include <vector>
using namespace std;
#include <iostream>
#include <cctype>
#include <string>
#include <vector>
using namespace std;
int main()
{
string temp;
vector<string> str;
while (cin >> temp)
str.push_back(temp);
for (auto& str1 : str)
{
for (auto& str2 : str1)
str2 = toupper(str2);
cout << str1 << endl;
}
{
string temp;
vector<string> str;
while (cin >> temp)
str.push_back(temp);
for (auto& str1 : str)
{
for (auto& str2 : str1)
str2 = toupper(str2);
cout << str1 << endl;
}
return 0;
}
}
3.18
不合法
int temp;
cin >> temp;
ivec.push_back(temp);
3.19
vector<int> ivec1(10,42);
vector<int> ivec2{42, 42, 42, 42, 42, 42, 42, 42, 42, 42};
vector<int> ivec3 = ivec1;
3.20
//p3_20.cpp -- 计算相邻整数和
#include <iostream>
#include <cctype>
#include <string>
#include <vector>
using namespace std;
int main()
{
int temp;
vector<int> ivec;
while(cin >> temp)
ivec.push_back(temp);
for(auto &n : ivec)
cout << n << " ";
cout << endl;
vector<int>::size_type n = ivec.size(); //记录ivec的大小
//打印两两相邻的和
for (vector<int>::size_type i = 0; i != n-1; ++i)
cout << ivec[i] + ivec[i+1] << " ";
cout << endl;
//打印首尾和
for (vector<int>::size_type i = 0; i != n/2; ++i)
cout << ivec[i] + ivec[n-1-i] << " ";
if(n%2 != 0) //如果元素为奇数个
cout << 2*ivec[n/2]; //将居中的元素*2打印
cout << endl;
return 0;
}
3.21
VS 2019 Comunity 编译并运行通过
//p3_21 -- 用迭代器实现vector容量和元素的显示
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
int main()
{
vector<int> v1; //a
vector<int> v2(10); //b
vector<int> v3(10, 42); //c
vector<int> v4{10}; //d
vector<int> v5{10, 42}; //e
vector<string> v6{10}; //f
vector<string> v7{10, "hi"};//g
if (!v1.empty())
{
cout << "size of v1 " << v1.size() << endl;
for(auto c = v1.cbegin(); c != v1.cend(); ++c)
cout << *c << " ";
cout << endl;
}
else
{
cout << "size of v1 " << v1.size() << endl;
}
if (!v2.empty())
{
cout << "size of v2 " << v2.size() << endl;
for(auto c = v2.cbegin(); c != v2.cend(); ++c)
cout << *c << " ";
}
else
{
cout << "size of v2 " << v2.size() << endl;
}
if (!v3.empty())
{
cout << "size of v3 " << v3.size() << endl;
for(auto c = v3.cbegin(); c != v3.cend(); ++c)
cout << *c << " ";
cout << endl;
}
else
{
cout << "size of v3 " << v3.size() << endl;
}
if (!v4.empty())
{
cout << "size of v4 " << v4.size() << endl;
for(auto c = v4.cbegin(); c != v4.cend(); ++c)
cout << *c << " ";
cout << endl;
}
else
{
cout << "size of v4 " << v4.size() << endl;
}
if (!v5.empty())
{
cout << "size of v5 " << v5.size() << endl;
for(auto c = v5.cbegin(); c != v5.cend(); ++c)
cout << *c << " ";
cout << endl;
}
else
{
cout << "size of v5 " << v5.size() << endl;
}
if (!v6.empty())
{
cout << "size of v6 " << v6.size() << endl;
for(auto c = v6.cbegin(); c != v6.cend(); ++c)
cout << *c << " ";
cout << endl;
}
else
{
cout << "size of v6 " << v6.size() << endl;
}
if (!v7.empty())
{
cout << "size of v7 " << v7.size() << endl;
for(auto c = v7.cbegin(); c != v7.cend(); ++c)
cout << *c << " ";
cout << endl;
}
else
{
cout << "size of v7 " << v7.size() << endl;
}
return 0;
}
3.22
for(auto it = text.cbegin(); it != text.cend() && !it->empty(); ++it)
{
for(auto it2 = it->cbegin(); it2 != it->cend(); ++it2)
*it2 = toupper(*it2);
cout << *it << endl;
}
3.23
//p3_23.cpp
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
int main()
{
vector<int> ivec(10);
int i = 1;
for (auto it = ivec.begin(); it != ivec.end(); ++it)
*it = i++;
{
vector<int> ivec(10);
int i = 1;
for (auto it = ivec.begin(); it != ivec.end(); ++it)
*it = i++;
for (auto it = ivec.cbegin(); it != ivec.cend(); ++it)
cout << *it << " ";
cout << endl;
cout << *it << " ";
cout << endl;
for (auto it = ivec.begin(); it != ivec.end(); ++it)
*it += *it;
*it += *it;
for (auto it = ivec.cbegin(); it != ivec.cend(); ++it)
cout << *it << " ";
cout << endl;
}
cout << *it << " ";
cout << endl;
}
3.24
//p3_24.cpp
#include <iostream>
#include <cctype>
#include <string>
#include <vector>
using namespace std;
int main()
{
int temp;
vector<int> ivec(10);
for (auto i = 0; i != 10; ++i)
ivec[i] = i + i / 2;
for (auto i = ivec.cbegin(); i != ivec.cend(); ++i)
cout << *i << " ";
cout << endl;
//打印两两相邻的和
for (auto i = ivec.cbegin(); i != ivec.cend()-1; ++i)
cout << *i + *(i + 1) << " ";
cout << endl;
//打印首尾和
for (auto i = ivec.cbegin(), i2 = ivec.cend(); i != ivec.cbegin() + (ivec.cend() - ivec.cbegin()) / 2; ++i)
{
i2--;
cout << *i + *i2 << " ";
}
cout << endl;
return 0;
}
3.25
//p3_25.cpp
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<unsigned> scores(11,0); //11个分数段,全部初始化为0
unsigned grade ;
auto vecbegin = scores.cbegin();
while (cin >> grade)
{
if(grade <= 100)
++*(vecbegin+grade/10);
}
for(auto it = scores.cbegin(); it != scores.cend; ++it)
cout << *it << " ";
cout << endl;
return 0;
}
3.27
a) 不合法buf_size不是常量 b) 合法
c)如果txt_size()被声明为constexpr时正确,否则不正确
d) 不正确,维度不一致,字符串末尾会自动添加一个空字符
3.28
sa 十个空字符串
ia 十个0
sa2 十个无意义的字符串
ia2 十个未初始化的int
3.29
不能拷贝和赋值
数组大小确定,不能随意向数组中增加元素
3.30
数组的下标范围是0~array_size -1
3.34
p1指向p2所指向的位置
3.35
3.36
//p3_36.cpp -- 比较数组或vector是否相等
#include <iostream>
#include <vector>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
using std::begin;
using std::end;
using std::endl;
using std::vector;
using std::begin;
using std::end;
int main()
{
int arr1[10] = { 0,1,2,3 };
int arr2[10] = { 0,1,2,3,4 };
{
int arr1[10] = { 0,1,2,3 };
int arr2[10] = { 0,1,2,3,4 };
vector<int> ivec1(10, 0);
vector<int> ivec2(begin(arr2), end(arr2));
vector<int> ivec2(begin(arr2), end(arr2));
int i;
for (i = 0; i < 10; ++i)
{
if (arr1[i] != arr2[i])
break;
}
if (i == 10)
cout << "arr1 == arr2" << endl;
else
{
cout << "arr1 != arr2" << endl;
}
for (i = 0; i < 10; ++i)
{
if (arr1[i] != arr2[i])
break;
}
if (i == 10)
cout << "arr1 == arr2" << endl;
else
{
cout << "arr1 != arr2" << endl;
}
if (ivec1 == ivec2)
cout << "ivec1 = ivec2" << endl;
else
{
cout << "ivec1 != ivec2" << endl;
}
cout << "ivec1 = ivec2" << endl;
else
{
cout << "ivec1 != ivec2" << endl;
}
return 0;
}
}
3.37
程序创建了一个字符数组
将输出hello后继续输出无意义的值直到遇到第一个空字符
3.38
指针的值表示地址的编号,量个编号相加当然没有意义
3.39
//3_39.cpp -- 比较字符串
#include <iostream>
#include <cstring>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::strcmp;
int main()
{
string str1;
string str2;
char cstr1[100] = "hello world";
char cstr2[100] = "hello world";
char cstr3[100] = "hello world!!!";
getline(cin, str1);
getline(cin, str2);
cout << "cstr1: " << cstr1 << endl;
cout << "cstr2: " << cstr2 << endl;
cout << "cstr3: " << cstr3 << endl;
if (!strcmp(cstr1, cstr2))
cout << "cstr1 == cstr2" << endl;
else
cout << "cstr1 != cstr2" << endl;
if (!strcmp(cstr1, cstr3))
cout << "cstr1 == cstr3" << endl;
else
cout << "cstr1 != cstr3" << endl;
if (str1 == str2)
cout << "str1 == str2" << endl;
else
{
cout << "str1 != str2" << endl;
}
return 0;
}
3.40
//p3_40.cpp -- 使用strcpy strcat
#include <iostream>
#include <cstring>
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
using std::endl;
int main()
{
char cstr1[] = { 'h','e','l','l','o','\0' };
char cstr2[] = { 'w','o','r','l','d','\0' };
char cstr3[100];
cout << "cstr1: " << cstr1 << endl;
cout << "cstr2: " << cstr2 << endl;
{
char cstr1[] = { 'h','e','l','l','o','\0' };
char cstr2[] = { 'w','o','r','l','d','\0' };
char cstr3[100];
cout << "cstr1: " << cstr1 << endl;
cout << "cstr2: " << cstr2 << endl;
strcpy(cstr3, cstr1);
strcat(cstr3, cstr2);
cout << "cstr3: " << cstr3 << endl;
strcat(cstr3, cstr2);
cout << "cstr3: " << cstr3 << endl;
return 0;
}
}
3.41 3.42
//p3_41.cpp -- 3.41 and 3.42
#include <iostream>
#include <vector>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
using std::begin;
using std::end;
using std::endl;
using std::vector;
using std::begin;
using std::end;
int main()
{
int iarr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int iarr2[10];
vector<int> ivec(begin(iarr), end(iarr)); //用数组初始化vector
int i = 0;
for (auto& i_ivec : ivec)
iarr2[i++] = i_ivec;
cout << "iarr[10]: ";
for (i = 0; i != 10; ++i)
cout << iarr[i]<< " ";
cout << endl;
cout << "ivec(10): ";
for (i = 0; i != 10; ++i)
cout << ivec[i]<< " ";
cout << endl;
cout << "iarr2[10]: ";
for (i = 0; i != 10; ++i)
cout << iarr2[i]<< " ";
cout << endl;
{
int iarr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int iarr2[10];
vector<int> ivec(begin(iarr), end(iarr)); //用数组初始化vector
int i = 0;
for (auto& i_ivec : ivec)
iarr2[i++] = i_ivec;
cout << "iarr[10]: ";
for (i = 0; i != 10; ++i)
cout << iarr[i]<< " ";
cout << endl;
cout << "ivec(10): ";
for (i = 0; i != 10; ++i)
cout << ivec[i]<< " ";
cout << endl;
cout << "iarr2[10]: ";
for (i = 0; i != 10; ++i)
cout << iarr2[i]<< " ";
cout << endl;
return 0;
}
}
3.43
//p3_43.cpp -- 多个版本处理多维数组
#include <iostream>
#include <iostream>
using namespace std;
int main()
{
int ia[3][4] = {
{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10, 11}
};
{
int ia[3][4] = {
{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10, 11}
};
//版本1
cout << "version1: " << endl;
for (auto& row : ia)
{
for (auto col : row)
cout << col << " ";
cout << endl;
}
cout << "version1: " << endl;
for (auto& row : ia)
{
for (auto col : row)
cout << col << " ";
cout << endl;
}
//版本2
cout << "version2: " << endl;
for (int row = 0; row < 3; ++row)
{
for (int col = 0; col < 4; ++col)
cout << ia[row][col] << " ";
cout << endl;
}
cout << "version2: " << endl;
for (int row = 0; row < 3; ++row)
{
for (int col = 0; col < 4; ++col)
cout << ia[row][col] << " ";
cout << endl;
}
//版本3
cout << "version3: " << endl;
for (int(*iptr_r)[4] = ia; iptr_r != ia + 3; ++iptr_r)
{
for (int* iptr_c = *iptr_r; iptr_c != *iptr_r + 4; iptr_c++)
cout << *iptr_c << " ";
cout << endl;
}
cout << "version3: " << endl;
for (int(*iptr_r)[4] = ia; iptr_r != ia + 3; ++iptr_r)
{
for (int* iptr_c = *iptr_r; iptr_c != *iptr_r + 4; iptr_c++)
cout << *iptr_c << " ";
cout << endl;
}
return 0;
}
}