1、字符串库简介
c语言
- 字符串是字符的数组
用于单字节的字符串函数
- strcpy()
- sprintf()
- stoi()
- 等
1、STL中的字符串类模板— basic_string
- basic_string : 不透明对象 管理’\0’结尾的字符数组
- basic_string 的两个预定义类型:
— 包含char 的string类型
— 包含wchar的wstring类型
template<class Ch, class Tr=char_traits<Ch>,class A = allocator<Ch>> class std::basic_string{
...
public
...
}
1.class Ch: 单个字符所属类型;
2.class Tr= char_traits: 字符的字符特征类的类别,提供字符串类别中的所有字符核心操作。
3.class A=allocator: 带有默认值, 定义字符串类别采用的内存模式。通常设定为“默认内存模型allocator”;
typedef basic_string<char> string; //basic_string 容器
typedef basic_string<wchar> wsring;// 宽字符的basic_string 容器
所有字符类型的接口,用法都是一样。
还支持不同种类的字符串:
typedef basic_string<unsigned char> Ustring;
typedef basic_string<Jchar> Jstring;//日文字符串
2、字符串操作的通用函数
1.构造器和析构器
string str; //空
string s(str);//str的拷贝
string s(str, strbegin);//str从strbegin开始复制
string s(str,strbegin,strlen);str从strbegin开始复制strlen长度的字符串
string s(cstr); //接受c风格字符串
string s(cstr, len)//c风格字符串的前len个字符
string s(num,c); //生成一个字符串,包含num个c字符
析构函数:
~string()
例子:
string s('x');//字符 ,错误
string(1,'x');//正确
string ("x");//字符串
2.大小和容量
- size()
- length() // 这两个返回string对象中的字符个数
- max_size() //返回string类对象最多可以包含的字符个数
capacity() //重新分配内存之前,string对象能包含的最大字符数
初始化一个s: string s("123456789");
得到结果
size: 9
length: 9
max_size: 4294967294
capacity: 15
----------------重新分配内存: --------
//执行s.resize(20);
size: 20
length: 20
max_size: 4294967294
capacity: 31
3.元素存取(访问)
- 访问单一字符
— 下标操作符[]:不检查索引有效性,常量的字符串和常量string,最后一个字符为’\0’,最后一个字符下标有效,返回’\0’
— 成员函数at(): 超过会返回out_of_range 异常
两个返回索引对应的字符的引用(当字符串发生重分配如重新被赋值等操作,失效,原来的值已经变化)。
4.字符串比较
- 比较运算符
- compare()
——— compare()函数 ———
他的原型:
int compare(const basic_string & s)const;
int compare(const Ch *p)const;
int compare(size_type pos, size_type n , const basic_string &s)cosnt;
int compare(size_type pos, size_type n, const basic_string &s,size_type pos2, size_type n2)const;
int compare(size_type pos, size_type n , const Ch *p, size_type n2 =npos)const;
例子:
string s("123456");
string a("abcdef");
string b("ABCDEF");
string c("123efg");
cout << "---------------- --------" << endl;
int m = a.compare(b); // a和b完整比较
int n = a.compare(1,5,b); // "bcedf"和"ABCDEF"
int p = a.compare(1,5,b,4,2); // bcdef 和EF
int q = s.compare(0, 3, c, 0, 3); // 123 和123
cout << "m: " <<m<<" n: "<<n<<" p: "<<p<<" q : "<<q << endl;
结果为:
m: 1 n: 1 p: 1 q : 0
5. 字符串内容修改和替换
1.更改内容
- assign()
- operator=
- erase()
- swap()
- insert()
- append()
—–assign()函数
原型:
basic_string & assign(const E *s); //直接使用字符串赋值
basic_string & assign(const E* s,size_type n);//字符串的n个长度
basic_string & assign(const string & str,size_type pos,size_type n); // str的子串长为n赋值
basic_string & assign(cosnt string& str); //使用字符串引用赋值
basic_string & assign(size_type n , E c); //使用n个重复字符赋值
basic_string & assign(const_iterator first, cosnt_iterator last); //使用迭代器赋值
例子:
string s("123456");
string str;
cout << "---------------- --------" << endl;
str.assign(s);
cout << "直接赋值:" << str << endl;
str.assign(s, 1, 3);
cout << "子串赋值:" << str << endl;
str.assign(5, 'c');
cout << "重复字符赋值: " << str << endl;
str.assign(s.begin(), s.end());
cout << "迭代器赋值:" << str << endl;
---------
结果为:
直接赋值:123456
子串赋值:234
重复字符赋值: ccccc
迭代器赋值:123456
—–erase()
原型:
iterator erase(iterator first,iterator last);
iterator erase(iterator it);
iterator erase(size_type p0=0,size_type n = npos);
使用方法如下:
str.erase(str.begin(),str.end());
str.erase(2);
—–swap()
函数原型:
void swap(basic_string & str);
使用方法如下:
string str2("aaaa");
str.swap(str2);
—–insert()
函数原型:
basic_string & insert (size_type p0,const E *s); // 插入一个字符串到p0
basic_stirng & insert(size_type p0,const E*s,size_type n); //字符串的n个字符插入到 p0
basic_stirng & insert(size_type p0,const basci_string& str);//string对象str插入到p0
basic_stirng & insert(size_type p0, const basic_string& str, size_type pos, size_type n);//str的n个字符插入到p0
basic_stirng & insert(size_type p0, size_tye n ,E c); //插入n个字符到p0
basic_stirng & insert(iterator it ,E c);//插入字符到迭代器位置
basic_stirng & insert(iterator it ,cosnt_iterator first, const_iterator last);//迭代器范围插入到it指向的位置
basic_stirng & insert(iterator it ,size_type n , E c);
例子:
string s("123456");
char n[] = "abced";
cout << "---------------- --------" << endl;
s.insert(1,n);
cout << "直接插入一个字符串:" << s << endl;
s = "123456";
//s.insert(s, n, 3);
//cout << "直接插入一个字符串的前三个字符:" << s << endl;
s="123456";
s.insert(1, a);
cout << "直接插入一个string: " << s<< endl;
s = "123456";
s.insert(1,5,'A');
cout << "直接插入一个n个字符:" << s<< endl;
s = "123456";
s.insert(s.begin(), 'A');
cout << "迭代器位置插入一个字符:" << s << endl;
s = "123456";
s.insert(s.begin(), a.begin(),a.end());
cout << "迭代器位置插入一个范围:" << s << endl;
----
其中我的vs第二个报错。没解决
结果为;
直接插入一个字符串:1abced23456
直接插入一个string: 1abcdef23456
直接插入一个n个字符:1AAAAA23456
迭代器位置插入一个字符:A123456
迭代器位置插入一个范围:abcdef123456
—–append()
函数原型:
basic_string & apend(const E *s); //追加字符串
basic_string & apend(const E *s,size_typee n);
//追加字符串前n个字符
basic_string & apend(const basic_string & str, size_type pos, size_type n);// v追加子串
basic_string & apend(const basic_string &str);//追加str
basic_string & apend(size_type n ,E c);//追加n个重复字符
basic_string & apend(const_iterator first, const_iterator last);//追加迭代器范围内的字符串
2. 替换
- 使用下标修改
- replace()函数
—replace()函数
原型:
(1)basic_string& repalce(size_type p0, size_type n0,const E *s);//s替换p0开始的n0个字符
(2)basic_string& repalce(size_type p0,size_type n0, const E *s,size_type n);//s的开头n个字符替换p0开始的n0个字符
(3)basic_string& repalce(size_type p0,size_type n0,const basic_string & str);//str替换
(4)basic_string& repalce(size_type p0,size_type n0,const basic_stirng &str, size_type pos, size_type n);//str的从pos开始的n个字符替换p0开始的n0个字符
(5)basic_string& repalce(size_type p0,size_type n0, size_type n , E E c);
(6)basic_string& repalce(iterator first0, iterator last0 ,cosnt E *s);
(7)basic_string& repalce(iterator first0, iterator last0 ,cosnt E *s,size_type n);
(8)basic_string& repalce(iterator first0, iterator last0 ,bcosnt basic_string & str);
(9)basic_string& repalce(iterator first0, iterator last0 ,size_type n, E c);
(10)basic_string& repalce(iterator first0, iterator last0 ,cosnt_iterator first,const_iterator last);
6.字符串连接
- operator+
string str1("123");
string str2("456");
string str =str1 + str2;
7.字符串I/O操作
- operator <<
- operator>>
- getline()
—–getline()函数—–
函数原型:
template<class CharType, class Traits, class Allocator>
basic_istream<CharType,Traits>& getline(basic_istream<CharType,Traits>& _Istr, basic_string<CharType,Traits, Allocator>& _str);//第一个参数是输入流,第二个参数是保存输入内容的字符串;
template<class CharType, class Traits, class Allocator>
basic_istream<CharType,Traits>& getline(basic_istream<CharType,Traits>& _Istr, basic_string<CharType,Traits, Allocator>& _str, CharType _Delim);
//三个参数,第三个参数为指定分界符
例子:
string str;
string str2;
getline(cin, str);
getline(cin,str2,' ');
cout << "第一次输入读取内容:" << str << endl;
cout << "第二次输入读取内容:" << str2 << endl;
-------------
结果为:
123456
abcedf efgd
第一次输入读取内容:123456
第二次输入读取内容:abcedf
可以看出读取到空格分界符,之后的字符就没有读取到字符串str2中。
8.字符串的搜索和查找
- string:: npos :无符号整数值,初始值为-1,表示未找到
- find() : 返回都是size_type类型 无符号整形
—–find()函数和rfind()函数———
函数原型:
size_type find(value_type_Ch, size_type_off = 0)const;
//第一个参数是被搜索的字符,第二个参数是源字符串搜索的起始位置
size_type find(cosnt value_type* _ptr, siez_type_off =0)const;
//第一个参数是被搜索的字符串
size_type find(const value_type* _ptr,size_type_off = 0,
size_type_Count)const;
//第三个参数是第一个参数的字符个数,可能是_ptr的个数,可以是_ptr子串的字符个数
size_type find(cosnt basic_string& _str, size_type_off=0)const;
//类似
—–find_first_of()函数和find_last_of()函数——
- find_first_of(): 返回搜索字符串第一次出现位置
- find_last_of(): 相反
函数原型为:
size_type find_first_of(value_type _Ch,size_type_off=0)cosnt;
size_type find_first_of(const value_type* _ptr,size_type_off=0)cosnt;
size_type find_first_of(const value_type* _ptr,size_type_off=0,size_type_Count)cosnt;
size_type find_first_of(cosnt basic_string& _str,size_type_off=0)cosnt;
size_type find_last_of(value_type _Ch,size_type_off=0)cosnt;
size_type find_last_of(const value_type* _ptr,size_type_off=0)cosnt;
size_type find_last_of(const value_type* _ptr,size_type_off=0,size_type_Count)cosnt;
size_type find_last_of(cosnt basic_string& _str,size_type_off=0)cosnt;
—–find_fist_not_of()和函数find_last_not_of()函数——-
函数原型和前面类似:
size_type find_first_not_of(value_type _Ch,size_type_off=0)cosnt;
size_type find_first_not_of(const value_type* _ptr,size_type_off=0)cosnt;
size_type find_first_not_of(const value_type* _ptr,size_type_off=0,size_type_Count)cosnt;
size_type find_first_not_of(cosnt basic_string& _str,size_type_off=0)cosnt;
9.字符串对配置器的支持
- 配置器的作用就是为容器开辟内存
- 类模板basic_string 的配置器成员:allocator_type
basic_string模板的第三个参数为配置器模板参数:
template<class CharType, class Traits=char_Traits<CharType>, class Allocator=allocator<CharType>
class basic_string;
//string累的声明:
typedef basic_string<char> string;
string类和配置器相关的 函数:get_allocator()
allocator_type string::get_allocator()const;
【引用】
1.《c++STL标准程序库开发指南》第二版