c++(九)string类用法STL

一、定义

	string s();								//构造一个空字符串
	string s(const char* s);				//复制s所指向的字符序列
	string s(const char* s, size_t n);		//复制s所指向的字符序列的前n个字符
	string s(size_t, char c);				//生成n个c字符的字符串
	string s(const string & str);			//生成str的复制

    //复制str中字符位置pos开始并且跨越len个字符的部分
	string s(const string & str, size_t pos, size_t len = npos);	

二、插入

void push_back (char c); //字符串尾部插入一个单字符

string& insert (size_t pos, const string& str);    //在pos位置上插入string对象
string& insert (size_t pos, const char* s);         //在pos位置上插入字符串
iterator insert (iterator p, char c);                      //在迭代器p的位置上插入字符c
 

三、拼接

string& append (const string& str);        //在字符串尾部追加字符串str
string& append (const char* s);             //在字符串尾部追加C风格的字符串s
string& append (size_t n, char c);          //将字符c重复追加n次到字符串对象的尾部
 

四、删除

void pop_back();    //移除最后一个字符,一次一个,字符串是非空的

string& erase (size_t pos = 0, size_t len = npos);              //删除pos位置开始的n个字符
iterator erase (iterator p);                                                   //删除pos位置的字符
iterator erase (iterator first, iterator last);                            //删除[pos1,pos2)上所有字符
 

五、查询

size_t find (const string& str, size_t pos = 0) const;     //查找指定字符串str出现的下标,默认从0

开始,可以指定开始查询的下标pos。

size_t find (const char* s, size_t pos = 0) const;      //查找指定C风格字符串s出现的下标,默认从0开始,可以指定开始查询的下标pos。

size_t find (char c, size_t pos = 0) const;                //查找字符c出现的下标,默认从0开始(可选)

    string two("All's well that ends well");
    char *s = "that";
    string three = "well";

    int index = two.find(three,0);
    int index1 = two.find(s,0);
    int index2 = two.find('w',0);

    cout << index << endl;//6
    cout << index1 << endl;//11
    cout << index2 << endl;//6

size_t rfind (const string& str, size_t pos = npos) const;

size_t rfind (const char* s, size_t pos = npos) const;

size_t rfind (char c, size_t pos = npos) const; 

以上三个函数,是方向查找,唯一的区别是查找位置是从末尾开始的。

六、比较

int compare (const string& str) const;
//比较调用函数的字符串对象和参数字符串对象 str 的大小关系。

int compare (size_t pos, size_t len, const string& str) const;
//比较调用函数的字符串对象从指定位置开始的子串和参数字符串对象 str 的大小关系。

int compare (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const;
//比较调用函数的字符串对象从指定位置开始的子串(长度为 len)和参数字符串对象
 str 的子串(从 subpos 开始,长度为 sublen)的大小关系。
 

七、替换

string& replace (size_t pos, size_t len, const char* s);//从pos开始数len个,替换为s
string& replace (size_t pos, size_t len, size_t n, char c);//从pos开始数len个,替换为n个字符c
 

    string one("Mike Lottery Winner!");
    string two("All's well that ends well");
    one.replace(0, 4, "Hello");
    two.replace(6, 4, 4, '$');
    cout << one << endl;//Hello Lottery Winner!
    cout << two << endl;//All's $$$$ that ends well

void swap (string& x, string& y);       //交换x,y
void swap (string& str);                     //交换x,y

    string one("Mike");
    string two("Amy");

    one.swap(two);
    cout << one << " and "<< two << endl;//Amy and Mike

    swap(one, two);
    cout << one << " and "<< two << endl;//Mike and Amy
    //一共交换了两次了属于

八、截取和拷贝

string substr (size_t pos = 0, size_t len = npos) const;
size_t copy (char* s, size_t len, size_t pos = 0) const;
 

九、C字符和string转换

    string one("Mike");
    string two("Amy");
    char str[] = "Tom";
    //将字符串转换为string
    string three(str);

    //将string转换为字符串
    cout << one.c_str() << " and " << two.data() << endl;//Mike and Amy
    cout << three << endl;//Tom

十、字符串其他用法

    at访问字符串

  char& at (size_t pos);
    const char& at (size_t pos) const;

    []访问字符串
    string[index];

    for迭代字符串

    string one("Mike");
    for (int i = 0; i < one.length(); ++i) {
        cout << one[i] << " in " << one.find(one[i]) << " is:" << one.at(i) << endl;
    }
    for(auto& e : one){
        e = 'x';
    }
    cout << one;//xxxx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值