栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

[C++提高编程笔记] 三.(一).STL常用容器之string容器

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[C++提高编程笔记] 三.(一).STL常用容器之string容器

文章目录
  • 1.string基本概念
  • 2.string构造函数
  • 3.string赋值操作
  • 4.string字符串拼接
  • 5.string查找和替换
  • 6.string字符串比较
  • 7.string字符存取
  • 8.string插入和删除
  • 9.string子串

相关笔记链接:
[C++提高编程笔记] 一.模板
[C++提高编程笔记] 二.STL初识
[C++提高编程笔记] 三.(一).STL常用容器之string容器
[C++提高编程笔记] 三.(二).STL常用容器之vector容器
[C++提高编程笔记] 三.(三).STL常用容器之deque容器
[C++提高编程笔记] 三.(四).STL常用容器之案例-评委打分
[C++提高编程笔记] 三.(五).STL常用容器之stack容器
[C++提高编程笔记] 三.(六).STL常用容器之queue容器
[C++提高编程笔记] 三.(七).STL常用容器之list容器
[C++提高编程笔记] 三.(八).STL常用容器之set/multiset容器
[C++提高编程笔记] 三.(九).STL常用容器之map/multimap容器
[C++提高编程笔记] 三.(十).STL常用容器之案例-员工分组
[C++提高编程笔记] 四.STL函数对象
[C++提高编程笔记] 五.STL常用算法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。


1.string基本概念

本质: string是C++风格的字符串,而string本质上是一个类。

string和char *区别:
1)char *是一个指针
2)string 是一个类,类内部封装了char *,管理这个字符串,是一个char *类型的容器。

特点:
1)string类内部封装了很多成员方法,例如:查找find,拷贝copy,删除delete,替换replace,插入insert……
2)string管理char *所分配的内存,不用担心复制越界和取值越界等。

2.string构造函数

函数原型:
1)string(); 创建一个空的字符串,例如string str;。
2)string(const char * s); 使用字符串s初始化。
3)string(const string& str); 拷贝构造,使用一个string对象初始化另一个string对象。
4)string(int n, char c); 使用n个字符c初始化。

#include 
using namespace std;
#include 

void test01()
{
    // 第一种:默认构造
    string s1;

    // 第二种:
    const char * str = "hello world"; // c语言风格的字符串
    string s2(str);
    cout << "s2=" << s2 << endl;

    // 第三种:拷贝构造
    string s3(s2);
    cout << "s3=" << s3 << endl;

    // 第四种:字符串里有10个a
    string s4(10, 'a');
    cout << "s4=" << s4 << endl;
}

int main()
{
    test01();

    return 0;
}
3.string赋值操作

功能描述: 给string字符串进行赋值。

赋值的函数原型:
1)string& operator=(const char* s;) C语言风格的char *类型字符串赋值给当前字符串(理解:字符串赋值给string变量)
2)string& operator=(const string &s); 把字符串s赋值给当前的字符串(理解:string变量赋值给string变量)
3)string& operator=(char c); 字符赋值给当前的字符串(理解:字符赋值给string变量)
4)string& assign(const char *s); 把字符串s赋给当前的字符串(理解:字符赋值给string变量)
5)string& assign(const char *s, it n); 用字符串s的前几个字符赋给当前的字符串
6)string& assign(const string &s); 用字符串s赋给当前字符串
7)string& assign(int n, char c); 用n个字符c赋给当前字符串

#include 
using namespace std;
#include 

void test01()
{
    // 第一种方式:
    string str1;
    str1 = "hello world";
    cout << str1 << endl;

    // 第二种方式:
    string str2;
    str2 = str1;
    cout << str2 << endl;

    // 第三种方式:
    string str3;
    str3 = 'a';
    cout << str3 << endl;

    // 第四种方式:
    string str4;
    str4.assign("hello C++");
    cout << str4 << endl;

    // 第五种方式:
    string str5;
    str5.assign("hello,C++", 5); //字符串的前5个字符赋值给str5
    cout << str5 << endl;

    // 第六种方式:
    string str6;
    str6.assign(str5);
    cout << str6 << endl;

    // 第七种方式:
    string str7;
    str7.assign(3, 'w');
    cout << str7 << endl;
}


int main()
{
    test01();

    return 0;
}

总结: string赋值方式很多,一般用operator=(等号赋值)比较多。

4.string字符串拼接 5.string查找和替换 6.string字符串比较 7.string字符存取 8.string插入和删除 9.string子串
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/698273.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号