• 认真地记录技术中遇到的坑!
  • 能摸鱼真是太好啦!嘿嘿嘿!

C++标准库<string>简单总结

C/C++ 悠悠 7年前 (2017-07-20) 2501次浏览 0个评论

C++标准库<string>简单总结

在C++中,如果需要对字符串进行处理,那么它自带的标准库<string>无疑是最好的选择,它实现了很多常用的字符处理函数。

要想使用标准C++中string类,首先包含其头文件:    #include <string>

然后使用string的命名空间:   using  std::string; 或者  using  std::wstring; 或者  using namespace std;

string是使用 char 类型的元素将 basic_string 模板类的专用化描述为 string 的类型;

wstring 是使用 wchar_t 类型的元素将 basic_string 模板类的专用化描述为 wstring 的类型。

而basic_string类是一个模板类,用于描述可存储任意字符类对象序列的对象。

string类的构造函数:

string(const char *s);    // 用c字符串s初始化

string(int n,char c);       // 用n个字符c初始化

string s1;                   // 默认构造函数

string s2=”hello”;        // 复制构造函数

注意:当构造的string太长而无法表达时会抛出length_error异常 ;

string类的字符访问:

string类重载了“[]”运算符,所以你可以像访问数组一样访问它:

const char &operator[](int n)const;  // 返回在字符串中参数指定索引位置的字符引用

char &operator[](int n);        // 返回在字符串中参数指定索引位置的字符引用

不过,还有一种更加安全的方法,使用at()方法访问,会自动检查字符索引的有效性,无效的索引会引发out_of_range异常,不过相对的访问速度会略低于上一种方法:

const char &at(int n)const;      // 返回在字符串中参数指定索引位置的字符引用

char &at(int n);            // 返回在字符串中参数指定索引位置的字符引用

注意:字符串中的第一个元素的索引为零,所给索引必须是给定范围内的正整数,第N 个元素的索引为 n – 1。

string类的转换:

const char *data()const;   //  将字符串的内容转换为以null终止的字符数组

const char *c_str()const;  // 返回一个以null终止的c字符串

以上两种方法的返回值都不建议进行修改或删除,因为string对象不一定是以空字符“\0”终止的,修改可能会引发指针无效等问题。

还可以把字符数列转换为int,float等原生数据类型,相应函数为:stod(string &str, int idx)   // stoi,stof,stol等。

string to_string(int val)        // 将val的值转换为string类型,val 可以为float,double等原生数据类型

string类的特性描述:

int capacity()const;      // 返回当前容量(即string中不必增加内存即可存放的元素个数)

int max_size()const;    // 返回string对象中可存放的最大字符串的长度

int size()const;          // 返回当前字符串的大小

int length()const;        // 返回当前字符串的长度

bool empty()const;      // 当前字符串是否为空

void resize(int len,char c);  // 把字符串当前大小置为len,并用字符c填充不足的部分

string类的输入输出:

getline(istream &is, string &s); // 将字符串从输入流 is 中一行一行地提取出来并存入s中。delim为行分隔符,默认以换行符’\n’分开。多用于文件流读取等。

string类重载了”>>”和”<<“两个运算符,”>>” 读取输入流的字符串的模板函数: cin >> s1; “<<” 字符串写入输出流的模板函数。cout << s2; // 当然咯,cin 和 cout 可以是文件输入和输出流

string类的赋值:

下面的方法的使用方式都是方法调用,比如  str1.assign(str2)

string &operator=(const string &s);   // 把字符串s赋给当前字符串,重载了”=”运算符

string &assign(const char *s);       // 用c类型字符串s赋值

string &assign(const char *s,int n);   // 用c字符串s开始的n个字符赋值

string &assign(const string &s);    // 把字符串s赋给当前字符串

string &assign(int n,char c);      // 用n个字符c赋值给当前字符串

string &assign(const string &s,int start,int n);      //把字符串s中从start开始的n个字符赋给当前字符串

string &assign(const_iterator first,const_itertor last);  //把first和last迭代器之间的部分赋给字符串

string类的子串:

string substr(int pos = 0,int n = npos) const;      //返回pos开始的n个字符组成的字符串

string类的连接:

