STL----string

1. string容器

string容器常用操作

string 构造函数

string();//创建一个空的字符串 例如: string str;      
string(const string& str);//使用一个string对象初始化另一个string对象
string(const char* s);//使用字符串s初始化
string(int n, char c);//使用n个字符c初始化 
【例 1】
#include <iostream>
#include <string>
using namespace std;
int main ()
{
    string str ("12345678");
    char ch[ ] = "abcdefgh";
    string a; //定义一个空字符串
    string str_1 (str); //构造函数,全部复制
    string str_2 (str, 2, 5); //构造函数,从字符串str的第2个元素开始,复制5个元素,赋值给str_2
    string str_3 (ch, 5); //将字符串ch的前5个元素赋值给str_3
    string str_4 (5,'X'); //将 5 个 'X' 组成的字符串 "XXXXX" 赋值给 str_4
    string str_5 (str.begin(), str.end()); //复制字符串 str 的所有元素,并赋值给 str_5
    cout << str << endl;
    cout << a << endl ;
    cout << str_1 << endl;
    cout << str_2 << endl;
    cout << str_3 << endl;
    cout << str_4 << endl;
    cout << str_5 << endl;
    return 0;
}
程序运行结果为:
12345678
12345678
34567
abcde
XXXXX
12345678

 

 


 

string基本赋值操作

string& operator=(const char* s);//char*类型字符串 赋值给当前的字符串
string& operator=(const string &s);//把字符串s赋给当前的字符串
string& operator=(char c);//字符赋值给当前的字符串
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个字符赋值给字符串

 

【例 1】
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str1 ("123456");
    string str;
    str.assign (str1); //直接赋值
    cout << str << endl;
    str.assign (str1, 3, 3); //赋值给子串
    cout << str << endl;
    str.assign (str1,2,str1.npos);//赋值给从位置 2 至末尾的子串
    cout << str << endl;
    str.assign (5,'X'); //重复 5 个'X'字符
    cout << str << endl;
    string::iterator itB;
    string::iterator itE;
    itB = str1.begin ();
    itE = str1.end();
    str.assign (itB, (--itE)); //从第 1 个至倒数第 2 个元素,赋值给字符串 str
    cout << str << endl;
    return 0;
}

 

 

 


 

string存取字符操作

char& operator[](int n);//通过[]方式取字符
char& at(int n);//通过at方法获取字符
例1:
#include <iostream> #include <string> int main() { const std::string cS ("c.biancheng.net"); std::string s ("abode"); char temp =0; char temp_1 = 0; char temp_2 = 0; char temp_3 = 0; char temp_4 = 0; char temp_5 = 0; temp = s [2]; //"获取字符 'c' temp_1 = s.at(2); //获取字符 'c' temp_2 = s [s.length()]; //未定义行为,返回字符'\0',但Visual C++ 2012执行时未报错 temp_3 = cS[cS.length()]; //指向字符 '\0' temp_4 = s.at (s.length ()); //程序异常 temp_5 = cS.at(cS.length ()); //程序异常 std::cout << temp <<temp_1 << temp_2 << temp_3 << temp_4 << temp_5 << std::endl; return 0; }
例2:
#include <iostream>
#include <string>
int main()
{
    string s ("abode");
    cout << s << endl ;
    char& r = s[2] ; //建立引用关系
    char*p=&s[3]; //建立引用关系
    r='X' ;//修改内容
    *p='Y' ;//修改内容
    cout << s << endl; //输出
    s = "12345678"; //重新赋值
    r ='X'; //修改内容
    *p='Y'; //修改内容
    cout << s << endl; //输出
    return 0;
}
程序输出结果为:
abode
abXYe
12XY5678

 

 

 


 

string拼接操作

string& operator+=(const string& str);//重载+=操作符
string& operator+=(const char* str);//重载+=操作符
string& operator+=(const char c);//重载+=操作符
string& append(const char *s);//把字符串s连接到当前字符串结尾
string& append(const char *s, int n);//把字符串s的前n个字符连接到当前字符串结尾
string& append(const string &s);//同operator+=()
string& append(const string &s, int pos, int n);//把字符串s中从pos开始的n个字符连接到当前字符串结尾
string& append(int n, char c);//在当前字符串结尾添加n个字符c

string 的查找和替换

int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找
int find(const char* s, int pos = 0) const;  //查找s第一次出现位置,从pos开始查找
int find(const char* s, int pos, int n) const;  //从pos位置查找s的前n个字符第一次位置
int find(const char c, int pos = 0) const;  //查找字符c第一次出现位置
int rfind(const string& str, int pos = npos) const;//查找str最后一次位置,从pos开始查找
int rfind(const char* s, int pos = npos) const;//查找s最后一次出现位置,从pos开始查找
int rfind(const char* s, int pos, int n) const;//从pos查找s的前n个字符最后一次位置
int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置
string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str
string& replace(int pos, int n, const char* s); //替换从pos开始的n个字符为字符串s

 

