STL之string

来源:互联网 发布:camera软件下载 编辑:程序博客网 时间:2024/06/06 08:48

一、string类型的初始化

MSDN上列出的所有String的构造函数:
这里写图片描述

感觉这里应该少了一种String(int32, char)这个构造函数,例子中会给出的:

#include<iostream>#include<string>using namespace std;int main() {    char a[11] = "helloworld";    char *p = a;        string s1("abc");    //String(Char*)    string s2 = "abc";    //相当于char* 转string类型    string s3(10, 'a');   //String(int32, char)    string s5(a);         //String(Char[])    string s6(a, 0, 5);    //String(Char[], Int32, Int32)    string s7(p, 5, 5);    //String(Char*, Int32, Int32)    cout << "s1 : " << s1 << endl;    cout << "s2 : " << s2 << endl;    cout << "s3 : " << s3 << endl;    cout << "s5 : " << s5 << endl;    cout << "s6 : " << s6 << endl;    cout << "s7 : " << s7 << endl;    return 0;}

输出结果:

这里写图片描述

上面的例子中没有给出String(Char, Int32)的示例,这个函数的解释是这样的:

Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times.

我试验了几个例子,还是没太搞懂,搞懂了再添加吧。

二、string的遍历

1、

#include<iostream>#include<string>using namespace std;int main() {    string s("hello world");    for (int i = 0; i < s.length(); i++)        cout << s[i];    cout << endl;    return 0;}

2、

#include<iostream>#include<string>using namespace std;int main() {    string s("hello world");    for (string::iterator it = s.begin(); it != s.end(); it++)        cout << *it;    cout << endl;    return 0;}

3、

#include<iostream>#include<string>using namespace std;int main() {    string s("hello world");    for (int i = 0; i < s.length(); i++)        cout << s.at(i);    cout << endl;    while (1) {}    return 0;}

上面三种方式的输出结果都是:

这里写图片描述

这里重点说一下s[i]与s.at(i)的区别s.at(i)可以抛出异常,而s[i]不可以。

#include<iostream>#include<string>using namespace std;int main() {    string s("hello world");    try {        for (int i = 0; i < s.length() + 3; i++)            cout << s.at(i);        cout << endl;    }    catch (...) {        cout << "catch exception" << endl;    }    cout << endl;    try {        for (int i = 0; i < s.length() + 3; i++)            cout << s[i];    }    catch (...) {        cout << "catch exception" << endl;    }    while (1) {}    return 0;}

输出结果:

这里写图片描述

代码中估计在遍历时超出string本身的长度,可以看到使用s.at(i)遍历string时,成功捕获了异常,而在s[i]遍历时,弹出了中断:

这里写图片描述

三、string与char*的转换

#include<iostream>#include<string>using namespace std;int main() {    string s1;    s1 = "hello world";    cout << typeid(s1.c_str()).name() << endl;    return 0;}

char* 可以直接隐式转换成string类型,string需要借助成员函数c_str()转换成char*类型。这里用typeid查看是否转成了char*类型。

输出结果:

这里写图片描述

四、string拷贝到char*的内存中

#define _SCL_SECURE_NO_WARNINGS#include<iostream>#include<string>using namespace std;int main() {    string s = "hello world";    char buf[128];    s.copy(buf, 5, 0);    cout << buf << endl;    while (1) {}    return 0;}
s.copy(buf, 5, 0);

这里把s从0开始的5个字符拷贝到buf中。

输出结果:

这里写图片描述

可以看到copy这个函数只拷贝一定数量的字符,并不会加‘\0’结束符,所以后面出现了一堆烫。

五、string的赋值

string &operator=(const string &s);//把字符串s赋给当前字符串string &assign(const char *s);//用c类型字符串s赋值string &assign(const char *s,int n);//用c字符串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 &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串

举几个例子:

#define D_SCL_SECURE_NO_WARNINGS#include<iostream>#include<string>using namespace std;int main() {    string s1;    s1.assign(10, 'c');    cout << s1 << endl;    s1.assign("hello", 3);    cout << s1 << endl;    s1.assign("i like c", 0, 5);    cout << s1 << endl;    string s2 = "i like game";    s1.assign(s2.begin(), s2.end());    cout << s1 << endl;    while (1) {}    return 0;}

输出结果:

这里写图片描述

六、string的连接

string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾 string &append(const char *s); //把c类型字符串s连接到当前字符串结尾string &append(const char *s,int n);//把c类型字符串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个字符cstring &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾

这里就举几个例子,简单的就不举例了:

#define D_SCL_SECURE_NO_WARNINGS#include<iostream>#include<string>using namespace std;int main() {    string s1 = "aaa";    s1.append("bcd", 0, 2);    cout << s1 << endl;    s1 = "aaa";    s1.append("hello world", 5);    cout << s1 << endl;    s1 = "aaa";    s1.append(10, 'b');    cout << s1 << endl;    s1 = "aaa";    string s2 = "bcd";    s1.append(s2.begin(), s2.end());    cout << s1 << endl;    while (1) {}    return 0;}

输出结果:

这里写图片描述

七、string的查找

int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置

find函数查找到字符或字符串在字符串中的位置,则返回位置下标,否则返回-1;

#define D_SCL_SECURE_NO_WARNINGS#include<iostream>#include<string>using namespace std;int main() {    string s1 = "wbm 123 wbm 456 wbm 789 wbm abc";    int offidex = s1.find("wbm", 0);    int idex = 1;    while (offidex != string::npos) {        cout << "第" << idex++ << "个wbm出现在" << offidex << "位置" << endl;        offidex++;        offidex = s1.find("wbm", offidex);    }    while (1) {}    return 0;}

输出结果:

这里写图片描述

八、string的字符替换

