概括
3.1. 使用using
声明命名空间;
3.2. 标准库类型string
的定义和初始化;string
对象的简单操作:输入、输出、比较和相加;头文件cctype
中的函数;使用range for和下标法对string
对象进行访问和处理;
3.3. 标准库类型vector
定义和初始化;使用push_back
成员函数为vector
对象添加元素;使用range for处理vector
对象;使用下标法处理某些类型的vector
对象;
3.4. 迭代器的简单操作:对string
对象和vector
对象进行处理;迭代器的类型;结合解引用和成员访问操作;迭代器的算术运算;
3.5. 内置数组的定义和初始化;数组和vector
对象在初始化与赋值上的的区别;复杂数组的声明的理解(参考『C++ Primer学习笔记』Chapter 2);使用下标法、range for、指针以及标准库函数begin
和end
对数组进行访问和处理;
使用cstring
头文件中的函数来处理C风格字符串(该字符串以空字符结束),C风格字符串、字符数组以及string
对象的区别和联系;
3.6. 多维数组的定义和初始化;使用下标法、range for、指针以及标准库函数begin
和end
来处理多维数组;
string对象、vector对象以及数组的处理目标
string对象的处理目标
string str = "value";
显示该字符串,并实现字母大小写之间的转换;
vector对象的处理目标
vector<int> ivec{1,2,3,4,5,6,7,8,9};
vector<string> svec{"how","are","you"};
显示该容器中的元素;对int
类型的元素实现倍数放大和缩小,对string
对象实现字母大小写之间的转换;
数组的处理目标
int arr[10] = {0,1,2,3,4,5,6,7,8,9};
int iarr[3][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11}};
显示数组的数值;对数组的元素实现倍数的方法和缩小;
在后续部分使用不同的方法进行处理时,均假设已经对这样对象进行了定义和初始化。
使用range for处理string对象、vector对象以及数组
string对象
for (auto &c:str)
c = toupper(c); //把string对象中小写字母修改为大写字母
for (auto c:str)
cout<<c; //输出string对象
cout<<endl;
cout<<str<<endl; //也可以直接输出
总结:无论输出还是修改,只需要使用一层range for就可以了,但是修改时必须为引用类型。
vector对象
for (auto &i:ivec)
i *= 2; //将vector对象中的元素值乘以2;
for (auto i:ivec)
cout<<i<<" "; //输出vector对象中的元素
cout<<endl;
for (auto &s:svec)
for (auto &t:s)
t = toupper(t); //将vector对象中的string对象的小写字母改为大写字母
for (auto s:svec)
cout<<s<<" "; //输出vector对象中的元素
cout<<endl;
总结:对于vector<int>
对象,无论输出还是修改,只需要使用一层range for就可以了,但是修改时必须为引用类型;对于vector<string>
对象,输出只需要使用一层range for,但是修改时则需要两层range for,且必须均为引用类型;
数组
for (auto &i:arr)
i *= 2; // 将数组中的元素乘以2;
for (auto i:arr)
cout<<i<<" "; //输出数组中的元素
cout<<endl;
for(auto &row:iarr)
for (auto &col:row)
col *=2; // 将数组中的元素乘以2;
for(auto &row:iarr)
{
for (auto col:row)
cout<<col<<"\t"; //输出数组中的元素
cout<<endl;
}
总结:对于一维数组,无论输出还是修改,只需要使用一层range for就可以了,但是修改时必须为引用类型;对于多维数组,无论输出还是修改,都需要使用多层range for,最内层在输出时可以不是引用类型,但修改时则必须是引用类型。
无论修改何种对象,必须使用引用类型!
使用下标法处理string对象、vector对象以及数组
string对象
for (decltype(str.size()) index = 0;index != str.size();++index)
str[index] = tolower(str[index]); //把string对象中大写字母改为小写字母
for (decltype(str.size()) index = 0;index != str.size();++index)
cout<<str[index]; //输出string对象
cout<<endl;
总结:无论输出还是修改都只需要使用一层的for循环即可。
vector对象
for (decltype(ivec.size()) index=0;index != ivec.size();++index)
ivec[index] /=2; //将vector对象中的元素值除以2;
for (decltype(ivec.size()) index=0;index != ivec.size();++index)
cout<<ivec[index]<<" "; //输出vector对象中的元素
cout<<endl;
for (decltype(svec.size()) index=0;index != svec.size();++index)
for(decltype(svec[index].size()) cnt = 0;cnt != svec[index].size();++cnt)
svec[index][cnt] = tolower(svec[index][cnt]); //将vector对象中的string对象的大写字母改为小写字母
for (decltype(svec.size()) index=0;index != svec.size();++index)
cout<<svec[index]<<" "; //输出vector对象中的元素
cout<<endl;
总结:vector<int>
对象输出和修改都只需要一层for循环;而vector<string>
对象输出只需要一层for循环,但修改需要两层的for循环:因为svec[index]
本身是string
对象,所以可以使用其成员函数size()
。
数组
for (size_t index = 0;index != sizeof(arr)/sizeof(int);++index)
arr[index] /=2; // 将数组中的元素除以2;
for (size_t index = 0;index != sizeof(arr)/sizeof(int);++index)
cout<<arr[index]<<" "; //输出数组中的元素
cout<<endl;
constexpr size_t rcnt = 3,ccnt = 4;
for (size_t row = 0;row != rcnt;++row)
for (size_t col = 0;col != ccnt;++col)
iarr[row][col] /=2; // 将数组中的元素除以2;
for (size_t row = 0;row != rcnt;++row)
{
for (size_t col = 0;col !=ccnt;++col)
cout<<iarr[row][col]<<"\t"; //输出数组中的元素
cout<<endl;
}
总结:一维数组只需要使用一层for循环,其中其数组的长度我们使用了sizeof
函数来得到;二维数组则需要两层for循环。
使用下标法,一定要检查其合法性!
使用迭代器处理string对象和vector对象
string对象
for (auto beg = str.begin();beg != str.end();++beg)
*beg = toupper(*beg); //把string对象中小写字母修改为大写字母
for (auto beg = str.begin();beg != str.end();++beg)
cout<<*beg; //输出string对象
cout<<endl;
vector对象
for (auto beg = ivec.begin();beg != ivec.end();++beg)
*beg *= 2; //将vector对象中的元素值乘以2;
for (auto beg = ivec.begin();beg != ivec.end();++beg)
cout<<*beg<<" "; //输出vector对象中的元素
cout<<endl;
for (auto beg = svec.begin();beg != svec.end();++beg)
for (auto sbeg = beg->begin();sbeg != beg->end();++sbeg)
*sbeg = toupper(*sbeg); //将vector对象中的string对象的小写字母改为大写字母
for (auto beg = svec.begin();beg != svec.end();++beg)
cout<<*beg<<" "; //输出vector对象中的元素
cout<<endl;
总结
指针也是迭代器,因此上述代码中对变量beg
等进行处理时都需要使用解引用符号*
,其中vector<string>
处理时,第二个for中结合了解引用和成员访问操作。
使用标准库函数begin和end处理数组
for (auto beg = begin(arr);beg != end(arr);++beg)
*beg *= 2; // 将数组中的元素乘以2;
for (auto beg = begin(arr);beg != end(arr);++beg)
cout<<*beg<<" "; //输出数组中的元素
cout<<endl;
for (auto rbeg = begin(iarr);rbeg != end(iarr);++rbeg)
for (auto cbeg = begin(*rbeg);cbeg != end(*rbeg);++cbeg)
*cbeg *= 2; // 将数组中的元素乘以2;
for (auto rbeg = begin(iarr);rbeg != end(iarr);++rbeg)
{
for (auto cbeg = begin(*rbeg);cbeg != end(*rbeg);++cbeg)
cout<<*cbeg<<"\t"; //输出数组中的元素
cout<<endl;
}
注意:在二维数组的内层for中,将rbeg
所指像的数组的第一个元素的指针赋给cbeg
,因此必须有解引用符号*
。
使用指针处理数组
constexpr ptrdiff_t sz = 10; // size_t或者ptrdiff_t类型都可以
for (int *ptr = arr,*beg = arr;ptr != beg + sz;++ptr)
*ptr /=2; // 将数组中的元素除以2;
for (int *ptr = arr,*beg = arr;ptr != beg + sz;++ptr)
cout<<*ptr<<" "; //输出数组中的元素
cout<<endl;
constexpr ptrdiff_t rowcnt = 3,colcnt = 4;
for (auto p = iarr,rbeg = iarr;p != (rbeg+rowcnt);++p)
for (auto q = *p,cbeg = *p;q != (cbeg+colcnt);++q)
*q /=2; // 将数组中的元素除以2;
for (auto p = iarr,rbeg = iarr;p != (rbeg+rowcnt);++p)
{
for (auto q = *p,cbeg = *p;q != (cbeg+colcnt);++q)
cout<<*q<<"\t"; //输出数组中的元素
cout<<endl;
}
注意:上述代码中的beg;rbeg,cbeg
均用来存储第一个元素的指针,从而用来判断是否循环结束,只使用ptr;p;q
则会造成死循环。
以处理对象为标准给出各自的代码
鉴于不知道如何上传附件,因此按照处理对象的不同,分别给出各个对象的不同处理方法,具体如下:
string对象
#include<iostream>
#include<string>//包括getline函数,page75
#include<vector>//使用vector,page86
#include<cctype>//is之类对单个字符处理的函数,page82
#include<cstddef>//定义数组下标size_t\ptrdiff_t的类型,page103\page107
#include<iterator>//数组的begin和end函数,page106
#include<cstring>//c语言中string.c的对应头文件,包含strlen等函数,page109
using std::cout;
using std::cin;
using std::endl;
using std::cerr;
using std::string;
using std::vector;
using std::begin;
using std::end;
/*
*
*
*
*/
int main()
{
// range for
string str = "value";
for (auto &c:str)
c = toupper(c); //把string对象中小写字母修改为大写字母
for (auto c:str)
cout<<c; //输出string对象
cout<<endl;
cout<<str<<endl; //也可以直接输出
// index
for (decltype(str.size()) index = 0;index != str.size();++index)
str[index] = tolower(str[index]); //把string对象中大写字母改为小写字母
for (decltype(str.size()) index = 0;index != str.size();++index)
cout<<str[index]; //输出string对象
cout<<endl;
// iterator
for (auto beg = str.begin();beg != str.end();++beg)
*beg = toupper(*beg);
for (auto beg = str.begin();beg != str.end();++beg)
cout<<*beg;
cout<<endl;
return 0;
}
vector对象
#include<iostream>
#include<string>//包括getline函数,page75
#include<vector>//使用vector,page86
#include<cctype>//is之类对单个字符处理的函数,page82
#include<cstddef>//定义数组下标size_t\ptrdiff_t的类型,page103\page107
#include<iterator>//数组的begin和end函数,page106
#include<cstring>//c语言中string.c的对应头文件,包含strlen等函数,page109
using std::cout;
using std::cin;
using std::endl;
using std::cerr;
using std::string;
using std::vector;
using std::begin;
using std::end;
/*
*
*
*
*/
int main()
{
// range for
vector<int> ivec{1,2,3,4,5,6,7,8,9};
for (auto &i:ivec)
i *= 2; //将vector对象中的元素值乘以2;
for (auto i:ivec)
cout<<i<<" "; //输出vector对象中的元素
cout<<endl;
vector<string> svec{"how","are","you"};
for (auto &s:svec)
for (auto &t:s)
t = toupper(t); //将vector对象中的string对象的小写字母改为大写字母
for (auto s:svec)
cout<<s<<" "; //输出vector对象中的元素
cout<<endl;
// index
for (decltype(ivec.size()) index=0;index != ivec.size();++index)
ivec[index] /=2; //将vector对象中的元素值除以2;
for (decltype(ivec.size()) index=0;index != ivec.size();++index)
cout<<ivec[index]<<" "; //输出vector对象中的元素
cout<<endl;
for (decltype(svec.size()) index=0;index != svec.size();++index)
for(decltype(svec[index].size()) cnt = 0;cnt != svec[index].size();++cnt)
svec[index][cnt] = tolower(svec[index][cnt]); //将vector对象中的string对象的大写字母改为小写字母
for (decltype(svec.size()) index=0;index != svec.size();++index)
cout<<svec[index]<<" "; //输出vector对象中的元素
cout<<endl;
// iterator
for (auto beg = ivec.begin();beg != ivec.end();++beg)
*beg *= 2; //将vector对象中的元素值乘以2;
for (auto beg = ivec.begin();beg != ivec.end();++beg)
cout<<*beg<<" "; //输出vector对象中的元素
cout<<endl;
for (auto beg = svec.begin();beg != svec.end();++beg)
for (auto sbeg = beg->begin();sbeg != beg->end();++sbeg)
*sbeg = toupper(*sbeg); //将vector对象中的string对象的小写字母改为大写字母
for (auto beg = svec.begin();beg != svec.end();++beg)
cout<<*beg<<" "; //输出vector对象中的元素
cout<<endl;
return 0;
}
数组
#include<iostream>
#include<string>//包括getline函数,page75
#include<vector>//使用vector,page86
#include<cctype>//is之类对单个字符处理的函数,page82
#include<cstddef>//定义数组下标size_t\ptrdiff_t的类型,page103\page107
#include<iterator>//数组的begin和end函数,page106
#include<cstring>//c语言中string.c的对应头文件,包含strlen等函数,page109
using std::cout;
using std::cin;
using std::endl;
using std::cerr;
using std::string;
using std::vector;
using std::begin;
using std::end;
/*
*
*
*
*/
int main()
{
// range for
int arr[10] = {0,1,2,3,4,5,6,7,8,9};
for (auto &i:arr)
i *= 2; // 将数组中的元素乘以2;
for (auto i:arr)
cout<<i<<" "; //输出数组中的元素
cout<<endl;
int iarr[3][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11}};
for(auto &row:iarr)
for (auto &col:row)
col *=2; // 将数组中的元素乘以2;
for(auto &row:iarr)
{
for (auto col:row)
cout<<col<<"\t"; //输出数组中的元素
cout<<endl;
}
// index
for (size_t index = 0;index != sizeof(arr)/sizeof(int);++index)
arr[index] /=2; // 将数组中的元素除以2;
for (size_t index = 0;index != sizeof(arr)/sizeof(int);++index)
cout<<arr[index]<<" "; //输出数组中的元素
cout<<endl;
constexpr size_t rcnt = 3,ccnt = 4;
for (size_t row = 0;row != rcnt;++row)
for (size_t col = 0;col != ccnt;++col)
iarr[row][col] /=2; // 将数组中的元素除以2;
for (size_t row = 0;row != rcnt;++row)
{
for (size_t col = 0;col !=ccnt;++col)
cout<<iarr[row][col]<<"\t"; //输出数组中的元素
cout<<endl;
}
// 标准库函数begin和end
for (auto beg = begin(arr);beg != end(arr);++beg)
*beg *= 2; // 将数组中的元素乘以2;
for (auto beg = begin(arr);beg != end(arr);++beg)
cout<<*beg<<" "; //输出数组中的元素
cout<<endl;
for (auto rbeg = begin(iarr);rbeg != end(iarr);++rbeg)
for (auto cbeg = begin(*rbeg);cbeg != end(*rbeg);++cbeg)
*cbeg *= 2; // 将数组中的元素乘以2;
for (auto rbeg = begin(iarr);rbeg != end(iarr);++rbeg)
{
for (auto cbeg = begin(*rbeg);cbeg != end(*rbeg);++cbeg)
cout<<*cbeg<<"\t"; //输出数组中的元素
cout<<endl;
}
// pointer
constexpr ptrdiff_t sz = 10; // size_t或者ptrdiff_t类型都可以
for (int *ptr = arr,*beg = arr;ptr != beg + sz;++ptr)
*ptr /=2; // 将数组中的元素除以2;
for (int *ptr = arr,*beg = arr;ptr != beg + sz;++ptr)
cout<<*ptr<<" "; //输出数组中的元素
cout<<endl;
constexpr ptrdiff_t rowcnt = 3,colcnt = 4;
for (auto p = iarr,rbeg = iarr;p != (rbeg+rowcnt);++p)
for (auto q = *p,cbeg = *p;q != (cbeg+colcnt);++q)
*q /=2; // 将数组中的元素除以2;
for (auto p = iarr,rbeg = iarr;p != (rbeg+rowcnt);++p)
{
for (auto q = *p,cbeg = *p;q != (cbeg+colcnt);++q)
cout<<*q<<"\t"; //输出数组中的元素
cout<<endl;
}
return 0;
}