例1:
#include <iostream> #include <string> using namespace std; int main () { string var ("abcdefghijklmn"); const string dest ("1234"); string dest2 ("567891234"); var.replace (3,3, dest); cout << "1: " << var << endl; var = "abcdefghijklmn"; var.replace (3,1, dest.c_str(), 1, 3); cout << "2: " << var << endl; var ="abcdefghijklmn"; var.replace (3, 1, 5, 'x'); cout << "3: " << var << endl; string::iterator itA, itB; string::iterator itC, itD; itA = var.begin(); itB = var.end(); var = "abcdefghijklmn"; var.replace (itA, itB, dest); cout << "4: " << var << endl; itA = var.begin (); itB = var.end(); itC = dest2.begin () +1; itD = dest2.end (); var = "abodefghijklmn"; var.replace (itA, itB, itC, itD); cout << "5: " << var << endl; var = "abcdefghijklmn"; var.replace (3, 1, dest.c_str(), 4); //这种方式会限定字符串替换的最大长度 cout <<"6: " << var << endl; return 0; }

 

程序执行结果为:
1: abc1234ghijklmn
2: abc234efghijklmn
3: abcxxxxxefghijklmn
4: 1234
5: 67891234efghijklmn
6: abc1234efghijklmn

 

 

例2:
#include <iostream> #include <string> using namespace std; int main () { string str_ch (" for"); string str (" Hi, Peter, I'm sick. Please bought some drugs for me."); string::size_type m= str.find ('P', 5); string::size_type rm= str.rfind('P', 5); cout << "Example - find() : The position (forward) of 'P' is: " << (int) m << endl; cout << "Example - rfind(): The position (reverse) of 'P' is: " << (int) rm << endl; string::size_type n = str.find (" some", 0); string::size_type rn = str.rfind (" some", 0); cout << "Example - find () : The position (forward) of 'some' is: " << (int) n << endl; cout << "Example - rfind () : The position (reverse) of 'some' is: " << (int) rn << endl; string::size_type mo = str.find (" drugs", 0, 5); string::size_type rmo = str.rfind (" drugs", 0, 5); cout << "Example - find(): The position (forward) of 'drugs' is: " << (int) mo << endl; cout << "Example - rfind(): The position (reverse) of 'drugs' is: " << (int) rmo << endl; string::size_type no = str.find (str_ch, 0); string::size_type rno = str.rfind(str_ch, 0); cout << "Example - find (): The position of 'for' is: " << (int) no << endl; cout << "Example - rfind(): The position of 'for' is: " << (int) rno << endl; cin.get (); }
程序的运行结果为:
Example - find() : The position (forward) of 'P' is: 5
Example - rfind(): The position (reverse) of 'P' is: 5
Example - find () : The position (forward) of 'some' is: 35
Example - rfind () : The position (reverse) of 'some' is: -1
Example - find(): The position (forward) of 'drugs' is: 40
Example - rfind(): The position (reverse) of 'drugs' is: -1
Example - find (): The position of 'for' is: 46
Example - rfind(): The position of 'for' is: -1

 

 

 


 

string比较操作

/*
compare函数在>时返回 1,<时返回 -1,==时返回 0。
比较区分大小写,比较时参考字典顺序,排越前面的越小。
大写的A比小写的a小。
*/
int compare(const string &s) const;//与字符串s比较
int compare(const char *s) const;//与字符串s比较
例子:
#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,5,B,4,2); //"Bcdef"和"Ef"比较 int q=C.compare(0,3,D,0,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
【例 2】
#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

 


 

string子串

string substr(int pos = 0, int n = npos) const;//返回由pos开始的n个字符组成的字符串

 

 


 

string插入和删除操作

string& insert(int pos, const char* s); //插入字符串
string& insert(int pos, const string& str); //插入字符串
string& insert(int pos, int n, char c);//在指定位置插入n个字符c
string& erase(int pos, int n = npos);//删除从Pos开始的n个字符

 

例1:
string
A("ello"); string B ; B.insert(0,A); cout << B << endl; A = "ello"; B = "H"; B.insert (1,"yanchy ",3); cout<< B <<endl; A = "ello"; B = "H"; B.insert (1,A,2,2); cout << B << endl; A="ello"; B.insert (1 , 5 , 'C'); cout << B << endl; A = "ello"; string::iterator it = B.begin () +1; const string:: iterator itF = A.begin(); const string:: iterator itG = A.end(); B.insert(it,itF,itG); cout << B << endl;

 

例2:
A = "ello"; cout << A << endl; cout << B << endl; B.append(A); cout << B << endl; A = "ello"; cout << A << endl; cout << B << endl; B.append("12345",2); cout << B << endl; A = "ello"; cout << A << endl; cout << B << endl; B.append("12345",2,3); cout << B << endl; A = "ello"; B = "H"; cout << A << endl; cout << B << endl; B.append (10, 'a'); cout << B << endl; A = "ello"; B = 'H'; cout << A << endl ; cout << B << endl; B.append(A.begin(), A, end()); cout << B << endl;

 

