在 C++ 中,为了方便处理字符串,引入了 string 类。string 类型支持长度可变的字符串。
使用 string 之前,必须包含相应的头文件,string 属于 std 命名域的,因此需要通过命名限定:
#include <string>
using std::string; //using namespace std;
string对象的定义和初始化
使用示例如下:
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
int main(int argc, char *argv[])
{
string s1 = "mike";
cout << "s1 = " << s1 << endl; //s1 = mike
string s2(s1); //将 s2 初始化为 s1 的一个副本
cout << "s2 = " << s2 << endl; // s2 = mike
string s3("jiang"); //将 s3 初始化为 "mike"
cout << "s3 = " << s3 << endl; // s3 = jiang
string s4(10, 'a'); // s4 的值为 10 个 'a'
cout << "s4 = " << s4 << endl; //s1 = mike
return 0;
}
string 对象常用操作
示例代码如下:
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
int main(int argc, char *argv[])
{
string s1 = "mike";
// 如果s1为空串,则返回true,否则返回false
if( true == s1.empty() ){
cout << "s1 is empty\n";
}else{
cout << "s1 is no empty\n";
}
// 字符串的大小, s1.size()等价于s1.length()
cout << "len = " << s1.size() << endl;
//返回 s1 最大容量
cout << "max_size = " << s1.max_size() << endl;
//取出每一个字符
for(int i = 0; i < s1.size(); i++){
//s1[i]等价于s1.at(i)
cout << i << " = " << s1[i] << endl;
}
string s2 = " jiang";
cout << "s1 + s2 = " << s1+s2 << endl;
// c++ string 和 C char * 相互转换
// string to const char *
const char *str = s1.c_str();
cout << "c_str = " << str << endl;
const char *data = s2.data();
cout << "data = " << data << endl;
// char * to string
const char *buf = "hello, c++";
//string my_str = string(buf);
string my_str = buf;
cout << "my_str = " << my_str << endl;
//s1和s2字符串交换
s1 = "mike";
s2 = "jiang";
s1.swap(s2);
cout << "s1 = " << s1 << ", s2 = " << s2 << endl;
s1.clear();
s1 = "mike";
cout << "s1 = " << s1 << endl;
s1 = "mikejiang";
//取出 s1 从 2 开始后的 4 个字符,位置从0开始
s2 = s1.substr(2, 4);
cout << "s2 = " << s2 << endl;
s1 = "mikejiang";
//删除 s1 从 2 开始后的 4 个字符,位置从0开始
s2 = s1.erase(2, 4);
cout << "s2 = " << s2 << endl;
s1 = "mike";
//s1 从 4 开始(位置从0开始),插入"jiang"字符串
s2 = s1.insert(4, "jiang");
cout << "s2 = " << s2 << endl;
s1="mikejiang";
//删除s1从4开始的5个字符,然后在5位置处插入"tyson"
s2 = s1.replace(4, 5, "tyson");
cout << "s2 = " << s2 << endl;
s1="mikejiang";
//查找字符串"j"在s1中的位置(位置从0开始)
int n = s1.find("j");
cout << "n = " << n << endl;
return 0;
}
迭代器操作 string 对象
string 类提供了向前和向后遍历的迭代器 iterator,迭代器提供了访问各个字符的语法,类似于指针操作,迭代器不检查范围。用 string::iterator 或 string::const_iterator 声明迭代器变量,const_iterator 不允许改变迭代的内容。
示例代码如下:
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
int main(int argc, char *argv[])
{
string s="mike"; //初始化为"mike"
//通过迭代器把所有字符元素的值打印出来
string::const_iterator t;
for( t=s.begin(); t!=s.end(); t++){
cout<< *t << ", ";
}
cout << endl;
string::iterator it;
//通过迭代器给所有字符元素赋值为'a'
for( it=s.begin(); it!=s.end(); it++){
*it = 'a';
}
cout << endl;
//通过迭代器把所有元素的值打印出来
for( it=s.begin(); it!=s.end(); it++){
cout<< *it << ", ";
}
cout << endl;
s = "mikejiang";
it = s.begin(); //返回指string最开始位置元素的指针(迭代器)
//删除指针it+1指向位置的元素,返回指向下一个元素位置的指针(迭代器)
s.erase(it+1); // remove "i"
cout << endl << "after erase:\n";
//通过迭代器把所有元素的值打印出来
for( it=s.begin(); it!=s.end(); it++){
cout<< *it << ", ";
}
cout << endl;
s = "mmmmmm";
it = s.begin();
//在位置it后插入3个'a'
s.insert(it, 3, 'a');
cout << endl << "after insert:\n";
//通过迭代器把所有元素的值打印出来
for( it=s.begin(); it!=s.end(); it++){
cout<< *it << ", ";
}
cout << endl;
return 0;
}
运行结果如下:
string 类型还支持大多数顺序容器操作。在某些方面,可将 string 类型视为字符容器。除了一些特殊操作,string 类型提供与 vector 容器相同的操作。string 类型与 vector 容器不同的是,它不支持以栈方式操纵容器: 在 string 类型中不能使用 front、 back 和 pop_back 操作。
本教程示例代码下载请点此链接:http://download.csdn.net/detail/tennysonsky/8896441