//3.2.1
#include<iostream>
#include<string>
using namespace std;
int main()
{
string line;
cout << "请输入你要输入的字符串,一行:" << endl;
while (getline(cin, line))
{
cout << line << endl;
}
return 0;
}
//3.2.2
#include<iostream>
#include<string>
using namespace std;
int main()
{
string word;
cout << "请输入你要输入的字符串,一个词:" << endl;
while (cin>>word)
{
cout << word << endl;
}
return 0;
}
3.3 标准库string的输入运算符自动忽略开头的空格、换行、制表符等,从第一个真正的字符开始读起,知道遇见下一个空白。
getline从任意字符开始,知道遇见换行符。
//3.4.1
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1,str2;
cout << "请输入两个字符串:" << endl;
cin >> str1 >> str2;
if(str1==str2)
{
cout <<"epual"<< endl;
}
else if (str1 > str2)
{
cout << str1 << ">" << str2 << endl;
}
else if (str1 <str2)
{
cout << str1 << "<" << str2 << endl;
}
return 0;
}
//3.4.2
using namespace std;
int main()
{
string str1,str2;
cout << "请输入两个字符串:" << endl;
cin >> str1 >> str2;
auto len1 = str1.size();
auto len2 = str2.size();
if(len1==len2)
{
cout <<"epual"<< endl;
}
else if (len1 > len2)
{
cout << str1 << ">" << str2 << endl;
}
else if (len1 <len2)
{
cout << str1 << "<" << str2 << endl;
}
return 0;
}
//3.5.1
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s,ss;
char ch = 'y';
cout << "请输入第一个字符串:" << endl;
while (cin >> s)
{
ss += s;
cout << "是否继续输入字符串y or n:" << endl;
cin >> ch;
if (ch == 'y' || ch == 'Y')//用单引号。双引号错误,双引号是字符串指针
cout << "请输入下一个字符串:"<<endl;
else
break;
}
cout << ss << endl;
return 0;
}
//3.5.2
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s,ss;
char ch = 'y';
cout << "请输入第一个字符串:" << endl;
while (cin >> s)
{
if (!ss.size())
ss += s;
else
ss = ss + " " + s;
cout << "是否继续输入字符串y or n:" << endl;
cin >> ch;
if (ch == 'y' || ch == 'Y')
cout << "请输入下一个字符串:"<<endl;
else
break;
}
cout << ss << endl;
return 0;
}
//3.6
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cout << "请输入一个字符串,可以包含空白:" << endl;
getline(cin, s);
for(auto &c : s)
{
c = 'X';
}
cout << s << endl;
return 0;
}
//3.8.1
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cout << "请输入一个字符串可以包含空格:" << endl;
getline(cin, s);
int i = 0;
while (s[i] != '\0')
{
s[i] = 'X';
i++;
}
cout << s << endl;
return 0;
}
//3.8.2
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cout << "请输入一个字符串可以包含空格:" << endl;
getline(cin, s);
unsigned int i = 0;
for (i = 0;i<s.size();i++)
{
s[i] = 'X';
}
cout << s << endl;
return 0;
}
//3.10.1
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
string s;
cout << "请输入一个字符串可以包含空格:" << endl;
getline(cin, s);
for (auto c : s)
{
if(!ispunct(c))
cout << c;
}
return 0;
}
//3.10.2
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
string s;
cout << "请输入一个字符串可以包含空格:" << endl;
getline(cin, s);
for (decltype(s.size()) i = 0;i<s.size();i++)
{
if(!ispunct(s[i]))
cout << s[i];
}
return 0;
}
//3.14
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int i;
char cont = 'y';
vector<int> ii;
cout << "请输入一组整数:" << endl;
while (cin >> i)
{
ii.push_back(i);
cout << "continue? y or n?" << endl;
cin >> cont;
if (cont != 'y' && cont != 'Y')
break;
}
for (auto elem : ii)
{
cout << elem << " ";
}
return 0;
}
//3.15
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string s;
char cont = 'y';
vector<string> ss;
cout << "请输入一组字符串:" << endl;
while (cin >> s)
{
ss.push_back(s);
cout << "continue? y or n?" << endl;
cin >> cont;
if (cont != 'y' && cont != 'Y')
break;
}
for (auto elem : ss)
{
cout << elem << " ";
}
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 << "v1元素的个数是:" << v1.size() << endl;
if (v1.size() > 0)
{
cout << "v1的元素分别是:" << endl;
for (auto elem : v1)
{
cout << elem << " ";
}
cout << endl;
}
cout << "v2元素的个数是:" << v2.size() << endl;
if (v2.size() > 0)
{
cout << "v2的元素分别是:" << endl;
for (auto elem : v2)
{
cout << elem << " ";
}
cout << endl;
}
cout << "v3元素的个数是:" << v3.size() << endl;
if (v3.size() > 0)
{
cout << "v3的元素分别是:" << endl;
for (auto elem : v3)
{
cout << elem << " ";
}
cout << endl;
}
cout << "v4元素的个数是:" << v4.size() << endl;
if (v4.size() > 0)
{
cout << "v4的元素分别是:" << endl;
for (auto elem : v4)
{
cout << elem << " ";
}
cout << endl;
}
cout << "v5元素的个数是:" << v5.size() << endl;
if (v5.size() > 0)
{
cout << "v5的元素分别是:" << endl;
for (auto elem : v5)
{
cout << elem << " ";
}
cout << endl;
}
cout << "v6元素的个数是:" << v6.size() << endl;
if (v6.size() > 0)
{
cout << "v6的元素分别是:" << endl;
for (auto elem : v6)
{
cout << elem << " ";
}
cout << endl;
}
cout << "v7元素的个数是:" << v7.size() << endl;
if (v7.size() > 0)
{
cout << "v1的元素分别是:" << endl;
for (auto elem : v7)
{
cout << elem << " ";
}
cout << endl;
}
return 0;
}
//3.17
#include<iostream>
#include<string>
#include<vector>
#include<cctype>
using namespace std;
int main()
{
char cont = 'y';
string word;
vector<string> ss;
cout << "请输入一组词:" << endl;
while (cin >> word)
{
ss.push_back(word);
cout << "continue? y or n" << endl;
cin >> cont;
if (cont != 'y'&&cont != 'Y')
break;
}
for (auto &r : ss)
{
for (auto &c : r)
{
c = toupper(c);
}
cout << r << endl;
}
return 0;
}
//3.20.1
#include<iostream>
#include<vector>
using namespace std;
int main()
{
char cont = 'y';
unsigned int num = 0;
int i;
vector<int> vi;
cout << "请输入一组整数:" << endl;
while (cin >> i)
{
vi.push_back(i);
cout << "continue? y or n" << endl;
cin >> cont;
if (cont != 'y'&&cont != 'Y')
break;
}
for (decltype(vi.size()) j = 0;j<vi.size()-1;j += 2)
{
cout << vi[j] + vi[j + 1] << " ";
if ((j + 2) % 10 == 0)
cout << endl;
}
if (vi.size() % 2 != 0)
cout << vi[vi.size() - 1];
return 0;
}
//3.20.2
#include<iostream>
#include<vector>
using namespace std;
int main()
{
char cont = 'y';
unsigned int num = 0;
int i;
vector<int> vi;
cout << "请输入一组整数:" << endl;
while (cin >> i)
{
vi.push_back(i);
cout << "continue? y or n" << endl;
cin >> cont;
if (cont != 'y'&&cont != 'Y')
break;
}
if (vi.size() == 0)
{
cout << "无元素"<<endl;
return -1;
}
for (decltype(vi.size()) j = 0;j<vi.size()/2;j++)
{
cout << vi[j] + vi[vi.size()-j-1] << " ";
if ((j + 1) % 5 == 0)
cout << endl;
}
if (vi.size() % 2 != 0)
cout << vi[vi.size()/2];
return 0;
}
//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 << "v1元素的个数是:" << v1.size() << endl;
if (v1.cbegin()!=v1.cend())
{
cout << "v1的元素分别是:" << endl;
for (auto it = v1.cbegin();it != v1.cend();++it)
{
cout << *it << " ";
}
cout << endl;
}
cout << "v2元素的个数是:" << v2.size() << endl;
if (v2.cbegin() != v2.cend())
{
cout << "v2的元素分别是:" << endl;
for (auto it = v2.cbegin();it != v2.cend();++it)
{
cout << *it << " ";
}
cout << endl;
}
cout << "v3元素的个数是:" << v3.size() << endl;
if (v3.cbegin() != v3.cend())
{
cout << "v3的元素分别是:" << endl;
for (auto it = v3.cbegin();it != v3.cend();++it)
{
cout << *it << " ";
}
cout << endl;
}
cout << "v4元素的个数是:" << v4.size() << endl;
if (v4.cbegin() != v4.cend())
{
cout << "v4的元素分别是:" << endl;
for (auto it = v4.cbegin();it != v4.cend();++it)
{
cout << *it << " ";
}
cout << endl;
}
cout << "v5元素的个数是:" << v5.size() << endl;
if (v5.cbegin() != v5.cend())
{
cout << "v5的元素分别是:" << endl;
for (auto it = v5.cbegin();it != v5.cend();++it)
{
cout << *it << " ";
}
cout << endl;
}
cout << "v6元素的个数是:" << v6.size() << endl;
if (v6.cbegin() != v6.cend())
{
cout << "v6的元素分别是:" << endl;
for (auto it = v6.cbegin();it != v6.cend();++it)
{
cout << *it << " ";
}
cout << endl;
}
cout << "v7元素的个数是:" << v7.size() << endl;
if (v7.cbegin() != v7.cend())
{
cout << "v7的元素分别是:" << endl;
for (auto it = v7.cbegin();it != v7.cend();++it)
{
cout << *it << " ";
}
cout << endl;
}
return 0;
}
//3.22
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<string> text;
string s;
cout << "input a sentence:" << endl;
while (getline(cin,s))
{
text.push_back(s);
break;
}
for (auto it = text.begin();it != text.end() && !it->empty();it++)
{
for (auto it1 = it->begin();it1 != it->end() ;it1++)
{
*it1 = toupper(*it1);
}
cout << *it << endl;
}
return 0;
}
//3.23
#include<iostream>
#include<ctime>
#include<vector>
#include<cstdlib>
using namespace std;
int main()
{
vector<int> vi;
srand((unsigned)time(NULL));
for (int i = 0;i < 10;i++)
{
vi.push_back(rand() % 1000);
}
cout << "10个随机数:" <<endl;
for (auto it = vi.cbegin();it != vi.cend();it++)
{
cout << *it << " ";
}
cout << endl;
for (auto it = vi.begin();it != vi.end();it++)
{
*it *= 2;
cout << *it << " ";
}
cout << endl;
return 0;
}
//3.24.1
#include<iostream>
#include<vector>
using namespace std;
int main()
{
char cont = 'y';
unsigned int num = 0;
int i;
vector<int> vi;
cout << "请输入一组整数:" << endl;
while (cin >> i)
{
vi.push_back(i);
cout << "continue? y or n" << endl;
cin >> cont;
if (cont != 'y'&&cont != 'Y')
break;
}
if (vi.cbegin() == vi.cend())
{
cout << "no elem" << endl;
return -1;
}
cout << "相邻两相和依次是:" << endl;
for (auto it = vi.cbegin();it!=vi.cend()-1;it++)
{
cout << (*it+*(++it)) << " ";
if ((it-vi.cbegin ()+1)%10 == 0)
cout << endl;
}
if (vi.size() % 2 != 0)
cout << *(vi.cend ()-1);
return 0;
}
//3.24.2
#include<iostream>
#include<vector>
using namespace std;
int main()
{
char cont = 'y';
unsigned int num = 0;
int i;
vector<int> vi;
cout << "请输入一组整数:" << endl;
while (cin >> i)
{
vi.push_back(i);
cout << "continue? y or n" << endl;
cin >> cont;
if (cont != 'y'&&cont != 'Y')
break;
}
if (vi.cbegin() == vi.cend())
{
cout << "no elem" << endl;
return -1;
}
cout << "相邻两相和依次是:" << endl;
auto beg = vi.begin();
auto end = vi.end();
for (auto it = beg;it!=(end-beg)/2 + beg;it++)
{
cout << (*it + *(beg + (end - it)-1)) << " ";
if ((it-beg+1)%5 == 0)
cout << endl;
}
if (vi.size() % 2 != 0)
cout << *((end - beg) / 2 + beg);
return 0;
}
//3.2.25
#include<iostream>
#include<vector>
using namespace std;
int main()
{
char cont = 'y';
unsigned int num = 0;
int i;
vector<unsigned> vi(11);
auto it = vi.begin();
cout << "请输入0~100的数:" << endl;
while (cin >> i)
{
if (i <= 100)
{
++*(it + i / 10);
}
cout << "continue?y or n?" << endl;
cin >> cont;
if (cont != 'y'&&cont != 'Y')
break;
}
for (it = vi.begin();it != vi.end();it++)
{
cout << *it << " ";
}
return 0;
}
//3.31
#include<iostream>
#include<cstddef>
using namespace std;
int main()
{
constexpr size_t num = 10;
int arr[num];
for (int i = 0;i < num;i++)
{
arr[i] = i;
}
for (auto j : arr)
{
cout << j << " ";
}
return 0;
}
//3.32.1
#include<iostream>
#include<cstddef>
using namespace std;
int main()
{
constexpr size_t num = 10;
int arr[num];
int arr1[num];
for (int i = 0;i < num;i++)
{
arr[i] = i;
arr1[i] = arr[i];
}
for (auto j : arr1)
{
cout << j << " ";
}
return 0;
}
//3.32.2
#include<iostream>
#include<vector>
using namespace std;
int main()
{
const int num = 10;
vector<int> arr;
vector<int> arr1;
for (int i = 0;i < num;i++)
{
arr.push_back(i);
}
for (int j = 0;j < num;j++)
{
arr1.push_back(arr[j]);
}
for (auto j : arr1)
{
cout << j << " ";
}
return 0;
}
//3.35
#include<iostream>
using namespace std;
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int *p = arr;
for (int i = 0;i < 10;i++)
{
*p = 0;
++p;
}
for (auto e : arr)
{
cout << e << " ";
}
return 0;
}
//3.36
#include<iostream>
#include<cstddef>
using namespace std;
int main()
{
const int size = 5;
int arr[size], arr1[size];
int i,j;
int *beg = begin(arr);
int *ed = end(arr);
int *beg1 = begin(arr1);
int *ed1 = end(arr1);
cout << "input arr:" << endl;
//输入数组1
while (cin >> i)
{
if (beg != ed)
{
*beg = i;
beg++;
}
if(beg == ed)
{
beg = begin(arr);
break;
}
}
cout << "input arr1" << endl;
//输入数组2
while (cin >> j)
{
if (beg1 != ed1)
{
*beg1 = j;
beg1++;
}
if(beg1 == ed1)
{
beg1 = begin(arr1);//输入完
break;
}
}
while (beg != ed)
{
if (*beg == *beg1)
{
beg++;
beg1++;
}
else
{
cout << "不相等" << endl;
break;
}
if (beg == ed)
{
cout << "arr==arr1" << endl;
}
}
return 0;
}
//3.36.2
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v1, v2;
int i;
size_t num = 0;
cout << "input v1" << endl;
//输入vector1
while (cin >> i)
{
v1.push_back(i);
num++;
if (num == 5)
{
num = 0;
break;
}
}
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
cout << "input v2" << endl;
//输入vector2
while (cin >> i)
{
v2.push_back(i);
num++;
if (num == 5)
{
num = 0;
break;
}
}
for (auto e : v2)
{
cout << e << " ";
}
cout << endl;
auto beg1 = v1.cbegin(), ed1 = v1.cend();
auto beg2 = v2.cbegin(), ed2 = v2.cend();
while (beg1 != ed1)
{
if (*beg1 != *beg2)
{
cout << "不相等" << endl;
}
beg1++;
beg2++;
}
cout << "arr==arr1" << endl;
return 0;
}
//3.39.1
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1, s2;
cout << "input s1:" << endl;
cin >> s1;
cout << "input s2:" << endl;
cin >> s2;
if (s1 == s2)
{
cout << "s1=s2" << endl;
}
else
cout << "s1!=s2" << endl;
}
//3.39.2
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char s1[20], s2[20];
cout << "input s1:" << endl;
cin >> s1;
cout << "input s2:" << endl;
cin >> s2;
if(strcmp(s1,s2))
{
cout << "s1!=s2" << endl;
}
else
cout << "s1=s2" << endl;
}
//3.40
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char s1[] = "hello", s2[] = " world!",s3[50];
strcpy(s3, strcat(s1, s2));
cout << s3 << endl;
return 0;
}
//3.41
#include<iostream>
#include<vector>
using namespace std;
int main()
{
char s1[] = { 0,1,2,3,4 };
vector<int> vi(begin(s1),end(s1));
for (auto e : vi)
{
cout << e << " ";
}
return 0;
}
//3.42
#include<iostream>
#include<vector>
#include<ctime>
#include<cstdlib>
using namespace std;
int main()
{
vector<int> vi;
const int siz = 10;
srand((unsigned)time(NULL));
for (int i = 0;i != siz;i++)
{
vi.push_back(rand() % 100);
cout << vi[i] << " ";
}
cout << endl;
auto it = vi.begin();
int arr[siz];
for (auto &e : arr)
{
e = *it;
it++;
cout << e << " ";
}
return 0;
}
//3.43
#include<iostream>
using namespace std;
int main()
{
int ia[3][4] = { 1,2,3,4,1,2,3,4,1,2,3,4 };
cout << "使用范围for管理循环"<<endl;
for (int(&row)[4] : ia)
{
for (int col : row)
{
cout << col << " ";
}
cout << endl;
}
cout << "使用下表运算符" << endl;
for (int i = 0;i != 3;i++)
{
for (int j = 0;j != 4;j++)
{
cout << ia[i][j] << " ";
}
cout << endl;
}
cout << "使用指针" << endl;
for (int (*p)[4] = ia;p != ia + 3;p++)
{
for (int *q = *p;q!= *p+4;q++)
{
cout << *q << " ";
}
cout << endl;
}
return 0;
}
//3.44
#include<iostream>
using namespace std;
using int_array = int[4];
int main()
{
int ia[3][4] = { 1,2,3,4,1,2,3,4,1,2,3,4 };
cout << "使用范围for管理循环"<<endl;
for (int_array &row : ia)
{
for (int col : row)
{
cout << col << " ";
}
cout << endl;
}
cout << "使用下表运算符" << endl;
for (int i = 0;i != 3;i++)
{
for (int j = 0;j != 4;j++)
{
cout << ia[i][j] << " ";
}
cout << endl;
}
cout << "使用指针" << endl;
for (int_array *p = ia;p != ia + 3;p++)
{
for (int *q = *p;q!= *p+4;q++)
{
cout << *q << " ";
}
cout << endl;
}
return 0;
}
//3.45
#include<iostream>
using namespace std;
int main()
{
int ia[3][4] = { 1,2,3,4,1,2,3,4,1,2,3,4 };
cout << "使用范围for管理循环"<<endl;
for (auto &row : ia)
{
for (auto col : row)
{
cout << col << " ";
}
cout << endl;
}
cout << "使用下表运算符" << endl;
for (auto i = 0;i != 3;i++)
{
for (auto j = 0;j != 4;j++)
{
cout << ia[i][j] << " ";
}
cout << endl;
}
cout << "使用指针" << endl;
for (auto *p = ia;p != ia + 3;p++)
{
for (auto *q = *p;q!= *p+4;q++)
{
cout << *q << " ";
}
cout << endl;
}
return 0;
}