c++STL容器之string容器

时间:2022-07-24
本文章向大家介绍c++STL容器之string容器,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

本质;string是c++风格的字符串,而string本质上是一个类

string和char*的区别:

  • char*是一个指针;
  • string是一个类,类内部封装了char*,管理这个字符串,是一个char*的容器;

特点:

string内部封装了很多内部成员方法,例如find、copy、delete、replace、insert等。

string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行操作。

一、string构造函数

  • string():创建一个空的字符串
  • string(const char* s):使用字符串s初始化
  • string(const string& str):使用一个string对象初始化另一个string对象
  • string(int n,char c):使用n个字符c初始化
#include<iostream>
#include<string>
using namespace std;

void test() {
    string s1;//默认构造
    const char* str = "hello world";
    string s2(str);
    cout <<"s2="<< s2 << endl;
    string s3(s2);
    cout << "s3=" << s3 << endl;
    string s4(10, 'a');
    cout << "s4=" << s4 << endl;
}

int main() {
    test();
    system("pause");
    return 0;
}

二、string赋值操作

#include<iostream>
#include<string>
using namespace std;

void test() {
    string str1;
    str1 = "hello world";
    cout << "str1=" << str1 << endl;
    string str2;
    str2 = str1;
    string str3;
    str3 = 'c';
    string str4;
    str4.assign("hello woeld");
    string str5;
    str5.assign("hello world", 5);//只赋值前n个字符
    string str6;
    str6.assign(str5);
    string str7;
    str7.assign(10, 'w');//赋值十个w
}

int main() {
    test();
    system("pause");
    return 0;
}

三、字符串拼接

#include<iostream>
#include<string>
using namespace std;

void test() {
    string str1;
    str1 = "我";
    str1 += "爱中国";
    cout << "str1=" << str1 << endl;
    str1 += '!';
    cout << "str1=" << str1 << endl;
    string str2 = "LOL";
    str1 += str2;
    cout << "str1=" << str1 << endl;
    string str3 = "i";
    str3.append(" love you");
    cout << "str3=" << str3 << endl;
    str3.append("new gameing",4);//拼接前n个字符
    str3.append(str2);
    str3.append(str2, 0, 2);//只截取第0,1个字符并拼接
    cout << "str3=" << str3 << endl;
}

int main() {
    test();
    system("pause");
    return 0;
}

三、字符串的查找和替换

#include<iostream>
#include<string>
using namespace std;

void test() {
    //1.查找
    string str1 = "abcdefg";
    //find、rfind只找到第一个出现的位置
    cout << str1.find("bc", 0) << endl;//默认从零位置开始,并返回找到的索引位置,未找到返回-1
    cout << str1.rfind("bc",6) << endl;//rfind是从右往左查找,6是起始索引位置
    //2.替换
    str1.replace(0, 2, "pppp");//将0-1之间的位置替换成"pppp"
    cout << str1 << endl;
}

int main() {
    test();
    system("pause");
    return 0;
}

四、字符串比较

#include<iostream>
#include<string>
using namespace std;

void test() {
    string str1 = "hello";
    string str2 = "hello";
    //逐一比较每个字符的ASCII,若全部相等,返回0,若str1的ASCII大于str2则返回1,否则返回-1
    str1.compare(str2);
}

int main() {
    test();
    system("pause");
    return 0;
}

五、字符串的存取

#include<iostream>
#include<string>
using namespace std;

void test() {
    string str1 = "hello";
    //读
    cout << str1[0] << endl;;
    cout << str1.at(0) << endl;
    //写
    str1[0] = 'm';
    cout << str1 << endl;
    str1.at(0) = 'p';
    cout << str1 << endl;
}

int main() {
    test();
    system("pause");
    return 0;
}

六、字符串的插入与删除

void test() {
    string str1 = "hello";
    str1.insert(1, "big");//在某个位置插入
    cout << str1 << endl;
    str1.erase(1, 3);//删除起始位置和最终位置之间的
    cout << str1 << endl;
}

七、子串获取

void test() {
    string str1 = "hello";
    cout << str1.substr(1, 3) << endl;//返回1-3之间的子串,包含下标1,2,3
}