C++ Primer 3.2.3~3.5.3部分节练习

来源:互联网 发布:海口关键词优化排名 编辑:程序博客网 时间:2024/06/06 00:52

备忘录

// ConsoleApplication1.cpp: 定义控制台应用程序的入口点。#include "stdafx.h"#include <iostream>#include <string>//使用字符串需要包含<string> #include <vector>using namespace std;using std::vector;struct Sales_date//一个结构体{std::string bookNo;unsigned unite_sold = 0;double revenue = 0.0;};int main(){/*string s,s2;cout << "输入一个字符串:" << endl;cin >> s>>s2;cout << s << "," << s2 << endl;system("pause");return 0;*//*//使用getline()读取一行//size()函数返回一个string的长度,注意:返回类型为string:size_type而非intstring line;cin >> line;cout << line << endl;system("pause");return 0;*///----------------------3.2.3处理string对象中的每个字符-----------------------//c++11提供了一种新的循环形式,相当于foreach(java)///*string str("hello world");for (auto c:str)//使用字符变量c来遍历str中的每个元素{cout << c << endl;}*///下面的例子查找一个字符串中的标点个数/*string s("hello, world!!");//decltype (s.size()) cnt = 0;string::size_type cnt = 0;//二者等价for (auto c : s){if (ispunct(c)){cnt++;}}cout << "共有"  << cnt << "个标点符号在句子" << s << "中" <<endl;*//*string s("hello world");for (auto &c : s){if (islower(c)){c = toupper(c);}}cout << s << endl;*///同样可以使用下表来访问string中的某个字符(元素),注意string[]索引符使用的是size_type类型,也即为什么可以使用//str[str.size(-1)]访问str的最后一个字符//使用下标执行迭代:str中的第一个单词改为大写/*string str("hello world");for (decltype (str.size()) i = 0; i != str.size() && !isspace(str[i]); i++){str[i] = toupper(str[i]);}cout << str << endl;*///----------------------3.2 p85 使用下标执行随机访问//----------------------/*const string hexdigits = "0123456789ABCDEF";//可能的十六进制数cout << "输入一个0到15的十进制数字,用空格分隔,按下回车以完成" << endl;string result;//结果string::size_type n;//保存从输入流读入的数while (cin >> n){if (n < hexdigits.size())//忽略无效输入{result += hexdigits[n];//得到对应的十六进制数}}cout << "十六进制数为:"<<result<<endl;cout << "n:" << (int)n << endl;*///----------------------3.3 标准库类型vector//----------------------//使用vector要包含头文件:#include <vector>,using std:vector//c++中的vector是一个类模版//模版本身不是类或函数,可以将模版看作编译器生成类或函数编写的一份说明////----------------------3.3.2节练习//----------------------/*vector<string> svec;//svec不含任何元素vector<int> intVec;int n = 0;int num;while (n < 5 && cin>>num){//cin>>num;intVec.push_back(num);n++;}cout << "elements of intVec are:" << endl;for (int i = 0; i < intVec.size(); i++){cout << intVec[i] << ",";}*//*vector<int> v{ 1,2,3,4,5,6,7,8,9 };for (auto &i : v){i *= i;}for (auto i : v){cout << i << ",";}*///----------------------3.3.3 节练习//----------------------//3.17:/*vector<string> text;int n = 0;while (n < 5){string temp;cin >> temp;text.push_back(temp);n++;}for (decltype(text.size()) i=0; i != text.size(); i++){for (auto &var : text[i]){if (!isupper(var)){var=toupper(var);}}}for (auto var : text){cout << var << endl;}*///----------------------3.4 迭代器//----------------------//以下例子展示了使用迭代器把String的第一个字符改为大写形式/*string s("some string");if (s.begin() != s.end()){auto it = s.begin();*it = toupper(*it);}cout << s << endl;*///以下例子通过循环配合迭代器将字符串中的所有字符变成大写/*string s("some string");//for (auto it = s.begin(); it != s.end(); it++)//{//if (isspace(*it))//{//continue;//}//*it = toupper(*it);//}cout << s << endl;*///----------------------3.4.2 节练习 练习3.24//----------------------/*unsigned grade;vector<unsigned> scores(11,0);//11个0int n = 0;while (cin >> grade && n<scores.size()){if (grade < 100){unsigned index=grade / 10;auto it = scores.begin()+index;*it+=1;}n++;}for (auto temp : scores){cout << temp << endl;}*///----------------------3.5.3 指针和数组//----------------------//使用指针遍历数组/*int arr[] = { 1, 2,3, 4, 5, 6, 7, 8, 9, 10 };//C++中的数组定义方式与C语言类似int *p = begin(arr);while (p != end(arr))//end和begin函数返回数组的尾后/头指针{cout << *p << endl;p++;}*///----------------------3.5.3 节练习//----------------------//下列代码利用指针将数组中的各元素置0/*int arr[]={ 1,2,3,4,5,6,7,8,9,10 };auto *p = arr;while (p != end(arr)){*p = 0;cout << *p << endl;p++;}*///以下是C语言标准库中的一些对Sting提供操作的函数,他们在C++中也同样适用,这些函数在C++中包含在cstring中//strlen(p)//长度,strcmp(p1,p2)比较,如果p1<p2,返回一个负值,strcat(p1,p2)p2加到p1后,strcpy(p1,p2)p2拷贝给p1,返回p1//----------------------3.5.5 节练习//----------------------vector<int> ivec{1, 2, 3, 4, 5};int arr[5];auto p = ivec.begin();int *t = arr;//首地址int i = 0;while (p < ivec.end()){*t++ = *p++;}for (int temp : arr){cout << temp << endl;}system("pause");}