string &operator+=(const string &s);    //把字符串s连接到当前字符串的结尾

string &append(const char *s);             //把c类型字符串s连接到当前字符串结尾

string &append(const char *s,int n);    //把c类型字符串s的前n个字符连接到当前字符串结尾

string &append(const string &s);         //同operator+=()

string &append(const string &s,int pos,int n);      //把字符串s中从pos开始的n个字符连接到当前字符串的结尾

string &append(int n,char c);             //在当前字符串结尾添加n个字符c

string &append(const_iterator first,const_iterator last);   //把迭代器first和last之间的部分连接到当前字符串的结尾

string类的比较:

bool operator==(const string &s1,const string &s2)const;       // 比较两个字符串是否相等,运算符”>”,”<“,”>=”,”<=”,”!=”均被重载用于字符串的比较;

int compare(const string &s) const;                  // 比较当前字符串和s的大小

int compare(int pos, int n,const string &s)const;           // 比较当前字符串从pos开始的n个字符组成的字符串与s的大小

int compare(int pos, int n,const string &s,int pos2,int n2)const;    // 比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小

相应的有用于C语言字符串的比较方法

int compare(const char *s) const;

int compare(int pos, int n,const char *s) const;

int compare(int pos, int n,const char *s, int pos2) const;

compare函数在>时返回1,<时返回-1,==时返回0 

string类的查找:

注意:string::npos 表示当搜索功能失败时,无符号整数值初始化到 –1,指示“未找到”或“所有剩余字符”。

// 从给定位置从前向后查找给定字符(串)位置,查找成功时返回匹配串的开始索引,查找失败是返回npos

int find(char c, int pos = 0) const;      //从pos开始查找字符c在当前字符串的位置

int find(const char *s, int pos = 0) const;      //从pos开始查找字符串s在当前串中的位置

int find(const char *s, int pos, int n) const;  //从pos开始查找字符串s中前n个字符在当前串中的位置

int find(const string &s, int pos = 0) const;  //从pos开始查找字符串s在当前串中的位置

相应的还有其他的查找函数,都是上面的四种重载方式

int rfind(char c, int pos = 0) const;         // 反向搜索字符串,查找最先出现的匹配指定字符顺序的子字符串。

int find_first_not_of(char c, int pos = 0) const;  // 搜索字符串不是指定字符串元素的第一个字符的索引

int find_first_of(char c, int pos = 0) const;     // 在字符串中搜索与指定字符串中任何元素匹配的第一个字符。

int find_last_not_of(char c, int pos = 0) const;   // 在字符串中搜索不属于指定字符串元素的最后一个字符。

int find_last_of(char c, int pos = 0) const;          // 搜索字符串与指定字符串匹配的所有元素的最后一个字符的。

string类的替换:

string &replace(int p0, int n0,const char *s);            // 删除从p0开始的n0个字符,然后在p0处插入串s

string &replace(int p0, int n0,const char *s, int n);         // 删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符

string &replace(int p0, int n0,const string &s);          // 删除从p0开始的n0个字符,然后在p0处插入串s

string &replace(int p0, int n0,const string &s, int pos, int n);     // 删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符

string &replace(int p0, int n0,int n, char c);            // 删除p0开始的n0个字符,然后在p0处插入n个字符c

string &replace(iterator first0, iterator last0,const char *s);     // 把[first0,last0)之间的部分替换为字符串s

string &replace(iterator first0, iterator last0,const char *s, int n);  // 把[first0,last0)之间的部分替换为s的前n个字符

string &replace(iterator first0, iterator last0,const string &s);   // 把[first0,last0)之间的部分替换为串s

string &replace(iterator first0, iterator last0,int n, char c);     // 把[first0,last0)之间的部分替换为n个字符c

string
&replace(iterator first0, iterator last0,const_iterator first,
const_iterator last);  // 把[first0,last0)之间的部分替换成[first,last)之间的字符串

string类的插入:

// 插入一个元素或多个元素或给定字符串一定范围的元素到指定位置的字符串中。

string &insert(int p0, const char *s);

string &insert(int p0, const char *s, int n);

string &insert(int p0,const string &s);

string &insert(int p0,const string &s, int pos, int n);

