转载地址:http://www.cnblogs.com/archimedes/p/cpp-string.html
string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作。
标准库string类型的目的就是满足对字符串的一般应用。
头文件 #include <string>
1、string对象的定义和初始化
以下是几种初始化string对象的方法:
string s1; //默认构造函数,s1为空串 string s2(s1); //将s2初始化为s1的副本 string s3("value"); //将s3初始化为一个字符串字面值副本 string s4(n,'c'); //将s4初始化字符'c'的n个副本
其他方法:
举例如下:
#include<iostream> #include<string> using namespace std; int main() { char *cp="Hiya"; char carray[]="World!!!!"; char nonull[]={'H','i'}; string s1(cp); //s1=="Hiya" string s2(carray,5); //s2=="World" string s3(carray+5,4); //s3=="!!!!" string s4(nonull); //error string s5(carray,2); //s5=="Hi" cout<<s1<<endl; cout<<s2<<endl; cout<<s3<<endl; cout<<s4<<endl; cout<<s5<<endl; cin.get(); return 0; }
注:s4的定义是错误的,调用这种形式的初始化,其参数必须是以空字符结束的数组,将不包含null的数组传递给此构造函数将导致编译器无法检测的严重错误。
2、string对象的输入和输出
使用标准输入输出操作符来读写string对象:
#include<iostream> #include<string> using namespace std; int main() { string s; cin>>s; cout<<s<<endl; return 0; }
string类型的输入输出操作符:
- 读取并忽略开头所有的空白字符(如空格、换行符、制表符等)
- 读取字符直至再次遇到空白字符,读取终止
我们可以把多个读操作或多个写操作放在一起:
string s1,s2;
cin>>s1>>s2;
cout<<s1<<s2<<endl;
(1)读入未知数目的的string对象
string的输入输出操作符也会返回所读的数据流,可以把输入操作作为判断条件:
int main() { string word; while(cin>>word) cout<<word<<endl; return 0; }
(2)使用getline读取整行文本
getline()这个函数接受两个参数:一个输入流对象和一个string对象。直到遇到换行符才终止。
int main() { string line; while(getline(cin,line)) cout<<line<<endl; return 0; }
3、string对象的操作
s.empty()
|
如果s为空,返回true,否则返回false
|
s.size() 或者 s.length()
|
返回s中字符的个数,返回值是 string::size_type 类型
|
s[n]
|
返回s中位置为n的字符,从0开始计数
|
s1 + s2
| 连接 s1 和 s2 ,返回新字符串 |
s1 = s2
| 把 s1 内容替换成 s2 的副本 |
s1 == s2
| 比较 s1 和 s2,相等返回true,否则为false |
!= , < , <= , > , >=
|
保持这些操作符惯有的含义
|
(1) string的 size 和 empty 操作
常用来作为判断的条件,用法如下:
string st("test");
if(st.size()==0)
//or
if(st.empty())
(2) string::size_type 类型
size()成员函数返回的是string::size_type类型的值,一种unsigned型,事实表明size_type存储的string长度是int所能存储的两倍
可以把一个string对象赋值给另一个string对象:
string s1,s2="test";
s1=s2;
先将s1占用的相关内存释放,分配给s1足够存放s2副本的内存空间,最后把s2中所有字符复制到新分配的内存空间
两个或多个string对象可以通过使用+或者+=连接起来
string s1("hello, ");
string s2("word\n");
string s3=s1+s2;
//or
s1+=s2;
string s1("hello");
string s2("world");
string s3=s1+", "+s2+"\n";
string类型通过下标操作符([])来访问string对象中的单个字符
4、string对象的容器特征
string类型还支持大多数顺序容器的操作,在某些方面,可将string类型视为字符容器。除了一些特殊操作,string类型提供与vector容器相同的操作。
不同的是:它不支持以栈的方式操纵容器:在string类型中不能使用front、back和pop_back操作
string类型提供的操作意味着可将操纵vector对象的程序改为操纵string对象:
#include<iostream> #include<string> using namespace std; int main() { string s("Hello!"); string::iterator iter=s.begin(); while(iter!=s.end()) cout<<*iter++<<endl; cin.get(); return 0; }
(1) string类的迭代器支持
用string::iterator 或 string::const_iterator 声明迭代器变量,const_iterator 不允许改变迭代的内容。
常用迭代器函数:
string::iterator
|
string类的迭代器类型
|
string::const_iterator
|
不允许改变迭代器的内容
|
s.begin()
|
返回string的起始位置 |
s.end()
|
返回string的最后一个字符后面的位置 |
string::reverse_iterator |
|
string::const_reverse_iterator |
|
s.rbegin()
|
返回string的最后一个字符的位置 |
s.rend()
|
返回string第一个字符前面的位置 |
rbegin() 和rend() 用于从后向前的迭代访问,
通过设置迭代string::reverse_iterator,string::const_reverse_iterator实现
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
void main()
{
string s("The zip code of Hondelage in Germany is 38108.");
string sd(s.begin(), s.end());
string::iterator iterA;
for(iterA = sd.begin(); iterA != sd.end(); iterA++)
{
cout << *iterA << " ";
}
string::reverse_iterator iterB;
for(iterB = sd.rbegin(); iterB != sd.rend(); iterB++)
{
cout << *iterB << " ";
}
}
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 pos 2, int n2) const; |
比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小 |
s.assign(s1)
|
用s2的副本替换s
|
s.assign(s2, pos2, len)
|
用s2中从下标pos2开始的len个字符副本替换s
|
s.assign(cp, len)
|
用cp所指向的数组的前len个字符副本替换s
|
s.assign(cp) | 用cp所指向的以空字符结束的字符串副本替换s |
s.erase(first, last)
|
删除迭代器所指范围(first, last)之间的所有字符,返回删除后迭代器的位置
|
s.erase(iter)
|
删除迭代器iter指向的字符,返回删除后迭代器的位置 |
s.erase(pos, len)
|
删除pos开始的n个字符,返回修改后的字符串 |
s.swap(s2) |
交换当前字符串与s2的值 |
s.insert(pos, n, c)
|
在下标为pos的元素之前插入n个字符c
|
s.insert(pos, s2)
|
在下标为pos的元素之前插入string对象s2的副本
|
s.insert(pos, s2, pos2, len)
|
在下标为pos得元素之前插入s2中下标pos2开始的len个字符
|
s.insert(pos, cp, len)
|
在下表为pos的元素之前插入cp所指向的数组的前len个字符
|
s.insert(pos, cp)
|
在下标为pos的元素之前插入cp所指向的数组副本
|
5、只适用于string类型的操作
string类型提供了容器类型不支持的其他几种操作:
(1) substr操作
s.substr(pos, n) |
返回一个string类型的字符串,它包含s中从下标pos开始的n个字符 |
s.substr(pos) |
返回一个string类型的字符串,它包含从下标pos开始到s末尾的所有字符 |
s.substr() |
返回s的副本 |
(2) append()连接函数
s.append(s2)
|
把字符串s2连接到当前字符串的结尾 |
s.append(s2, pos2, len)
|
把字符串s2中从pos2开始的len个字符连接到当前字符串的结尾 |
s.append(cp)
|
把cp指向的数组连接到当前字符串的结尾
|
s.append(cp, len)
| 把cp指向的数组中的前len个字符连接到当前字符串的结尾 |
s.append(n, c)
|
将n个字符c连接到当前字符串的结尾
|
s.append(first, last)
|
将迭代器所指范围(first, last)内的所有字符连接到当前字符串的结尾
|
(3) replace函数
replace函数将这些字符插入到指定位置,从而替换string对象中一段已存在的字符。
s.replase(pos, len, args) |
删除s中从下标pos开始的len个字符,用args所指的字符替换,返回s的引用
|
s.replase(first, last, args) |
删除迭代器所指范围(first, last)内的所有字符,用args所指的字符替换,返回s的引用
|
其中 args 的值为:
6、string类的查找函数
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在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值
int rfind(char c, int pos = npos) const; //从pos开始从后向前查找字符c在当前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值
int find_first_of(char c, int pos = 0) const; //从pos开始查找字符c第一次出现的位置
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos
int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;
int find_first_not_of(const string &s,int pos = 0) const;
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos
int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string &s,int pos = npos) const;
int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos, int n) const;
int find_last_not_of(const string &s,int pos = npos) const;
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找