<C++学习十一>标准库string的使用
摘要: 本篇博客仅作为笔记,如有侵权,请联系,立即删除(网上找博客学习,然后手记笔记,因纸质笔记不便保存,所以保存到网络笔记)
使用:
1、C++标准库负责管理和存储字符串所占用的内存;
2、头文件:#include<string>
3、空间域:using namespace std。
string的对象的初始化的方法:
string();//空串 string(size_type length,char ch);//以length为长度的ch拷贝 string(const char *str);//以str为初值 string(const char *str,size_type length);//同上 string(string &str,size_type index,size_type length); //以index为索引开始的子串,长度为length, 或者小于length string(input_iterator start,input_iterator end); //从以start到end得元素为初值 例: string str0; string str1(s1); string str2(5,'c');//将字符串初始化为字符'c'的n个副本 string str3("Now is the time..."); string str4(str2,11,4); string str = "helloworld";
string的相关简单函数以及操作符号:
s.empty(); //判断字符串是否为空,返回true或者false s.size(); //返回字符串中的字符的个数 s[n]; //返回字符串中的第n个字符,下标从0开始 s1+s2; //将s1和s2连城一个新的字符串,返回新生成的字符串 s1=s2; //将s1的内容替换成s2的内容 s1==s2; //比较s1和s2的内容,判断其内容是否一样
string类中的添加文本(append)的函数
append函数可以完成以下工作:
(1)在字符串末尾添加str
(2)在字符串末尾添加str的子串,子串以index索引开始,长度为len
(3)在字符串末尾添加str中的num个字符
(4)在字符串末尾添加num个字符ch
(5)在字符串末尾添加以迭代器start和end表示的字符序列
string str1("hello"); string str2("world"); str1.append("hello"); //str1后面追加一个hello str1.append(str2); //str1后面追加str2的字符 str1.append(10,"!"); //str1后面追加10个! str1.append(str2,2,2); //从str2的第2个位置,添加2位
string类的赋值函数(assign)
函数以下列方式赋值:
(1)用str为字符串赋值
(2)用str的开始num个字符为字符串赋值
(3)用str的子串为字符串赋值,子串以index索引开始,长度为len
(4)用num个字符ch为字符串赋值
string str1; string str2("hello world"); str1.assign(str2); //str1的值为hello world str1.assign(str2,5,5); //以5为索引,复制5位长度 str1.assign("world"); //为字符串赋值 str1.assign("hello world",5,5); //同上 str1.assign(15,"n"); //复制15个n到字符串
string类的at函数,返回下标对应的字符
string text = "ABCDEF"; char ch = text.at(2);
string类的begin函数,返回的是一个指向第一个元素的迭代器
string str("123456789"); iterater it = str.begin();
string类的c_str函数
string str("hello world"); char *char_str = str.c_str(); //将字符串转化成char*(字符指针)
string类capacity函数
capacity()函数返回在重新申请更多空间前字符串可以容纳的字符数(这个数字至少与size()一样大)。
string str("ni hao"); int num = str.capacity();
string类的比较函数compare
compare()函数以多种方式比较字符串和str,返回:
大于零 this < str
零 this == str
小于零 this > str
string类的拷贝函数copy()
copy函数拷贝自己的num个字符到str中(从索引index开始)。
string str("1234567"); char *str2 = "helloworld"; int num = str.copy(str2,5,0); //从0位开始拷贝5个字符到num中
string类的data函数
const char* data(); //函数返回指向自己第一个字符的指针
string类的end函数
iterator end(); //end函数返回一个迭代器,指向这个字符串的尾部 string str_1("i want to study c++"); iterator it = st_1.end();
string类的删除函数erase();
(1)删除pas指向的字符,返回指向下一个字符的迭代器
(2)删除从start到end得所有字符,返回一个迭代器,指向被删除的最后一个字符的下一个位置
(3)删除从index索引开始的num个字符,返回*this。
string类find函数
find()函数:
(1)返回str在字符串中第一次出现的位置(从index开始查找),如果没找到则返回string::nops。
(2)返回str在字符串中第一次出现的位置(从index开始查找),长度为length。如果没有找到就返回string::npos;
(3)返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没有找到就返回string::nops。
string str1("Alpha Beta Gama Delta"); unsigned int loc = str1.find("Omega",0); if(loc != sting::nops){ cout << loc << endl; }
string类的查找函数:find_first_of,查找字符串中第一个匹配字符,find_first_not_of,查找字符串中第一个不匹配字符。
find_last_of,查找字符串中最后一个匹配字符,find_last_not_of,查找字符串中最后一个不匹配字符。
string类的获取配置器的函数get_allocator()
allocator_type get_allocator();
string的插入(insert)函数
(1)在迭代器i表示的位置前插入一个字符ch
(2)在字符串的位置index插入字符串str
(3)在字符串的位置index插入字符串str的子串(以index开始,长num个字符)
(4)在字符串的位置index插入字符串str的num个字符
(5)在字符串的位置index插入num个字符ch的拷贝
(6)在迭代器i表示的位置前面插入num个字符ch的拷贝
(7)在迭代器i表示的位置前面插入一段字符,从start开始,以end结束。
string类的长度length()函数
size_type length();
length()函数返回字符串的长度,这个数字应该和size()返回的数字相同
string类的最大长度max_size
size_type max_size();
max_size()函数返回字符串能保存的最大字符数。
string类获取逆向迭代器的函数rbegin()和rend()函数
const reverse_iterator rbegin(); rbegin(); //返回一个逆向迭代器,指向字符串的最后一个字符。 const reverse_iterator rend(); rend(); //返回一个逆向迭代器,指向字符串的开头(第一个字符前一个位置)
string类的替换函数replace()
有许多用法,需要时查找。
string类的保留空间函数reverse()函数
void reserve(size_type num);
reserve()函数设置本字符串的capacity()以保留num个字符空间
string改变文本字符串的大小函数resize()
void resize(size_type num); void resize(size_type num,char ch);
resize()函数改变本字符串的大小到num,新空间的内容不确定,也可以用制定ch填充。
string逆向查找rfing()函数
和find()使用方法一样,只不过是最后一次匹配的。
string中返回字符串中当前有的字符数函数size()
size_type size();
size()函数返回字符串中现在拥有的字符数。
string的截取函数substr()
basic_string substr(size_type index,size_type num = nops);
substr()返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,将默认值string::nops,即返回从index开始的剩余字符。
string类的交换函数swap()
void swap(basic_string &str);
swap()函数把str和文本字符串交换。