C++中的String
一、string类包含在头文件<string>中,并使用名称空间std。
#include<string>
using namespace std;
二、string的构造函数。
1. string (); // 创建一个默认的string对象,长度为0
string s1; // 空字符串
2. string (size_type n, char c); // 创建一个包含n个元素的string对象,其中每个元素都被初始化为字符c
string s2(3,'s'); // "sss"
3. string (const char *str); // str指向NBTS(null-terminated string),即以空字符结束的字符串
char arr[] = {'a', 'b', 'c', '\0'};
string s3(arr); // "abc"
string s3("abc"); // 同上
4. string(const string &str); // 复制构造函数
string s4(s3);
5. string (const char *str, size_type n); // 将string对象初始化为str指向的NBTS的前n个字符
string s5(arr,2) // "ab"
6. string (const string &str, string size_type pos = 0, size_type n = npos);// 将string初始化为对象str中从位置 pos开始到结尾的字符,或从位置pos 开始的n个字符
string s6(s3,1,2) // "bc"
7. template<class Iter>
string (Iter begin, Iter end); //将string对象初始化为区间[ begin, end)内的字符,其中begin和end的行为就像指针,用于指定位置,包括begin,不包括end
char alls[] = "All's well that ends well";
string str1(alls);
string str2(alls+6,alls+10); //"well" 6,7,8,9四个字符
string str3(&str1[6], &str1[10]); //"well"
其他初始化方式:
string s3 = s2; //通过拷贝构造函数,初始化s3
string s1 = "abcdefg"; // "abcdefg"
赋值方式:
string name("eweqedqrq");
string pres,veep,source,join,awkward;
pres = name;
veep = "dsadadad";
source = 'x';
join = name + source;
awkward = {'w','w','s','c'};
这里如果出现下面的语句,输出会乱码:
char arr[] = { 'w', 'w', 's', 'c' };
awkward = arr;
三、对string的一些操作
1. cin string对象时,会自动以空格为止,读取整行可采用以下代码:
string str1;
getline(cin, str1);
getline(cin, str1, 'c'); //读到 'c' 为止
2. size() 和 length()
都返回string对象中的元素数。
3. 字符串存取
string word("ewqehqf");
cout<<word[3]<<endl;
word[3] = 'r'; //既可以访问,也可以改值
const ward("dasas");
cout<<ward[5]<<endl; //只能访问,不能改值
at提供类似的操作,word.at(pos)...,有一个差别是,at会检查边界,如果传入的索引值pos>=size(),将引发out_of_range异常。
4. 字符串搜索
find()
size_type find(const string & str, size_type pos = 0)const
从字符串的pos位置开始查找子字符串str,如果找到,返回该字符串首次出现时,其首字符的索引;否则,返回string::npos。
size_type find(const char *s, size_type pos = 0)const
从字符串的pos位置开始查找子字符串s,如果找到,返回该字符串首次出现时,其首字符的索引;否则,返回string::npos。
size_type find(const char *s, size_type pos = 0, size_type n)
从字符串的pos位置开始,查找s的前n个字符组成的子字符串,如果找到,返回该字符串首次出现时,其首字符的索引;否则,返回string::npos。
size_type find(char ch, size_type pos = 0)const
从字符串的pos位置开始查找字符ch,如果找到,返回该字符串首次出现时,其首字符的索引;否则,返回string::npos。
rfind()
size_type rfind(const basic_string &str, size_type pos = npos) const noexcept;
size_type rfind(const chatT *s, size_type pos = npos) const;
size_type rfind(const chatT *s, size_type pos = npos, size_type n) const;
size_type rfind(chatT c, size_type pos = npos) const noexcept;
与find()相似,只是它们搜索字符串最后一次出现的位置,该位置位于npos之前,如果没有找到,该方法将返回npos。
find_first_of()
size_type find_first_of(const basic_string &str, size_type pos = 0)const noexcept;
size_type find_first_of(const char *s, size_type pos, size_type n)const;
size_type find_first_of(const char *s, size_type pos = 0)const;
size_type find_first_of(char c,size_type pos = 0)const noexcept;
与find()相似,但它们不是搜索整个子字符串,而是搜索子字符串中的字符首次出现的位置。例如:
string longer("That is a funny hat.");
string shorter("fluke");
size_type loc1 = longer.find_first_of(shorter); //loc1 = 10
size_type loc2 = longer.find_first_of("fat"); //loc2 = 2
在longer中,首次出现的fluke中的字符是funny中的f,而首次出现的fat中的字符是That中的a。
find_last_of()
size_type find_last_of(const basic_string &str, size_type pos = npos)const noexcept;
size_type find_last_of(const char *s, size_type pos, size_type n)const;
size_type find_last_of(const char *s, size_type pos = npos)const;
size_type find_last_of(char c,size_type pos = npos)const noexcept;
与find_last_of()相似,只是它们搜索字符串最后一次出现的位置,该位置位于npos之前,如果没有找到,该方法将返回npos。例如:
string longer("That is a funny hat.");
string shorter("hat");
size_type loc1 = longer.find_last_of(shorter); //loc1 = 18
size_type loc2 = longer.find_last_of("any"); //loc2 = 17
在longer中,最后出现的hat中的字符是hat中的t,而最后出现的any中的字符是hat中的a。
find_first_not_of()
size_type find_first_not_of(const basic_string &str, size_type pos = 0)const noexcept;
size_type find_first_not_of(const char *s, size_type pos, size_type n)const;
size_type find_first_not_of(const char *s, size_type pos = 0)const;
size_type find_first_not_of(char c,size_type pos = 0)const noexcept;
与find_first_of()类似。
find_last_not_of()
size_type find_last_not_of(const basic_string &str, size_type pos = npos)const noexcept;
size_type find_last_not_of(const char *s, size_type pos, size_type n)const;
size_type find_last_not_of(const char *s, size_type pos = npos)const;
size_type find_last_not_of(char c,size_type pos = npos)const noexcept;
与find_last_of()类似。
5. 比较方法和函数
string类提供了用于比较2个字符串的方法的函数compare,有如下重载:
int compare(const basic_string &str) const noexcept;
string s1("bellflower");
string s2("bell");
string s3("cat");
int a13 = s1.compare(s3); //a13 is < 0
int a12 = s1.compare(s2); //a12 is > 0
如果根据traits::compare() 提供的顺序,第一个字符串位于第二个字符串之前,则返回一个小于0的值,如果这两个字符串相同,则他将返回0,如果第一个字符串位于第二个字符串之后,则他将返回一个大于0的值。如果较长的字符串的前半部分与较短的字符串相同,则较短的字符串将位于较长的字符串之前。如果两个一个大写一个小写,则大写在前,小写在后,A->a->B->b...
int compare(size_type pos1, size_type n1, const basic_string &str)const;
string s1("bellflower");
string s2("bell");
int a2 = s1.compare(0, 4, s2); //a2 is 0
取第一个字符串从pos1开始的n1个字符与第二个字符串比较
int compare(size_type pos1, size_type n1, const basic_string &str, size_type pos2, size_type n2)const;
取第一个字符串从pos1开始的n1个字符与第二个字符串从pos2开始的n2个字符比较
int compare(const char *s)
将一个字符数组而不是string对象作为第二个字符串
int compare(size_type pos1, size_type n1, const char *s)const;
int compare(size_type pos1, size_type n1, const char *s, size_type pos2, size_type n2)const;
这俩同理。
6. 字符串修改方法
6.1用于追加和相加的方法
可以使用重载的 += 运算符或append()方法将一个字符串追加到另一个字符串的后面。如果得到的新字符串长于最大字符串长度,将引发length_error异常。
basic_string & operator += (const basic_string &str); //加string对象
basic_string & operator += (const char *s); //加NBTS,要以 '\0' 结尾,否则会出现乱码,猜测是自动寻找到内存中的 '\0' 为止
basic_string & operator += (const char c); //加字符
basic_string & append (const basic_string &str); //加string对象
basic_string & append (const basic_string &str, size_type pos, size_type n); //加string对象的一部分
basic_string & append (const char *s); //加NBTS
basic_string & append (const char *s, size_type n);
basic_string & append (size_type n, char c); //加n个字符c
templete<class InputIterator>
basic_string & append(InputIterator first, InputIterator last); //在末尾加[first, last)之间的内容
void push_back(char c); //在末尾加字符c
除此之外,还可以使用+号:
string st1("red");
string s2("rain");
string s3 = st1 + "uce";
string s4 = 't' + st2;
string s5 = st1 + st2;
6.2其他赋值方法
除了基本的赋值运算符外,string类还提供了assign()方法,该方法使得能够将整个字符串、字符串的一部分或由相同字符组成的字符序列赋给string对象。
basic_string & assign(const basic_string &str);
basic_string & assign(basic_string &&str)noexcept; //接受右值引用作为参数,C++11特性
basic_string & assign(const basic_string &str,size_type pos, size_type n);
basic_string & assign(const charT* s, size_type n);
basic_string & assign(const charT* s);
basic_string & assign(size_type n,charT c);
basic_string & assign(initializer_list<charT>);
templete<class InputIterator>
basic_string& assign(InputIterator first, InputIterator last);
例子:
string test;
string stuff("set tubs clones ducks");
test.assign(stuff,1,5); // test is "et tu"
test.assign(6,'#'); //test is "######"
6.3插入方法
insert()方法能将string对象、字符串数组、或几个字符插入到string对象中。如果pos1超过了目标字符串结尾,或者pos2超过了要插入的字符串结尾,将引发out_of_range异常,如果得到的字符串长度大于max_size(),将引发length_error异常。
basic_string & insert(size_type pos1, const basic_string &str); //在pos处插入string对象
basic_string & insert(size_type pos1, const basic_string &str, size_type pos2, size_type n); //在pos1处插入pos2的一部分
basic_string & insert(size_type pos, const char *s, size_type n); //在pos处插入字符数组的前8个元素
basic_string & insert(size_type pos, const char *s); //在pos处插入字符数组
basic_string & insert(size_type pos, size_type n, const char c); //在pos处插入n个字符c
iterator insert(const_iterator p, charT c);
iterator insert(const_iterator p, size_type n, charT c);
template<class InputIterator>
void insert(iterator p, InputIterator first, InputIterator last);
iterator insert(const_iterator p, initializer_list<charT>);
6.4清除方法
erase()方法从字符串中删除字符
basic_string &erase(size_type pos = 0, size_type n = npos); //从pos位置开始,删除n个字符或删除到字符串尾
iterator erase(const_iterator position); //删除迭代器位置引用的字符,并返回指向下一元素的迭代器,如果没有其他元素,返回end()
iterator erase(const_iterator first, const_iterator last); // 删除区间[first, last)中的字符,返回最后一个迭代器,该迭代器指向最后一个被删除元素后面的一个元素
void pop_back; //删除最后一个字符
6.5替换方法
replace()
basic_string & replace(size_type pos1, size_type n1, const basic_string &str); //从pos1位置开始的n1个元素被str替换
basic_string & replace(size_type pos1, size_type n1, const basic_string &str, size_type pos2, size_type n2); //从pos1位置开始的n1个元素被str的一部分替换
basic_string & replace(size_type pos, size_type n1, const charT* s, size_type n2); //从pos1位置开始的n1个元素被 s 的前n2个元素替换
basic_string & replace(size_type pos, size_type n1, const charT* s); //从pos1位置开始的n1个元素被字符数组 s 替换
basic_string & replace(size_type pos, size_type n1, size_type n2, charT c); //从pos1位置开始的n1个元素被n2个字符c替换
basic_string & replace(const_iterator i1, const_iterator i2, const basid_string &str);
basic_string & replace(const_iterator i1, const_iterator i2, const char* s,size_type n);
basic_string & replace(const_iterator i1, const_iterator i2, const charT* s);
basic_string & replace(const_iterator i1, const_iterator i2, size_type n, charT c);
template<class InputIterator>
basic_string& replace(const_iterator i1, const_iterator i2, InputIterator j1, InputIterator j2);
basic_string & replace(const_iterator i1, const_iterator i2, initializer_list<charT>);
6.6其他修改方法
copy()和swap()
copy()方法将string对象或其中的一部分复制到指定的字符串数组中:
size_type copy(charT* s, size_type n, size_type pos = 0)const; //s指向目标数组,n是要复制的字符数,pos指出从string对象的什么位置开始复制,函数返回复制的字符数(<=n)。
swap()方法交换两个string对象的内容:
void swap(basic_staring& str);