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>简单总结的更多相关文章

  1. 实现C++标准库string类的简单版本

    代码如下: #ifndef STRING_H #define STRING_H #include <cassert> #include <utility> #include & ...

  2. C++标准库string类型

    string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作.标准库string类型的目的就是满足对字符串的一般应用. 本文地址:http://www.cn ...

  3. C++标准库string

    C++标准库string 定义和初始化 string s1 默认初始化,s1是一个空串 string s2(s1) s2是s1的副本 string s2 = s1 等价于s2(s1),s2是s1的副本 ...

  4. c/c++ 标准库 string

    c/c++ 标准库 string 标准库 string的小例子 test1~test10 #include <iostream> using namespace std; int main ...

  5. C 标准库 - string.h

    C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...

  6. 标准库string与C风格字符串

    返回字符串的长度 string标准库 #include<iostream> #include<cstring> using namespace std; int main() ...

  7. 【C++ Primer每日刷】之三 标准库 string 类型

    标准库 string 类型 string 类型支持长度可变的字符串.C++ 标准库将负责管理与存储字符相关的内存,以及提供各种实用的操作.标准库string 类型的目的就是满足对字符串的一般应用. 与 ...

  8. C标准库<string.h>实现

    本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...

  9. 把《c++ primer》读薄(3-1 标准库string类型初探)

    督促读书,总结精华,提炼笔记,抛砖引玉,有不合适的地方,欢迎留言指正. 问题1:养成一个好习惯,在头文件中只定义确实需要的东西 using namespace std; //建议需要什么再using声 ...

随机推荐

  1. (转载)让ie6也支持max-width,和max-height实现图片等比例缩放

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. location对象位置操作,进行跳转

    location位置操作,进行跳转 location.assign("http://www.baidu.com") 跳转,打开新的url 等价于,将location.href或wi ...

  3. gitHub远程分支创建

    1.在本地master上分出一个项目分支 git checkout -b bug1_dujie git checkout -b bug1_dujie origin/master 在远程添加分支 $gi ...

  4. MongoDB-性能优化之索引

    首先看一个实例 >;i<;i++){ db.indexdemo.insert({),"create":new Date});} WriteResult({ }) > ...

  5. hdu2524 (求矩形个数,水题。。。)

    hdu 2524 N - 暴力求解.打表 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  6. C语言指针类型 强制转换

    关于C语言指针类型 强制转换  引用一篇文章: C语言中,任何一个变量都必须占有一个地址,而这个地址空间内的0-1代码就是这个变量的值.不同的数据类型占有的空间大小不一,但是他们都必须有个地址,而这个 ...

  7. libcurl 下载上传

    近来一个新的项目需要使用到http. 本来用socket来写一个的,后来发现功能实在太简单,有点捉襟见肘. 于是改用libcur来做. 首先下载libcur的源码,然后配置: ./configure ...

  8. Topk引发的一些简单的思考

    软件工程课程的一个题目:写一个程序,分析一个文本文件中各个词出现的频率,并且把频率最高的10个词打印出来.文本文件大约是30KB~300KB大小. 首先说一下这边的具体的实现都是在linux上实现的. ...

  9. poj Candies

    http://poj.org/problem?id=3159 #include<cstdio> #include<queue> #include<cstring> ...

  10. UOJ 52 元旦激光炮

    http://uoj.ac/problem/52 题意:每次可以得到3个序列中 思路:每次分别取出三个序列的K/3长度的位置,取最小的那个,然后每次减掉它,总复杂度是Nlog3N #include & ...