- 标准库类型string表示可变长的字符序列,为了在程序中使用string类型,我们必须包含头文件: #include <string>
- 声明一个字符串
- 声明一个字符串有很多种方式,具体如下:
1 string s;//调用默认构造函数,s为一个空字符串 2 string s(str);//等价于string s = str;调用拷贝构造函数,s是str的备份 3 string s(str,strindex);//将字符串str内始于strindex位置的部分当作s的初始值 4 eg.string str = "123456789"; 5 string s(str,3);//s的初值为str由位置3开始的字符串,即456789 6 string s(str,stridx,strlen); // 将字符串str由stridx位置起始且长度为strlen的部分最为s的初值,如果strlen大于最大长度,则只截取字符串最大长度 7 eg.string s(str,3,10);//s=456789,由位置3开始,截取长度为10的部分,由于str剩余部分长度小于10,则截取str剩余最大长度 8 string s(cstr);//将C风格字符串作为s的初值 9 eg.string s("hello");//s的初值为hello 10 string s(cstr,length);//将C风格字符串的length长度部分作为s的初值 11 eg.string s("hello",2);//s="he" 12 string s(num,c);//生成一个字符串,包含num个c字符 13 eg.string s(10,'c');//s的初值为“cccccccccc”
- 字符串操作函数
- c++字符串的操作函数很多,这里把常用的罗列出来
1 =、assign()//用于赋予新值,assign函数用于将一个字符串的部分内容赋值给另一个string对象 2 eg.string s1 = "hello"; 3 string s2; 4 s2.assign(s1,0,3);//s2的值为“hel” 5 6 swap() //交换两个字符串的内容 7 eg.string s1 = "hello"; 8 string s2 = "world"; 9 swap(s1,s2);//swap函数将s1和s2的内容交换,现在s1="world",s2="hello" 10 11 +=、append()、push_back()//在字符串尾部追加内容,"+="可追加string对象,字符以及C风格字符串,append函数则可以追加string对象和C风格字符串,push_back函数则只能追加字符 12 eg.string s1 = "hello"; 13 string s2 = " world"; 14 s1 += s2;//正确,s1的值为”hello world“ 15 s1 +="world";// 正确,s1的值为"hello world" 16 s1 +='c'; //正确,s1的值为"helloc" 17 18 s1.append(s2);//正确,s1的值为"hello world" 19 s1.append(" world");//正确,s1的值为"hello world" 20 s1.append('c');//错误,append函数不支持追加字符 21 22 s1.push_back(s2);//错误 23 s1.push_back("world");//错误 24 s1.push_back('c');//正确 25 26 27 insert()//用于插入字符串 28 eg.string s1 = "hello"; 29 s1.insert(0,"world ");//s1的值为world hello 30 31 erase()//用于删除字符的 32 eg.string str("This is an example phrase."); 33 string::iterator it;//迭代器 34 35 str.erase(10,8);//str的值为"This is an phrase.",删除了从位置10开始的8个字符 36 37 it = str.begin()+9;//迭代器位置为9 38 str.erase(it);//删除了从it迭代器位置处的一个字符,str="This is a phrase." 39 40 str.erase(str.begin()+5,str.end()-7);//删除两个参数之间的所有字符,str="This phrase." 41 42 43 clear()函数和~string()//都是用来删除全部字符的 44 eg.str.clear();//删除str的全部字符,此时str为一个空串 45 str.~string();//销毁所有字符,释放内存 46 47 replace()函数,用于替换字符 48 eg.1.string line = "this@ is@ a test string!"; 49 line = line.replace(line.find("@"),1,"");//将line中从find的@位置开始替换一个长度的字符为"" 结果为this is@ a test string! 50 51 ==、!=、<、<=、>、>=、compare()//比较字符串 52 eg.string s1 = "haha"; 53 string s2 = "haha"; 54 if(s1.compare(s2) == 0){ 55 cout << "相等" << endl; 56 } 57 58 size()函数和length()函数,返回字符串的字符数 59 eg.string str = "haha"; 60 str.size() 等于 str.length(),值均为4 61 62 empty()//判断字符串是否为空 63 64 下标法str[index]或者str.at(index)获取字符串内指定位置的字符 65 66 data()函数,将内容以字符数组的形式返回
- C++字符串和C字符串的转换
- C++提供的由C++字符串得到对应的C_string的方法是使用data()、c_str()和copy(),其中,data()以字符数组的形式返回字符串内容,但并不添加'\0'.
- c_str()返回一个以'\0'结尾的字符数组
- copy()则把字符串的内容复制或写入已有的c_string或字符数组内
- 元素存取
- 我们可以使用下标操作符[]和函数at()来对字符串的字符进行访问,但是应该注意的是下标操作符并不会检查索引是否有效,如果索引失效,会引起未定义的行为
- at()函数则会检查索引,如果索引失效会抛出out_of_range异常
- 注意,操作符可取到字符串尾部的'\0'字符
已知类string的原型为:
1 class String 2 { 3 public: 4 String(const char *str = NULL);//普通构造函数 5 String(const String &other);//拷贝构造函数 6 ~String(void);//析构函数 7 8 private: 9 char *m_data;//用于保存字符串 10 };
编写上述三个函数的实现:
1 //普通构造函数 2 String:String(const char *str) 3 { 4 if(str == NULL){ 5 m_data = new char[1]; 6 *m_data = '\0'; 7 }else{ 8 int length = strlen(str); 9 m_data = new char[length+1]; 10 strcpy(m_data,str); 11 } 12 } 13 14 //析构函数 15 String::~String(void) 16 { 17 delete []m_data; 18 } 19 20 //拷贝构造函数 21 String::String(const String &other) 22 { 23 int length = strlen(other.m_data); 24 m_data = new char[length+1]; 25 strcpy(m_data,other.m_data); 26 }