本质;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
}

c++STL容器之string容器的更多相关文章

  1. [知识点]C++中STL容器之map

    UPDATE(20190416):写完vector和set之后,发现不少内容全部引导到map上了……于是进行了一定的描述补充与更正. 零.STL目录 1.容器之map 2.容器之vector 3.容器 ...

  2. [知识点]C++中STL容器之vector

    零.STL目录 1.容器之map 2.容器之vector 3.容器之set 一.前言 关于STL和STL容器的概念参见STL系列第一篇——map(见上).今天介绍第二个成员——vector. 二.用途 ...

  3. [知识点]C++中STL容器之set

    零.STL目录 1.容器之map 2.容器之vector 3.容器之set 一.前言 继上期的vector之后,我们又迎来了另一个类数组的STL容器——set. 二.用途与特性 set,顾名思义,集合 ...

  4. C++STL模板库序列容器之List容器

    目录 一丶List容器的存储结构 二丶丶STL中list容器的使用. 一丶List容器的存储结构 list容器底层是链表结构来维护的.跟vector不一样. vector是数组维护的.拥有连续内存.所 ...

  5. STL容器之map

    [1]map容器 map 是关联容器.容器中的每一个元素都是由一个键值和一个数据值组成的. set 是一个集合它以其元素作为键值(同一个键值只能出现一次),且默认以升序排列. list 是一个顺序容器 ...

  6. STL容器之vector

    [1]模板类vector 模板类vector可理解为广义数组.广义数组,即与类型无关的数组,具有与数组相同的所有操作. 那么,你或许要问:既然C++语言本身已提供了一个序列式容器array,为什么还要 ...

  7. STL容器之Array[转]

    转自https://blog.csdn.net/sin_geek/article/details/51067874 作者 Sin_Geek 简介 array在头文件<array> 中定义 ...

  8. C++ STL容器之 stack

    STL 中的 stack 是一种容器适配器,而不是一种容器. 它是容器适配器是指,只要支持一系列方法的容器(empty, size, back, push_back, pop_back),都能作为st ...

  9. STL容器之set

    [1]set容器 一个集合(set)是一个容器,它其中所包含的元素的值是唯一的. [2]set容器方法 (1)set构造函数.插入函数.遍历过程 应用示例代码如下: #include <set& ...

随机推荐

  1. 【UE4】GAMES101 图形学作业5:光线与物体相交(球、三角面)

    总览 在这部分的课程中,我们将专注于使用光线追踪来渲染图像.在光线追踪中最重要的操作之一就是找到光线与物体的交点.一旦找到光线与物体的交点,就可以执行着色并返回像素颜色. 在这次作业中,我们要实现两个 ...

  2. 【数据结构与算法Python版学习笔记】树——树的遍历 Tree Traversals

    遍历方式 前序遍历 在前序遍历中,先访问根节点,然后递归地前序遍历左子树,最后递归地前序遍历右子树. 中序遍历 在中序遍历中,先递归地中序遍历左子树,然后访问根节点,最后递归地中序遍历右子树. 后序遍 ...

  3. py3.8安装

    ubantu python3.8# 命令下载wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tar.xz#解压tar -xvJf P ...

  4. LiveVideoStackCon2021 北京站专访:从上云到创新,视频云的新技术、新场景

    伴随着视频技术的进步和标准的迭代,视频产业从模拟进入到数字时代,完成了从电影电视到互联网的媒介转换,并且衍生出了超高清.3D.AR/VR 等多种创新形态.特别是在后疫情的当下,我们可以看到音视频技术领 ...

  5. 高斯消元de小板几

    感觉就是模拟解方程,还比手动解方程笨一些.... 但是大数据的话,他毕竟比我解得快多了.... 1 inline int Gauss(int n){ 2 int cnt=1;//真实到达的行列式行数 ...

  6. 计算机网络之网络层移动IP

    文章转自:https://blog.csdn.net/weixin_43914604/article/details/105319753 学习课程:<2019王道考研计算机网络> 学习目的 ...

  7. Luogu P1118 [USACO06FEB]数字三角形 Backward Digit Sums | 搜索、数学

    题目链接 思路:设一开始的n个数为a1.a2.a3...an,一步一步合并就可以用a1..an表示出最后剩下来的数,不难发现其中a1..an的系数恰好就是第n层杨辉三角中的数.所以我们可以先处理出第n ...

  8. testNG 注解使用说明

    1.TestNG常用注解 @BeforeSuite 标记的方法:在某个测试套件(suite)开始之前运行 @BeforeTest 在某个测试(test)开始之前运行 @BeforeClass 在某个测 ...

  9. 攻防世界 WEB 高手进阶区 XCTF 4th-CyberEarth ics-06 Writeup

    攻防世界 WEB 高手进阶区 XCTF 4th-CyberEarth ics-06 Writeup 题目介绍 题目考点 掌握暴力破解手段 Writeup 打开链接 http://220.249.52. ...

  10. mysql-5.7部署总从同步

    主从部署方案: https://blog.csdn.net/mrbuffoon/article/details/103082295 主从数据库不同步处理方案: https://blog.csdn.ne ...