目录
2.2string类对象的访问及遍历操作(Iterators and Element access)
2.4string类对象的修改及相关操作(Modifiers and String operations)
1.auto和范围for
1.1auto关键字
(1)C++11中,标准委员会变废为宝赋予了auto全新的含义即:auto不再是一个存储类型指示符,而是作为一个新的类型指示符来指示编译器,auto声明的变量必须由编译器在编译时期推导而得。
#include <iostram>
using namespace std;
int func1()
{
return 10;
}
int main()
{
int a = 10;
auto b = a; //自动推导变量类型
auto c = 'a'; //常量字符也能推导
auto d = func1(); //使用函数返回值进行类型推导
// 编译报错:rror C3531: “e”: 类型包含“auto”的符号必须具有初始值设定项
// auto e;
cout << typeid(b).name() << endl;
cout << typeid(c).name() << endl;
cout << typeid(d).name() << endl;
return 0;
}
(2)用auto声明指针类型时,用auto和auto*没有任何区别,但用auto声明引用类型时则必须加&。
#include <iostream>
using namespace std;
int main()
{
int x = 10;
auto y = &x;
auto* z = &x;
auto& m = x;
cout << typeid(x).name() << endl;
cout << typeid(y).name() << endl;
cout << typeid(z).name() << endl;
cout << typeid(m).name() << endl;
return 0;
}
(3)当在同一行声明多个变量时,这些变量必须是相同的类型,否则编译器将会报错,因为编译器实际只对第一个类型进行推导,然后用推导出来的类型定义其他变量。
int main()
{
auto aa = 1, bb = 2;
// 编译报错:error C3538: 在声明符列表中,“auto”必须始终推导为同一类型
// auto cc = 3, dd = 4.0;
return 0;
}
(4)auto不能作为函数的参数,可以做返回值,但是建议谨慎使用,最好不用(因为经常用auto作为返回值,会降低代码的可读性,如果该函数用auto作返回值,返回值的类型还需要去函数内部寻找)。
// 不能做参数
void func2(auto a)
{}
// 可以做返回值,但是建议谨慎使用
auto func3()
{
return 3;
}
(5)auto不能直接用来声明数组.
int main()
{
// 编译报错:error C3318: “auto []”: 数组不能具有其中包含“auto”的元素类型
// auto array[] = { 4,5,6 };
return 0;
}
auto使用的地方是当返回值的类型名称过于长的时候可以使用auto进行自动推导,提高编程的效率.
1.2范围for
(1)对于一个有范围的集合而言,由程序员来说明循环的范围是多余的,有时候还会容易犯错误。因此C++11中引入了基于范围的for循环。for循环后的括号由冒号“ :”分为两部分:第一部分是范围内用于迭代的变量,第二部分则表示被迭代的范围,自动迭代,自动取数据,自动判断结束。
(2)范围for可以作用到数组和容器对象上进行遍.
(3)范围for的底层很简单,容器遍历实际就是替换为迭代器,这个从汇编层也可以看到。
#include <iostream>
#include <string>
using namespace std;
int main()
{
int array[] = { 1,2,3,4,5 };
//c++98的遍历
for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i)
{
cout << array[i] << " ";
}
cout << endl;
//c++11的遍历
for (auto& e : array) //如果待遍历的对象大小很大,可以加引用进行遍历,减少拷贝,提高效率
cout << e << " ";
cout << endl;
for (auto e : array) //用于数组
cout << e << " ";
cout << endl;
string str("hello world");
for (auto ch : str) //用于字符串
{
cout << ch << " ";
}
cout << endl;
return 0;
}
2.string类常用接口说明
string类的接口我按照C++函数网址进行介绍,这里只进行常用接口的介绍,其他接口、类中的函数参数和函数重载请参考该网址,下列介绍就不一一列出了.
2.1默认成员函数
2.1.1构造函数(constructor)
#include <iostream>
#include <string>
using namespace std;
void string_test1()
{
//1.default
string s1; //默认构造函数为空字符串
cout << s1 << endl;
//2.from c-string
string s2 = "hello world";
cout << s2 << endl;
//3.substring
string s3(s2, 6, 4);
cout << s3 << endl;
string s4(s2, 6); //不传第三个参数默认到最后
cout << s4 << endl;
//4.from buffer
string s5("abcd", 3);
cout << s5 << endl;
//5.fill
string s6(6, 'X');
cout << s6 << endl;
//6.copy
string s7 = s2;
cout << s7 << endl;
string s8(s2);
cout << s8 << endl;
//7.range 使用迭代器区间进行构造
string s9 = "hello world";
string s10(++s9.begin(), --s9.end());
cout << s10 << endl;
//8.initializer list"
string s11 = { "hello world" };
cout << s11 << endl;
}
int main()
{
string_test1();
return 0;
}
2.1.2赋值运算符重载(operator=())
#include <iostream>
#include <string>
using namespace std;
void string_test2()
{
string s1 = "hello world";
string s2;
string s3;
//1.string
s2.operator=(s1);
cout << s2 << endl;
s3 = s1;
cout << s3 << endl;
//2.c-string
string s4;
s4 = "XiaoC";
cout << s4 << endl;
//3.character
string s5;
s5 = 'C';
cout << s5 << endl;
//4.initializer list
string s6;
s6 = { "hello XiaoC" };
//s6.operator=({ "hello XiaoC" }); //另一种写法
cout << s6 << endl;
}
int main()
{
string_string2();
return 0;
}
2.2string类对象的访问及遍历操作(Iterators and Element access)
#include <iostream>
#include <string>
using namespace std;
void string_test4()
{
string s1 = "hello XiaoC";
const string s2 = s1;
//1.operator[]直接用下标访问字符串中的元素,且修改字符串种的元素
cout << s1[0] << " " << s2[2] << endl;
s1[0] = 'C';
cout << s1 << endl;
//s2[0] = 'C'; //const修饰的string对象不能进行修改
//2. 3种遍历string对象的方式
//1.for + operator[]
for (size_t i = 0; i < s1.size(); ++i)
cout << s1[i] << " ";
cout << endl;
//2.迭代器
string::iterator it = s1.begin();
while (it != s1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
//3.范围for
for (auto ch : s1)
cout << ch << " ";
cout << endl;
//使用反向迭代器反向遍历
string::reverse_iterator rit = s1.rbegin();
while (rit != s1.rend())
{
cout << *rit << " ";
rit++;
}
cout << endl;
//C++11之后,直接使用auto定义迭代器,让编译器推导迭代器的类型
auto rit2 = s1.rbegin();
while (rit2 != s1.rend())
{
cout << *rit2 << " ";
rit2++;
}
cout << endl;
}
int main()
{
string_test4();
return 0;
}
cbegin()和cend是专门为了const对象设置的接口,但是普通迭代器begin()和end()也重载了一个const对象的版本,所以const对象也能调用普通迭代器。
2.3string类对象的容量操作(Capacity)
#include <iostream>
#include <string>
using namespace std;
void string_test3()
{
//1.size-返回字符串有效字符长度
//2.length-返回字符串有效字符长度
string s1;
s1 = "hello world";
cout << s1.size() << endl;
cout << s1.length() << endl;
cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
//3.capacity,返回空间总大小
cout << s1.capacity() << endl; //返回的是15,实际是16个空间,从0-15
cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
//4.empty,检测字符串是否为空串,是返回true,否则返回false
cout << s1.empty() << endl;
string s2;
cout << s2.empty() << endl;
cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
//5.clear,清空有效字符
s2.clear();
cout << s2 << endl;
cout << s2.size() << endl;
cout << s2.capacity() << endl;
cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
//6.reserve,为字符串预留空间
//测试reserve是否会改变string中有效元素个数
string s4;
s4.reserve(100); //至少预留100个空间
cout << s4.size() << endl;
cout << s4.capacity() << endl;
//测试reserve参数小于string的底层空间大小时,是否会将空间缩小
s4.reserve(50);
cout << s4.size() << endl;
cout << s4.capacity() << endl; //vs中不会缩小容量
cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
//7.resize,将有效字符的个数改成n个,多出的空间用字符c填充
string s3 = "hello XiaoC";
cout << s3 << endl;
cout << s3.size() << endl;
cout << s3.capacity() << endl;
s3.resize(20, '5'); //大于原来字符串时,用‘5’填充多余的空间
cout << s3 << endl;
cout << s3.size() << endl;
cout << s3.capacity() << endl; //不够时扩容
s3.resize(5); //小于原来的字符串时,则截取n个字符
cout << s3 << endl;
cout << s3.size() << endl;
cout << s3.capacity() << endl; //缩小时不缩容
s3.resize(10); //大于原来的字符串,但不给用于填充的字符时,填充'\0'
cout << s3 << endl;
cout << s3.size() << endl;
cout << s3.capacity() << endl;
}
int main()
{
string_test3();
return 0;
}
2.3.1利用reserve提高插入数据的效率
如果提前知道string中大概要放多少个元素时,可以提前将string中空间设置好,减少扩容次数,提高效率。
1.没有添加reserve()的情况:
void TestPushBack()
{
string s;
size_t sz = s.capacity();
//s.reserve(100); //如果提前知道string中大概要放多少个元素时,可以提前将string中空间设置好
cout << "capacity changed: " << sz << '\n';
cout << "making s grow:\n";
for (int i = 0; i < 100; ++i)
{
s.push_back('c');
if (sz != s.capacity())
{
sz = s.capacity();
cout << "capacity changed: " << sz << '\n';
}
}
}
2.添加reserve()的情况:
void TestPushBack()
{
string s;
size_t sz = s.capacity();
s.reserve(100); //如果提前知道string中大概要放多少个元素时,可以提前将string中空间设置好
cout << "capacity changed: " << sz << '\n';
cout << "making s grow:\n";
for (int i = 0; i < 100; ++i)
{
s.push_back('c');
if (sz != s.capacity())
{
sz = s.capacity();
cout << "capacity changed: " << sz << '\n';
}
}
}
2.4string类对象的修改及相关操作(Modifiers and String operations)
#include <iostream>
#include <string>
using namespace std;
void string_test5()
{
//1.push_back - 在字符串后尾插字符c
string s1;
s1.push_back('c');
cout << s1 << endl;
cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
//2.append - 在字符串后追加一个字符串
s1.append("hello");
cout << s1 << endl;
cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
//3.operator+= - 在字符串后追加字符串或者一个字符
s1 += " XiaoC";
cout << s1 << endl;
s1 += 'C';
cout << s1 << endl;
cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
//4.c_str - 返回C格式字符串
cout << s1.c_str() << endl;
cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
//5.find - 从字符串pos位置开始往后找字符c,返回该字符第一次出现在字符串中的位置
//npos是string类中的成员常量
//static const size_t npos = -1
//表示计算机能表达的最大整数
string s2 = "hello world";
size_t pos1 = s2.find("world");
string s3(s2, pos1);
cout << s3 << endl;
cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
//取出url中的域名
string url("http://www.cplusplus.com/reference/string/string/find/");
cout << url << endl;
size_t start = url.find("://");
if (start == string::npos)
{
cout << "invalid url" << endl;
return;
}
start += 3;
size_t finish = url.find('/', start);
string address = url.substr(start, finish - start);
cout << address << endl;
cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
//6.rfind - 从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置
//获取file的后缀
string file("string.cpp");
size_t pos2 = file.rfind('.');
string suffix(file.substr(pos2, file.size() - pos2));
cout << suffix << endl;
cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
//7.substr - 在str中从pos位置开始,截取n个字符,然后将其返回
string str = "hello XiaoC";
size_t pos3 = str.find('X');
cout << str.substr(pos3, str.size() - pos3) << endl;
cout << "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
//删除url的协议前缀
size_t pos4 = url.find("://");
url.erase(0, pos4 + 3);
cout << url << endl;
}
int main()
{
string_test5();
return 0;
}
2.4.1成员常量npos
npos是string类中的成员常量,npos == -1,但是npos是size_t类型,是一个非负整数,-1的二进制编码为全1,对应的非负整数是计算机能表示的最大整数,所以npos在string类中表示计算机中的最大整数。
2.5string类非成员函数
#include <iostream>
#incldue <string>
using namespace std;
void string_test6()
{
//1.operator+ - 因为传值返回,导致深拷贝效率低,尽量少用
string s1 = "hello";
string s2 = "XiaoC";
string s3 = s1 + " " + s2;
cout << s3 << endl;
//2.operator>> - 输入运算符重载
cin >> s1;
//3.operator<< - 输出运算符重载
cout << s1 << endl;
//4.getline, 获取一行字符串
getchar(); //把缓冲区里面遗留的'\n'去掉
string name;
cout << "Please, enter your full name: ";
getline(cin, name);
cout << "Hello, " << name << "!" << endl;
//5.ralational operators
string s4 = "hello";
string s5 = "world";
cout << (s4 < s5) << endl;
cout << (s4 >= s5) << endl;
}
int main()
{
string_test6();
return 0;
}