//前4个函数在p0位置插入字符串s中pos开始的前n个字符

string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c

iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置

void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符

void insert(iterator it, int n, char c);//在it处插入n个字符c

string类的删除:

string &erase(int pos = 0, int n = npos); // 删除pos开始的n个字符,返回修改后的字符串

iterator erase(iterator first, iterator last); // 删除[first,last)之间的所有字符,返回删除后迭代器的位置

iterator erase(iterator it);         // 删除it指向的字符,返回删除后迭代器的位置

string类的迭代器处理:

string类提供了向前和向后遍历的迭代器iterator,迭代器提供了访问各个字符的语法,类似于指针操作,迭代器不检查范围。

用string::iterator或string::const_iterator声明迭代器变量,const_iterator不允许改变迭代的内容。常用迭代器函数有:

const_iterator begin()const;

iterator begin();                //返回string的起始位置

const_iterator end()const;

iterator end();                    //返回string的最后一个字符后面的位置

const_iterator rbegin()const;

iterator rbegin();                //返回string的最后一个字符的位置

const_iterator rend()const;

iterator rend();                    //返回string第一个字符位置的前面

rbegin和rend用于从后向前的迭代访问,通过设置迭代器string::reverse_iterator,string::const_reverse_iterator实现

成员函数表

append 向字符串的末尾添加字符。
assign 对字符串的内容赋新的字符值。
at 返回对字符串中指定位置的元素的引用。
back 返回字符串中最后一个元素的引用
begin 返回发现字符串中第一个元素的位置的迭代器。
c_str 将字符串的内容转换为以 null 结尾的 C 样式字符串。
capacity 返回在不增加字符串内存分配的情况下可存储在字符串中的元素的最大数目。
cbegin 返回发现字符串中第一个元素的位置的常量迭代器。
cend 返回发现字符串中最后一个元素之后的位置的常量迭代器。
clear 清除字符串中的全部元素。
compare 将字符串与指定字符串比较,确定两个字符串是否相等或按字典顺序一个字符串是否小于另一个。
copy 将指定数目的字符从源字符串中的索引位置复制到目标字符组。 已否决。 请改用 basic_string::_Copy_s。
crbegin 返回发现反向字符串中第一个元素的位置的常量迭代器。
crend 返回发现反向字符串中最后一个元素之后的位置的常量迭代器。
_Copy_s 将指定数目的字符从源字符串中的索引位置复制到目标字符组。
data 将字符串的内容转换为字符数组。
empty 测试字符串是否包含字符。
end 返回发现字符串中最后一个元素之后的位置的迭代器。
erase 从字符串中的指定位置删除一个或一系列元素。
find 向前搜索字符串,搜索与指定字符序列匹配的第一个子字符串。
find_first_not_of 在字符串中搜索不属于指定字符串中元素的第一个字符。
find_first_of 在字符串中搜索与指定字符串中任何元素匹配的第一个字符。
find_last_not_of 在字符串中搜索不属于指定字符串中任何元素的最后一个字符。
find_last_of 在字符串中搜索属于指定字符串中一个元素的最后一个字符。
front 返回对字符串中第一个元素的引用。
get_allocator 返回用于构造字符串的 allocator 对象的副本。
insert 将一个、多个或一些列元素插入字符串中的指定位置。
length 返回字符串中元素的当前数目。
max_size 返回字符串可包含的字符的最大数目。
pop_back 删除字符串的最后一个元素。
push_back 在字符串的末尾处添加一个元素。
rbegin 返回指向反向字符串中第一个元素的迭代器。
rend 返回指向刚超出反向字符串的最后一个元素的位置的迭代器。
replace 用指定字符或者从其他范围、字符串或 C 字符串复制的字符来替代字符串中指定位置的元素。
reserve 将字符串的容量设置为一个数目,这个数目至少应与指定数目一样大。
resize 根据要求追加或删除元素,为字符串指定新的大小。
rfind 向后搜索字符串,搜索与指定字符序列匹配的第一个子字符串。

注意:有参数的方法都重载了好几种参数形式,参数的形式参照上面列出来的一些方法

(其实是人太懒,暂时不列出了,希望周末能把这个坑补上)


转载请注明出处 C++标准库<string>简单总结
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址