例3:
#include <iostream> #include <string> using namespace std; int main () { string str1 ("123456"); string str2 ("abcdefghijklmn"); string str; str.assign(str1); cout << str << endl; str.assign (str1 , 3, 3); cout << str << endl; str.assign (str1, 2, str1.npos); cout << str << endl; str.assign (5, 'X'); cout << str << endl; string::iterator itB; string::iterator itE; itB = str1.begin (); itE = str1.end(); str.assign (itB, (--itE)); cout << str << endl; str = str1; cout << str << endl; str.erase(3); cout << str << endl; str.erase (str.begin (), str.end()); cout << ":" << str << ":" << endl; str.swap(str2); cout << str << endl; string A ("ello"); string B ("H"); B.insert (1, A); cout << B << endl; A = "ello"; B ='H'; B.insert (1, "yanchy ", 3); cout << "插入: " << B << endl; A = "ello"; B = "H"; B.insert(1,A,2,2); cout << "插入:" << B << endl; A = "ello"; B = "H"; B.insert (1,5,'C'); cout << "插入:" << B << endl; A = "ello"; B = "H"; string::iterator it = B.begin () +1; const string::iterator itF = A.begin (); const string::iterator itG = A.end (); B.insert(it,itF,itG); cout<<"插入:"<< B << endl; A = "ello"; B = "H"; cout << "A = " << A <<", B = " << B << endl ; B.append (A); cout << "追加:" << B << endl; B = "H"; cout << "A = "<< A << ", B= " << B << endl; B.append("12345", 2); cout << "追加:" << B << endl; A = "ello"; B = "H"; cout << "A = " << A << ", B= " << B << endl; B.append ("12345", 2, 3); cout << "追加:" << B << endl; A = "ello"; B = "H"; cout << "A = " << A <<", B = " << B << endl; B.append (10 , 'a'); cout << "追加:"<< B << endl; A = "ello"; B = "H"; cout << "A = " << A << ", B = " << B << endl; B.append(A.begin() , A.end()); cout << "追加:" << B << endl; cin.get(); return 0; }

 

程序运行结果:
123456
456
3456
XXXXX
12345
123456
123
::
abcdefghijklmn
Hello
插入: Hyan
插入:Hlo
插入:HCCCCC
插入:Hello
A = ello, B = H
追加:Hello
A = ello, B= H
追加:H12
A = ello, B= H
追加:H345
A = ello, B = H
追加:Haaaaaaaaaa
A = ello, B = H
追加:Hello

 

 


 

 

string和c-style字符串转换

//string 转 char*
string str = "it";
const char* cstr = str.c_str();
//char* 转 string 
char* s = "it";
string str(s);

 


 

 

string字符串长度

例1:
#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

 


 

 

字符串输入输出

例1:
#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

 

 


 

string支持迭代器方法

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main ()
{
    string s ("The zip code of Hondelage in Germany is 38108.");
    cout << "Original: " << s << endl;
    string sd(s.begin(),s.end ()); //构造函数中使用迭代器
    cout << "Destination: " << sd << endl;
    transform (sd.begin(), sd.end(), sd.begin(), toupper); //算法中使用迭代器(仿函数)
    cout << "Destination (All Toupper)): " << sd << endl;
    string sd1;
    sd1.append (sd.begin(),(sd.end() -7)); //append()函数中使用迭代器
    cout << "Destination sd1: " << sd1 << endl;
    string sd2;
    string::reverse_iterator iterA;
    string temp = "0";
    for (iterA = sd.rbegin (); iterA != sd.rend (); iterA++) //reverse_iterator
    {
        temp=* iterA;
        sd2.append (temp);
    }
    cout << "Destination sd2: " << sd2 << endl;
    sd2.erase (0, 15); //erase()函数中使用迭代器
    cout << "Destination sd2 (Erased 15 chars) : " << sd2 << endl;
    string::iterator iterB = sd2.begin ();
    string sd3 = string ("12345678");
    sd2.insert (sd2.begin(), sd3.begin(), sd3.end()); //insert()函数中使用迭代器
    cout << "Destination sd2 (Insert 8 chars) : " << sd2 << endl;
    sd2.replace (sd2.begin (), sd2.end(), "This is an Exarrple of Replace"); //Replace
    cout <<"Destination sd2 (Replace All): " << sd2 << endl; // replace ()函数中使用迭代器
}

 

程序运行结果为:
Original: The zip code of Hondelage in Germany is 38108.
Destination: The zip code of Hondelage in Germany is 38108.
Destination (All Toupper)): THE ZIP CODE OF HONDELAGE IN GERMANY IS 38108.
Destination sd1: THE ZIP CODE OF HONDELAGE IN GERMANY IS
Destination sd2: .80183 SI YNAMREG NI EGALEDNOH FO EDOC PIZ EHT
Destination sd2 (Erased 15 chars) : EG NI EGALEDNOH FO EDOC PIZ EHT
Destination sd2 (Insert 8 chars) : 12345678EG NI EGALEDNOH FO EDOC PIZ EHT
Destination sd2 (Replace All): This is an Exarrple of Replace

 

test

https://www.cnblogs.com/isChenJY/p/11536674.html

posted @ 2020-04-10 10:16  多发Paper哈  阅读(96)  评论(0编辑  收藏  举报
Live2D