C++ primer 第九章
9.5 额外的string操作
构造 string 的其它方法
const char* cp = "Hello World!"; //以空字符结束的数组
char noNull[] = { 'H', 'I' }; //不是以空字符结束
std::string s1(cp); //拷贝cp中的字符直至遇到空字符,等同于s1="Hello World!"
std::string s2(noNull,2); //从noNull拷贝两个字符: s2 = "HI"
std::string s3(noNull); //拷贝noNull中的字符直至遇到空字符
std::string s4(cp+6,5); //从cp[6]开始拷贝5个字符: s4 = "World"
std::string s5(s1, 6, 5); //从s1[6]开始拷贝5个字符直到s1末尾: s5 = "World"
std::string s6(s1, 6); //从s1[6]开始拷贝6个字符直到s1末尾: s5 = "World!"
std::string s6(s1, 6, 20); //正确,只拷贝到末尾
std::string s6(s1, 20); //数组角标越界
std::string s("Hello World");
std::string str1 = s.substr(0, 5); //Hello
std::string str2 = s.substr(6); //World
std::string str3 = s.substr(6, 11); //World
std::string str4 = s.substr(12); //数组角标越界
改变 string 的其它方法
std::string s("Hello World");
//除了接受迭代器版本的insert和erase,string 还提供了接受下标的版本
s.insert(s.size(), 5, '!'); //在s末尾插入5个感叹号
s.erase(s.size() - 5, 5); //删除s最后5个字符
//还提供接受C风格字符数组的inset和assign版本
const char* cp1 = "Stately plump Buck";
s.assign(cp1, 7); //s = Stately
std::cout << s << std::endl;
s.insert(s.size(), cp1 + 7); //s = Stately plump Buck
std::cout << s << std::endl;
//指定来自其它的字符串的字符插入到当前string
std::string st1 = "some string", st2 = "some other string";
st1.insert(0, st2); //在 st1 中位置0之前插入 st2
std::cout << st1 << std::endl;
st1.insert(0, st2, 0, st2.size()); //在st1[0]之前插入st2中st2[0]开始的st2.size()个字符
std::cout << st1 << std::endl;
//append操作是在string末尾进行插入操作的一种简写形式
std::string s1("C++ Primer"), s2 = s1;
s1.insert(s1.size(), "4th Ed"); //s1 = C++ Primer 4th Ed
s2.append("4th Ed"); //s2 = C++ Primer 4th Ed
//replace操作是调用erase和insert操作的一种简写
//将 4th 替换为 5th 的等价方法
s1.erase(11, 3); // s1 = C++ Primer Ed
s1.insert(11, "5th"); // s1 = C++ Primer 5th Ed
//从位置11开始,删除三个字符插入"5th"
s2.replace(11, 3, "5th");
string 的搜索操作
string提供了六个不同的搜索函数
每个函数都有四个不同的重载
//每个搜索操作都返回一个 string::size_type 类型
std::string name("AnnaBelle");
auto pos1 = name.find("Anna"); //pos1 = 0
std::string numbers("0123456789"), name1("r2d2");
//返回1,即name第一个数字的下标
auto pos2 = name1.find_first_of(numbers);
std::string dept("03714p3");
//返回5--字符'p'的下标
auto pos3 = dept.find_first_not_of(numbers);
std::string::size_type pos = 0; //开始查找的位置
//每步循环查找name中下一个数
while ((pos = name1.find_first_of(numbers,pos)) != std::string::npos){
std::cout << "found number at index:" << pos << " element is " << name[pos] << std::endl;
++pos; //移动至下一个字符
}
std::string river("Missisippi");
auto first_pos = river.find("is"); //返回1
auto last_pos = river.rfind("is"); //返回4,反向查找
compare 操作
数值转换
int i = 42;
std::string s1 = std::to_string(i); //将整数i转换为字符表示
double d = std::stod(s1); //将字符串s转换为浮点数
std::string s2 = "pi = 3.14";
//stod遇到不是数值的字符停止
d = std::stod(s2.substr(s2.find_first_of("+-.0123456789")));
9.6 容器适配器
所有适配器都要求容器具有添加,删除和访问尾元素的能力
//fill up stack
for (size_t ix = 0; ix != 10; ++ix){
intStack.push(ix);
}
while (!intStack.empty()){
int val = intStack.top();
std::cout << val << std::endl;
intStack.pop();
}
默认情况下,栈和队列(非优先级队列)是基于 deque(双向队列,首尾均为插入),priority_queue 是基于vector实现的。可创建设配器,通过第二个参数来指定容器类型
std::stack<int,std::vector<int>> intStack;