创建 string 对象
字符串的操作,需要先用 string 类型的构造函数,构造出一个 string 对象,为随后添加的字符分配内存,并初始化 string 对象的内部变量。如下是它的几个常用的构造函数原型。
1. string()
默认的构造函数,用于创建一个不包含任何非空字符的 string 对象。
例如,下面一行代码创建空的 string 对象 s .
string s;
2 string(const string& s, size_type pos = 0, size_type n=npos)
拷贝构造函数,将 string 对象 s 的 [pos, pos+n) 中的字符拷贝到新创建的 string 对象空间中,其中 string::npos 已定义为 -1。当 pos取值0,n取值 string::npos 时,就是用整个字符串 s 拷贝创建一个新的 string 对象。
例如,下面代码拷贝创建 string 对象 s2
// string s1;
string s2(s1);
3. string(const char*)
将 char* 数组的字符拷贝到新创建的 string 对象空间中。
例如,下面代码将字符数组 cArray 的3个字符,拷贝到 string 对象 s 中。
char* cArray="abc";
string 的其他常用函数用法
字符串的操作,需要先用 string 类型的构造函数,构造出一个 string 对象,为随后添加的字符分配内存,并初始化 string 对象的内部变量。如下是它的几个常用的构造函数原型。
1. string()
默认的构造函数,用于创建一个不包含任何非空字符的 string 对象。
例如,下面一行代码创建空的 string 对象 s .
string s;
2 string(const string& s, size_type pos = 0, size_type n=npos)
拷贝构造函数,将 string 对象 s 的 [pos, pos+n) 中的字符拷贝到新创建的 string 对象空间中,其中 string::npos 已定义为 -1。当 pos取值0,n取值 string::npos 时,就是用整个字符串 s 拷贝创建一个新的 string 对象。
例如,下面代码拷贝创建 string 对象 s2
// string s1;
string s2(s1);
3. string(const char*)
将 char* 数组的字符拷贝到新创建的 string 对象空间中。
例如,下面代码将字符数组 cArray 的3个字符,拷贝到 string 对象 s 中。
char* cArray="abc";
string s(cArray); // s 包含字符 'a', 'b', 'c' 和一个 null 字符
字符添加
<span style="font-size:14px;">字符添加
/* 字符的添加
string 对象具有如下几个常用的添加字符或字符串的函数,并提供了与这些添加函数相对应的更方便的字符串 "+" 和 "+=" 操作。
void push_back(char c) // 在 string 对象的字符串尾部添加一个【字符】 c
string& append(const char* s) // 在 string 对象的字符串尾部添加【字符串】 s
string& append(const string& s) // 在 string 对象的字符串尾部添加 s 对象的【字符串】
iterator insert(iterator pos, const char& c) // 在 string 对象的 pos 位置之前,插入一个字符 c
下面的示例程序创建 string 对象 str1 和 str2 ,然后使用上面的函数或操作,进行字符或字符串的添加操作。
*/
-------------------------------------------------------- 字符添加
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str1("abcd");
/*
VC6.0 版本的编译器的 STL 有点点老旧了。不支持string 下面的 push_back 操作,
在vs2010下面测试,string 里面,是支持 push_back操作的
*/
// str1.push_back('a');
// str1.push_back('b');
// str1.push_back('c');
// str1.push_back('d');
cout << "打印str1: "<< str1 << endl;
char* cArray = "efgh";
string str2(cArray);
cout << "打印str2: " << str2 << endl;
// 字符串的 + 操作
cout << "打印str1+str2: " << str1+str2 << endl;
// 字符串 str1后添加字符串 str2
cout << "append后,打印 str1: " << str1.append(str2) << endl;
// 在 str1 的第2个字符位置前插入字符 '8'
string::iterator i;
i = str1.begin();
i++;
str1.insert(i, '8');
cout << "insert后,打印 str1: " << str1 << endl;
// 字符串的 "+=" 操作
str1 += str2;
cout << "str1+=str2 后,打印 str1: " << str1 << endl;
return 0;
}</span>
字符的遍历
/* 字符的遍历访问
string 字符串提供了数组操作符 “[]” ,用于访问指定索引位置处的字符。一般的字符遍历则是使用前向迭代器和反向迭代器来进行。此时,通常要用 begin/rbegin 和 end/rend 函数找出遍历开始的首字符和末字符,然后通过迭代器的 “++” 和 “*” 操作,读取字符串中的字符。
下面的示例程序,先用数组方式,将字符串的字符逐个打印出来。然后,用反向迭代器,将字符串逆序打印:
*/
-------------------------------------------------------- 字符的遍历
#include <string>
#include <iostream>
using namespace std;
int main()
{
char* cArray = "Hello, world!";
string str(cArray);
// 数组方式
for (size_t j=0; j<str.size(); j++)
cout << "第" << j << "个字符:" << str[j] << endl;
// 迭代器方式
string::reverse_iterator ri, rend;
rend = str.rend();
cout << "反向打印字符:" << endl;
for (ri=str.rbegin(); ri!=rend; ++ri)
cout << *ri << " ";
cout << endl;
return 0;
}
字符的删除
字符的删除
/* 字符的删除
字符串中字符的删除,可使用如下几个重载的 erase 和 clear 函数,分别删除指定位置上的若干字符或删除所有的字符。
iterator erase(iterator pos) // 删除字符串中 position 所指的字符
iterator erase(iterator first, iterator last) // 删除字符串中迭代器区间 [first, last) 上的所有字符
string& erase(size_type pos=0, size_type n=npos) // 删除字符串中从索引位置 pos 开始的 n个字符。
void clear() // 删除字符串中的所有字符
下面的程序给出了字符删除的示例:
*/
-------------------------------------------------------- 字符的删除
#include <string>
#include <iostream>
using namespace std;
int main()
{
char* cArray = "a12345678b";
string str(cArray);
// 删除第一个字符
str.erase(str.begin());
// 打印出 12345678b
cout << str << endl;
// 删除操作
str.erase(str.begin()+3, str.end()-2);
// 打印出 1238b
cout << str << endl;
// str.clear(); // 在VC6.0 下面又没有编译通过,VC6.0的 STL 有些老旧了
return 0;
}
字符的替换
字符的替换
/* 字符的替换
string 提供了相当多的字符替换的重载函数 replace ,可将指定索引位置或迭代器位置处的字符替换。如下是几个常用的函数原型说明:
string& replace(size_type pos, size_type n, const char* s) // 将当前字符串从 pos 索引位置开始的 n 个字符,替换为字符串 s
string& replace(size_type pos, size_type n, size_type n1, char c) // 将当前字符串从 pos 索引位置开始的 n 个字符,替换为 n1 个字符 c
string& replace(iterator first, iterator last, const char* s) // 将当前字符串的 [first, last) 区间中的字符替换为字符串 s
*/
-------------------------------------------------------- 字符的替换
#include <string>
#include <iostream>
using namespace std;
int main()
{
char* cArray = "hello,boy!";
string str(cArray);
// boy 替换 girl
str.replace(6,3,"girl");
// 打印 hello girl!
cout << str << endl;
// 将“!”替换为“。”
str.replace(10, 1, 1, '.');
// 打印 hello girl.
cout << str << endl;
str.replace(str.begin(), str.begin()+5, "good morning");
// 打印 good morning girl.
cout << str << endl;
return 0;
}
字符的搜索
字符的搜索
/* 字符的搜索
string 提供了如下几个比较常用的在字符串中搜索匹配的字符串或字符的函数
size_type find(const char* s, size_type pos=0) // 在当前字符串 pos 索引位置开始,查找子串 s ,返回找到的位置索引, -1 表示查找不到子串。
size_type find(char c, size_type pos=0) // 在当前字符串的 pos 索引位置开始,查找字符串 c 的索引位置,返回 -1 表示查找不到字符 c
size_type rfind(const char* s, size_type pos=npos) // 从当前字符串的 pos 索引位置开始,反向顺序查找子串 s ,返回找到字符串的索引位置, -1 表示查找不到该子串。
size_type rfind(char c, size_type pos=npos) // 从当前字符串的 pos 索引位置开始,查找位于子串 s 的字符,返回找到字符的索引位置, -1 表示查找不到该字符。
size_type find_first_of(const char* s, size_type pos=0) // 从当前字符串的 pos 索引位置开始,查找位于子串 s 的字符,返回找到字符的索引位置, -1 表示查找不到。
size_type find_first_not_of() //从当前字符串的 pos 索引位置开始,查找第1个不位于子串 s 的字符,返回找到字符的索引位置, -1 表示查找不到。
size_type find_last_of() // 从当前字符串的 pos 索引位置开始,查找最后一个子串 s 的字符,返回找到的字符的索引位置, -1 表示查找不到字符。
size_type find_last_not_of() // 从当前字符串的 pos 索引位置开始,查找最后一个不位于子串 s 的字符,返回找到的字符的索引位置, -1 表示查找不到字符。
下面的示例程序,使用了以上的函数进行字符串和字符的搜索
*/
-------------------------------------------------------- 字符的搜索
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str("dog bird chicken bird cat");
// 字符串查找
cout << str.find("bird") << endl; // 打印 4
cout << (int)str.find("pig") << endl; // 打印 -1
// 字符查找
cout << str.find('i', 7) << endl; // 打印 11
// 从字符串的 末尾 开始查找字符串
cout << str.rfind("bird") << endl; // 打印 17
// 从字符串的 末尾 开始查找字符
cout << str.rfind('i') << endl; // 打印 18
// 查找第1个属于某子串的字符
cout << str.find_first_of("13r98") << endl; // 找到字符 r, 打印 6
// 查找第1个不属于某字符串的字符
cout << str.find_first_not_of("dog bird 2009") << endl; // 找到字符 c , 打印 9
// 查找最后一个属于某子串的字符
cout << str.find_last_of("13r98") << endl; // 字符 r,打印19
// 查找最后一个不属于某字符串的字符
cout << str.find_last_not_of("tea") << endl; // 字符 c, 打印 22
return 0;
}
字符串的比较
字符串的比较
/* 字符串的比较
string 提供了字典式的比较函数 compare ,它的几个常用函数原型如下:
int compare(const string& s) // 将当前字符串与字符串 s 比较,相等返回 0 ,大于 s 返回 1 ,小于 s 返回 -1
int compare(size_type pos, size_type n, const string& s) // 将当前字符串从 pos 索引位置开始的 n 个字符构成的字符串与字符串 s 比较,相等返回 0 ,大于 s 返回 1 ,小于 s 返回 -1
int compare(const char* s) // 直接将当前字符串与字符串 s 比较。相等返回 0 ,大于 s 返回 1 ,小于 s 返回 -1
下面的示例程序使用 compare 函数,比较并打印字符串比较结果
*/
-------------------------------------------------------- 字符串的比较
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str1("abcdef");
string str2("abc");
// 相等,打印0
cout << str1.compare("abcdef") << endl;
// str1 > str2 , 打印 1
cout << str1.compare(str2) << endl;
// str1 < "abyz" , 打印 -1
cout << str1.compare("abyz") << endl;
// str1 的前3个字符 == str2 , 打印 0
cout << str1.compare(0, 3, str2) << endl;
return 0;
}
string 的其他常用函数用法
string 的其他常用函数用法
/* 其他常用函数
string 字符串还有以下几个比较常用的函数,如下是他们的使用原型说明。
size_type length() // 字符串的非空字符个数,即字符串的长度
size_type size() // 返回字符串的长度
bool empty() // 字符串是否为空,空则返回 1,否则返回 0
const char* c_str() // 将string 对象转换为 c 字符数组。 (***)
*/
-------------------------------------------------------- string 的其他常用函数用法
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str;
// 空字符串,返回 1
cout << str.empty() << endl;
str += "1234567";
cout << str.empty() << endl; // 不为空,返回0
cout << str.size() << endl; // 7个字符,返回7
cout << str.length() << endl; // 返回 7
// 转换为字符数组 cArray
const char* cArray=str.c_str();
// 返回字符 3
cout << cArray[2] << endl;
return 0;
}
// 补充几个其他的用法: 数组方式访问,或者.at() 函数方式访问。
#include <string>
#include <iostream>
using namespace std;
int main()
{
string s1 = "abcdefghijklmnopqrst";
char chA = s1[3]; // 将第4个字符赋给chA,chA值为d
char chB = s1.at(5); // 将第5个字符赋给chB,chB值为f
char chC = s1[50]; // 数组越界
// char chD = s1.at(50); // 产生out_of_rang 异常,可以用try,catch捕获异常
cout << s1.c_str() << endl; // 输出字符串 s1
return 0;
}
string 的赋值
string &operator=(const string &s);//把字符串s赋给当前的字符串
string &assign(const char *s); //把字符串s赋给当前的字符串
string &assign(const char *s, int n); //把字符串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的子串
string substr(int pos=0, int n=npos) const; //返回由pos开始的n个字符组成的子字符串
string 的 assing 用法:
// 自己去用代码实现,熟悉其含义和用法
string strA("到此一游");
string strC;
strC = strA;
strC.assign("abcdefghijk");
strC.assign("baidu");
strC.assign(strA);
strC.assign(5,'f');