C++ STL介绍——String类
目录
1、简介要使用 string 类,必须包含头文件 <string>。string 库提供了许多其他功能,如删除字符串的部分或全部,用一个字符的部分或全部替换另一个字符串的部分或全部,插入、删除字符串中数据,比较、提取、复制、交换等。 2、string类成员函数汇总
3、String类的构造函数以及析构函数常见的 string 类构造函数有以下几种形式: string strs ;//生成空字符串 string s(str);//生成字符串str的复制品 string s(str,stridx) ; //将字符串str中始于stridx的部分作为构造函数的初值 string s(str,strbegin,strlen); //将字符串str中始于strbegin、长度为strlen的部分作为字符串初值 string s(cstr);//以C_string类型cstr作为字符串s的初值 string s(cstr,char_len);//以C_string类型cstr的前char_len个字符串作为字符串s的初值 string s(num,c);//生成一个字符串,包含num个c字符 string s(strs,beg,end) ;//以区间[beg,end]内的字符作为字符串s的初值 析构函数如下: ~string() ; //销毁所有内存,释放内存 例1 string的构造 #include <iostream> #include <string> using namespace std; int main () { string str ("12345678"); char ch[ ] = "abcdefgh"; string a; //定义一个空字符串 string str1 (str); //构造函数,全部复制 string str2 (str,2,5); //构造函数,从字符串str的第2个元素开始,复制5个元素,赋值给str_2 string str3 (ch,5); //将字符串ch的前5个元素赋值给str_3 string str4 (5,'X'); //将 5 个 'X' 组成的字符串 "XXXXX" 赋值给 str_4 string str5 (str.begin(),str.end()); //复制字符串 str 的所有元素,并赋值给 str_5 cout << str << endl; cout << a << endl ; cout << str1 << endl; cout << str2 << endl; cout << str3 << endl; cout << str4 << endl; cout << str5 << endl; return 0; } 程序运行结果: 12345678 12345678 34567 abcde XXXXX 12345678 4、获取字符串长度String 类型对象包括三种求解字符串长度的函数:
例2 string获取长度 #include <iostream> #include <string> using namespace std; int main () { int size = 0; int length = 0; unsigned long maxsize = 0; int capacity=0; string str ("12345678"); string str_custom; str_custom = str; str_custom.resize (5); size = str_custom.size(); length = str_custom.length(); maxsize = str_custom.max_size(); capacity = str_custom.capacity(); cout << "size = " << size << endl; cout << "length = " << length << endl; cout << "maxsize = " << maxsize << endl; cout << "capacity = " << capacity << endl; return 0; } 程序运行结果: size = 8 length = 8 maxsize = 2147483647 capacity = 15 5、获取字符串元素????字符串中元素的访问是允许的,一般可使用两种方法访问字符串中的单一字符: 需要注意的是,这两种访问方法是有区别的:
例3 string获取字符串元素 #include <iostream> #include <string> using namespace std; int main() { string s ("abcde"); char temp =0; char temp_1 = 0; temp = s [2]; //"获取字符 'c' temp_1 = s.at(2); //获取字符 'c' cout << temp << " " << temp_1 << std::endl; return 0; } 程序运行结果: c c 6、字符串比较方法????string 类模板既提供了 1、compare()函数函数原型: int compare (const basic_string& s) const; int compare (const Ch* p) const; int compare (size_type pos,size_type n,const basic_string& s) const; int compare (size_type pos,const basic_string& s,size_type pos2,size_type n2) const; int compare (size_type pos,const Ch* p,size_type = npos) const; 例4 string比较之compare #include <iostream> #include <string> using namespace std; int main () { string A ("aBcdef"); string B ("AbcdEf"); string C ("123456"); string D ("123dfg"); //下面是各种比较方法 int m=A.compare (B); //完整的A和B的比较 int n=A.compare(1,5,B,4,2); //"Bcdef"和"AbcdEf"比较 int p=A.compare(1,2); //"Bcdef"和"Ef"比较 int q=C.compare(0,3,D,3); //"123"和"123"比较 cout << "m = " << m << ",n = " << n <<",p = " << p << ",q = " << q << endl; cin.get(); return 0; } 程序运行结果: m = 1,n = -1,p = -1,q = 0 由此可知,string 类的比较 compare() 函数使用非常方便,而且能区分字母的大小写。 2、比较运算符String 类的常见运算符包括 >、<、==、>=、<=、!=。 例5 String比较之比较运算符 #include <iostream> #include <string> using namespace std; void TrueOrFalse (int x) { cout << (x?"True":"False")<<endl; } int main () { string S1 = "DEF"; string CP1 = "ABC"; string CP2 = "DEF"; string CP3 = "DEFG"; string CP4 ="def"; cout << "S1= " << S1 << endl; cout << "CP1 = " << CP1 <<endl; cout << "CP2 = " << CP2 <<endl; cout << "CP3 = " << CP3 <<endl; cout << "CP4 = " << CP4 <<endl; cout << "S1 <= CP1 returned "; TrueOrFalse (S1 <=CP1); cout << "S1 <= CP2 returned "; TrueOrFalse (S1 <= CP2); cout << "S1 <= CP3 returned "; TrueOrFalse (S1 <= CP3); cout << "CP1 <= S1 returned "; TrueOrFalse (CP1 <= S1); cout << "CP2 <= S1 returned "; TrueOrFalse (CP2 <= S1); cout << "CP4 <= S1 returned "; TrueOrFalse (CP4 <= S1); cin.get(); return 0; } 程序运行结果: S1= DEF CP1 = ABC CP2 = DEF CP3 = DEFG CP4 = def S1 <= CP1 returned False S1 <= CP2 returned True S1 <= CP3 returned True CP1 <= S1 returned True CP2 <= S1 returned True CP4 <= S1 returned False 在使用时比较运算符时,对于参加比较的两个字符串,任一个字符串均不能为 7、字符串输入输出????字符串的输入输出直接用 #include <iostream> #include <string> using namespace std; void main () { string s1,s2; getline(cin,s1); getline(cin,s2,' '); cout << "You inputed chars are: " << s1 << endl; cout << "You inputed chars are: " << s2 << endl; } 程序运行结果: 123456 asdfgh klj You inputed chars are: 123456 You inputed chars are: asdfgh 8、字符串查找函数在 STL 中,字符串的查找功能可以实现多种功能,比如说:
????若查找 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |