3-2
#include<iostream>
#include<string>
using namespace std;
int main()
{
int option;
while (cout<<"1 读入整行\n2 读入单词\n", cin >> option) {
if (option == 1) {
string s;
//循环读取,每次读取一行,getline会读取到输入流的换行符时停止,并且将换行符以外的全部字符保存
while (getline(cin, s)) {
cout << s << endl;
}
//清空输入流,不然EOF会对下次输入造成影响
cin.clear();
}
else if (option == 2) {
string s;
//循环读取,每次读取一个单词,通过cin读取字符串会自动忽略开头空白字符,并持续读取直到下一个空白字符
while (cin >> s) {
cout << s << endl;
}
//清空输入流,不然EOF会对下次输入造成影响
cin.clear();
}
}
system("pause");
return 0;
}
3-4
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1, s2;
cin >> s1 >> s2;
if (s1 >= s2) {
cout << s1 << endl;
}
else {
cout << s2 << endl;
}
cin >> s1 >> s2;
if (s1.size() >= s2.size()) {
cout << s1 << endl;
}
else {
cout << s2 << endl;
}
system("pause");
return 0;
}
3-5
#include<iostream>
#include<string>
using namespace std;
int main()
{
string sum, temp;
while (cin >> temp) {
sum += temp;
}
cout << sum << endl;
cin.clear();
sum = "";
while (cin >> temp) {
if (!sum.size()) {
sum += temp;
}
else {
sum = sum + " " + temp;
}
}
cout << sum << endl;
system("pause");
return 0;
}
3-6
#include<string>
using namespace std;
int main()
{
string str;
getline(cin, str);
for (auto &c : str) {
c = 'X';
}
cout << str << endl;
system("pause");
return 0;
}
3-8
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
cout << "please input a string:" << endl;
getline(cin, str);
//字符串索引
decltype(str.size()) i = 0;
while (i < str.size()) {
str[i] = 'X';
i++;
}
cout << str << endl;
cout << "please input a string:" << endl;
getline(cin, str);
for (i = 0; i < str.size(); i++) {
str[i] = 'X';
}
cout << str << endl;
system("pause");
return 0;
}
3-9
#include<iostream>
#include<vector>
using namespace std;
int main()
{
system("pause");
return 0;
}
3-10
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
string str, output;
getline(cin, str);
for (auto &ch : str) {
if (!ispunct(ch)) {
output += ch;
}
}
cout << output << endl;
system("pause");
return 0;
}
3-14
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v;
int tmp;
while (cin >> tmp) {
v.push_back(tmp);
cout << "继续吗(y/n)?"<<endl;
char ans = 'y';
cin >> ans;
if (ans == 'n' or ans == 'N') {
break;
}
}
for (auto i : v) {
cout << i << endl;
}
system("pause");
return 0;
}
3-15
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
vector<string> v;
string tmp;
while (cin >> tmp) {
v.push_back(tmp);
cout << "继续吗(y/n)?" << endl;
char ans = 'y';
cin >> ans;
if (ans == 'n' or ans == 'N') {
break;
}
}
for (auto i : v) {
cout << i << endl;
}
system("pause");
return 0;
}
3-16
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<int> v1;
vector<int> v2(10);
vector<int> v3(10, 42);
vector<int> v4{ 10 };
vector<int> v5{ 10, 42 };
vector<string> v6{ 10 };
vector<string> v7{10, "hi"};
cout << "第1个容器:" << endl;
cout << v1.size() << endl;
for (auto c : v1) {
cout << c << endl;
}
cout << "第2个容器:" << endl;
cout << v2.size() << endl;
for (auto c : v2) {
cout << c << " ";
}
cout << endl;
cout << "第3个容器:" << endl;
cout << v3.size() << endl;
for (auto c : v3) {
cout << c << " ";
}
cout << endl;
cout << "第4个容器:" << endl;
cout << v4.size() << endl;
for (auto c : v4) {
cout << c << " ";
}
cout << endl;
cout << "第5个容器:" << endl;
cout << v5.size() << endl;
for (auto c : v5) {
cout << c << " ";
}
cout << endl;
cout << "第6个容器:" << endl;
cout << v6.size() << endl;
for (auto c : v6) {
cout << c << " ";
}
cout << endl;
cout << "第7个容器:" << endl;
cout << v7.size() << endl;
for (auto c : v7) {
cout << c << " ";
}
cout << endl;
system("pause");
}
3-17
#include<iostream>
#include<string>
#include<vector>
#include<cctype>
using namespace std;
int main()
{
vector<string> v;
string tmp;
while (cin>>tmp) {
v.push_back(tmp);
char opt = 'y';
cout << "继续输入吗(y/n)?" << endl;
cin >> opt;
if (opt == 'n' or opt == 'N') {
break;
}
}
for (auto &item : v) {
for (auto &ch : item) {
ch = toupper(ch);
}
}
for (auto item : v) {
cout << item << endl;
}
system("pause");
}
3-20
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> vInt;
int iVal;
cout << "请输入一组数字:" << endl;
while (cin >> iVal) {
vInt.push_back(iVal);
}
cout << "相邻两项和:" << endl;
for (decltype(vInt.size()) i = 0; i < vInt.size() - 1;i++) {
cout << vInt[i] + vInt[i + 1] << " ";
}
cout << endl;
cout << "首尾项求和:" << endl;
for (decltype(vInt.size()) i = 0; (i < vInt.size()) and (i <= vInt.size() - i - 1); i++) {
//处理奇数情形
if (i == vInt.size() - i - 1) {
cout << vInt[i];
break;
}
cout << vInt[i] + vInt[vInt.size()-1-i] << " ";
}
cout << endl;
system("pause");
}
3-21
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<int> v1;
vector<int> v2(10);
vector<int> v3(10, 42);
vector<int> v4{ 10 };
vector<int> v5{ 10, 42 };
vector<string> v6{ 10 };
vector<string> v7{ 10, "hi" };
cout << "第1个容器:" << endl;
cout << v1.size() << endl;
for (auto e = v1.cbegin(); e != v1.cend(); e++) {
cout << *e << " ";
}
cout << endl;
cout << "第2个容器:" << endl;
cout << v2.size() << endl;
for (auto e = v2.cbegin(); e != v2.cend(); e++) {
cout << *e << " ";
}
cout << endl;
cout << "第3个容器:" << endl;
cout << v3.size() << endl;
for (auto e = v3.cbegin(); e != v3.cend(); e++) {
cout << *e << " ";
}
cout << endl;
cout << "第4个容器:" << endl;
cout << v4.size() << endl;
for (auto e = v4.cbegin(); e != v4.cend(); e++) {
cout << *e << " ";
}
cout << endl;
cout << "第5个容器:" << endl;
cout << v5.size() << endl;
for (auto e = v5.cbegin(); e != v5.cend(); e++) {
cout << *e << " ";
}
cout << endl;
cout << "第6个容器:" << endl;
cout << v6.size() << endl;
for (auto e = v6.cbegin(); e != v6.cend(); e++) {
cout << *e << " ";
}
cout << endl;
cout << "第7个容器:" << endl;
cout << v7.size() << endl;
for (auto e = v7.cbegin(); e != v7.cend(); e++) {
cout << *e << " ";
}
cout << endl;
system("pause");
}
3-22
#include<iostream>
#include<vector>
#include<string>
#include<cctype>
using namespace std;
int main()
{
vector<string> vStr;
string str;
//只输入回车取得空行
while (getline(cin, str)) {
vStr.push_back(str);
}
//遇到空元素说明段落结束
for (auto eStr = vStr.begin(); eStr != vStr.end() and !(*eStr).empty(); eStr++) {
for (auto eCh = (*eStr).begin(); eCh != (*eStr).end(); eCh++) {
*eCh = toupper(*eCh);
}
cout << *eStr << endl;
}
system("pause");
}
3-23
#include<vector>
#include<iostream>
using namespace std;
int main()
{
vector<int> vInt(10);
for (auto e = vInt.begin(); e != vInt.end(); e++) {
cout << "input a number:";
int i;
cin >> i;
*e = i;
}
for (auto e = vInt.begin(); e != vInt.end(); e++) {
*e *= 2;
cout << *e << " ";
}
cout << endl;
system("pause");
}
3-24
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> vInt;
int iVal;
while (cin >> iVal) {
vInt.push_back(iVal);
}
if (vInt.cbegin() == vInt.cend()) {
cout << "向量为空" << endl;
return -1;
}
auto begin = vInt.cbegin();
auto end = vInt.cend();
auto size = vInt.size();
cout << "邻项和:" << endl;
int count = 0;
for (auto it = begin; it <= end - 2; it++) {
count++;
cout << (*it + (*(++it))) << " ";
//每行输出5个结果
if (count % 5 == 0) {
cout << endl;
}
}
if (size % 2 != 0) {
cout << *(end - 1);
}
cout << endl;
cout << "首尾和:" << endl;
count = 0;
for (auto it = begin; it != (begin + size / 2 + 1); it++) {
//到中间了
count++;
if (it == (end - (it - begin) - 1)) {
cout << *it << " ";
break;
}
cout << *it + *(end - (it - begin) - 1) << " ";
if (count % 5 == 0) {
cout << endl;
}
}
cout << endl;
system("pause");
}
3-25
#include<iostream>
#include<vector>
using namespace std;
int main()
{
//分数段向量
vector<unsigned> vscore(11);
unsigned int score;
int count = 0;
auto it = vscore.begin();
while (cin >> score) {
if (score <= 100) {
(*(it + score / 10))++;
count++;
}
}
cout << "共" << count << "个有效成绩" << endl;
for (auto e = vscore.begin(); e != vscore.end(); e++) {
cout << *e << endl;
}
system("pause");
}
3-26
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> vInt = {-99, -45, -11, 0, 7, 11, 18, 56, 77, 99, 100};
int i;
cout << "input:";
cin >> i;
auto begin = vInt.begin();
auto end = vInt.end();
auto mid = begin + (end - begin) / 2;
while (mid!=end and i!=*mid) {
if (i < *mid) {
end = mid;
mid = begin + (end - begin) / 2;
}
else {
begin = mid + 1;
mid = begin + (end - begin) / 2;
}
}
if (mid != end) {
cout << *mid << endl;
}
else if (i == *mid) {
cout << *mid << endl;
}
else {
cout << "not found" << endl;
}
system("pause");
}
3-31
#include<iostream>
using namespace std;
int main()
{
int array[10], array2[10];
for (int i = 0; i < 10; i++) {
array[i] = i;
}
cout << "first array:" << endl;
for (auto i : array) {
cout << i << " ";
}
cout << endl;
for (int i = 0; i < 10; i++) {
array2[i] = array[i];
}
cout << "a copy of the first array:" << endl;
for (auto i : array2) {
cout << i << " ";
}
cout << endl;
system("pause");
}
3-32
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> vint1, vint2;
//使用了vector.size()的循环里面不要修改vector的大小
for (int i = 0; i < 10; i++) {
vint1.push_back((int)i);
}
cout << "first vector:" << endl;
for (auto i : vint1) {
cout << i << " ";
}
cout << endl;
//vector支持拷贝操作,不用像数组复制起来那么繁琐
vint2 = vint1;
cout << "a copy of the first vector:" << endl;
for (auto i : vint1) {
cout << i << " ";
}
cout << endl;
system("pause");
}
3-35
#include<iostream>
#include<iterator>
using namespace std;
int main()
{
const int sz = 10;
int arr[sz];
int *pstart = begin(arr);
int *pend = end(arr);
for (int *pnow = pstart; pnow < pend; pnow++) {
*pnow = pnow - pstart;
cout << *pnow << " ";
}
cout << endl;
for (int *pnow = pstart; pnow < pend; pnow++) {
*pnow = 0;
cout << *pnow << " ";
}
cout << endl;
system("pause");
}
3-36
#include<iostream>
#include<iterator>
using namespace std;
int main()
{
int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[10];
//第二个数组的首尾指针
int *pstart2 = begin(arr2);
int *pend2 = end(arr2);
cout << "input 10 numbers:" << endl;
for (int *pnow = pstart2; pnow < pend2; pnow++) {
cin >> *pnow;
}
bool flag = true;
//第一个数组的首尾指针
int *pstart1 = begin(arr1);
int *pend1 = end(arr1);
for (int *pnow1 = pstart1, *pnow2 = pstart2;
pnow1 < pend1, pnow2 < pend2;
pnow1++, pnow2++)
{
if (*pnow1 != *pnow2) {
flag = false;
break;
}
}
flag ? cout << "equal"<<endl : cout << "not equal"<<endl;
//对于vector来说方法是一样的,只不过把指针改为迭代器而已
system("pause");
}
3-39
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int main()
{
string str1, str2;
cin >> str1 >> str2;
if (str1 > str2) {
cout << str1 << "大于" << str2 << endl;
}
else if (str1 == str2) {
cout << str1 << "等于" << str2 << endl;
}
else {
cout << str1 << "小于" << str2 << endl;
}
char cstr1[100], cstr2[100];
cin >> cstr1 >> cstr2;
int res = strcmp(cstr1, cstr2);
if (res > 0) {
cout << cstr1 << "大于" << cstr2 << endl;
}
else if (res == 0) {
cout << cstr1 << "等于" << cstr2 << endl;
}
else {
cout << cstr1 << "小于" << cstr2 << endl;
}
system("pause");
}
3-40
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char cstr1[10] = "Hello,";
char cstr2[10] = "C++!";
char cstr3[20];
strcat(cstr1, cstr2);
strcpy(cstr3,cstr1);
cout << cstr3 << endl;
system("pause");
}
3-41
#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
vector<int> v(begin(arr), end(arr));
for (auto i : v) {
cout << i << " ";
}
cout << endl;
system("pause");
}
3-42
#include<iostream>
#include<vector>
#include<iterator>
using namespace std;
int main()
{
const int size = 50;
vector<int> v;
for (int i = 0; i < size; i++)
{
v.push_back(i);
cout << i << " ";
}
cout << endl;
//赋值给一个整型数组
int arr[size];
auto it = v.cbegin();
//范围for语句遍历数组
for (auto &i : arr) {
i = *it;
cout << *it << " ";
it++;
}
cout << endl;
system("pause");
}
3-43
#include<iostream>
#include<iterator>
using namespace std;
int main()
{
//定义并且初始化一个二维数组ia
int ia[3][4] = {
{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10, 11}
};
//range-based for loop
for (int (&p)[4] : ia) {
for (int i : p) {
cout << i << " ";
}
}
cout << endl;
//index based ordinary for loop
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cout << ia[i][j] << " ";
}
}
cout << endl;
//pointer based ordinary for loop
for (int(*poutter)[4] = begin(ia); poutter < end(ia); poutter++) {
for (int *pinner = begin(*poutter); pinner < end(*poutter); pinner++) {
cout << *pinner << " ";
}
}
cout << endl;
system("pause");
}
3-44
#include<iostream>
#include<iterator>
using namespace std;
int main()
{
//定义并且初始化一个二维数组ia
int ia[3][4] = {
{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10, 11}
};
using int_array = int[4];
//range-based for loop
for (int_array &p : ia) {
for (int i : p) {
cout << i << " ";
}
}
cout << endl;
//index based ordinary for loop
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
cout << ia[i][j] << " ";
}
}
cout << endl;
//pointer based ordinary for loop
using p_outter_array = int(*)[4];
//typedef int(*outter_array)[4];
using p_inner_array = int*;
//typedef int *p_inner_array;
for (p_outter_array poutter = begin(ia); poutter < end(ia); poutter++) {
for (p_inner_array pinner = begin(*poutter); pinner < end(*poutter); pinner++) {
cout << *pinner << " ";
}
}
cout << endl;
system("pause");
}
3-45
#include<iostream>
#include<iterator>
using namespace std;
int main()
{
//定义并且初始化一个二维数组ia
int ia[3][4] = {
{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10, 11}
};
//range-based for loop
for (auto &p : ia) {
for (auto i : p) {
cout << i << " ";
}
}
cout << endl;
//index based ordinary for loop
for (auto i = 0; i < 3; i++) {
for (auto j = 0; j < 4; j++) {
cout << ia[i][j] << " ";
}
}
cout << endl;
for (auto poutter = begin(ia); poutter < end(ia); poutter++) {
for (auto pinner = begin(*poutter); pinner < end(*poutter); pinner++) {
cout << *pinner << " ";
}
}
cout << endl;
system("pause");
}