1、用string 或C-string 代替操作string 中从 _Pos1 开始的 _Num1 个字符

basic _ string& replace( size _ type _Pos1 ,size _ type _Num1 , const value _ type* _Ptr ); basic _ string& replace(size _ type _Pos1 ,size _ type _Num1 ,const basic _ string _Str );
string a,b; string s ( "AAAAAAAA" ); string s1p ( "BBB" ); const char* cs1p = "CCC"; a = s.replace ( 1 , 3 , s1p ); // s= "ABBBAAAA" b = s.replace ( 5 , 3 , cs1p ); // s= "ABBBACCC" 

2、从string 中 _Pos2 开始的 _Num2 个字符,代替操作string 中从 _Pos1 开始的 _Num1 个字符
用C-string 中0(默认位置)开始的 _Num2 个字符,代替操作string 中从 _Pos1 开始的 _Num1 个字符

basic _ string& replace( size _ type _Pos1 , size _ type _Num1 , const basic _ string& _Str , size _ type _Pos2 , size _ type ); basic _ string& replace( size _ type _Pos1 , size _ type _Num1 , const value _ type* _Ptr , size _ type _Num2 );
string a, b; string s ( "AAAAAAAA" ); string s2p ( "BBB" ); const char* cs2p = "CCC"; a = s.replace ( 1 , 3 , s2p , 1 , 2 ); // s= "ABBAAAA" b = s.replace ( 4 , 3 , cs2p , 1 ); // s= "ABBAC" 

3、用 _Count 个character _Ch , 代替操作string 中从 _Pos1 开始的 _Num1 个字符

basic _ string& replace( size _ type _Pos1 , size _ type _Num1 , size _ type _Count , value _ type _Ch );
string result; string s ( "AAAAAAAA" ); char ch = 'C'; result = s.replace ( 1 , 3 , 4 , ch ); //s="ACCCCAAAA"

4、用string 或C-string ,代替操作string 中从 First0 到 Last0 的字符

basic _ string&replace(iterator First0 ,iterator Last0, const basic _ string& _Str ); basic _ string&replace(iterator First0,iterator_Last0, const value _ type* _Ptr );
string s("AAAAAAAA"); string s4p("BBB");    const char* cs4p = "CCC";    string a, b;    a = s.replace(s.begin(), s.begin() + 3, s4p); //s= "BBBAAAAA"     b = s.replace(s.begin(), s.begin() + 3, cs4p); //s= "CCCAAAAA" 

5、用string 中从 _Pos2 开始的 _Num2 个字符,代替操作string 中从 First0 到 Last0 的字符

用C-string 中的 _Num2 个字符,代替操作string 中从 First0 到 Last0 的字符

basic _ string& replace( iterator _First0 , iterator _Last0 , const value _ type* _Ptr , size _ type _Num2 ); template<class InputIterator> basic _ string& replace( iterator _First0 , iterator _Last0 , InputIterator _First , InputIterator _Last ); 
string s("AAAAAAAA"); string s4p("BBB");    const char* cs4p = "CCC";    string a, b;    a = s.replace(s.begin(), s.begin() + 4, s4p.begin(), s4p.end()); // s= "BBBAAAA"     b = s.replace(s.begin(), s.begin() + 3, cs4p, 2); //s = "CCAAAA"

注意这里s的长度变了,因为我们用短字符串替换了长字符串

6、用 _Count 个character _Ch , 代替操作string 中从 First0 到 Last0 的字符

basic _ string& replace( iterator _First0 , iterator _Last0 , size _ type _Count , value _ type _Ch ); 
string s("AAAAAAAA");    string a;    a = s.replace(s.begin(), s.begin() + 4, 3, 'a'); // s= "aaaAAAA" 

这里跟上面情况一样,用短字符串替换了长字符串。

最后举个遍历替换的例子:

#define D_SCL_SECURE_NO_WARNINGS#include<iostream>#include<string>using namespace std;int main() {    string s1 = "wbm 123 wbm 456 wbm 789 wbm abc";    int offidex = s1.find("wbm", 0);    int idex = 1;    while (offidex != string::npos) {        s1.replace(offidex, 3, "WBM");        offidex++;        offidex = s1.find("wbm", offidex);    }    cout << s1 << endl;    while (1) {}    return 0;}

输出结果:

这里写图片描述

九、string插入与删除操作

1、插入

string& insert(int pos = 0, const char* s);  //在pos位置插入字符串sstring& insert(int pos = 0; const string& s);  //在pos位置插入字符串sstring& insert(int pos, int n, char c); //在pos位置插入n个字符c
string s = "hello world";s.insert(0, "AAA");cout << s << endl;s = "hello world";s.insert(0, 10, 'a');cout << s << endl;

输出结果:

这里写图片描述

2、删除

string& erase(int pos = 0, int n = npos);  //在pos位置删除n个字符string& erase(string::iterator it);   //删除迭代器it所指的位置
string s = "hello world";    string::iterator it = s.begin();    while (it != s.end()) {        if (*it == 'l')            it = s.erase(it);        else            it++;    }    cout << s << endl;    s = "hello world";    s.erase(0, 3);    cout << s << endl;

输出结果:

这里写图片描述

十、string的算法

string s = "hello WORLD";string::iterator it = s.end();it--;transform(s.begin(), s.end(), s.begin(), toupper);cout << s << endl;s = "hello WORLD";transform(s.begin(), s.end(), s.begin(), tolower);cout << s << endl;

transform是字符转换大小写的算法:

transform(s.begin(), s.end(), s.begin(), toupper);

讲s从头至尾的全部字符转成大写,并放到s的开头。

transform(s.begin(), s.end(), s.begin(), tolower);

讲s从头至尾的全部字符转成小写,并放到s的开头。

输出结果:

这里写图片描述

0 0
原创粉丝点击