IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> STL:string容器操作 -> 正文阅读

[C++知识库]STL:string容器操作

2、string 容器

2.1 string基本概念

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

2)string和char*的区别:

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

3)特点:

string类内部封装了很多成员方法

例如查找find、拷贝copy、删除delete等

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

2.2 string构造函数

1)string(); //创建一个空字符串

2)string(const char*s); //使用字符串s初始化

3)string(const string& str); //使用string对象初始化另一个string对象

4)string(int n,char c); //使用n个字符c初始化

#include<iostream>

using namespace std;

#include<string>



void test01()

{

??? string s1;//默认构造 创建一个空字符串

??? cout << "s1=" << s1 << endl;

??? const char* str = "hello world";

??? string s2(str);

??? cout << "s2=" << s2 << endl;

??? string s3(s2);

??? cout << "s3=" << s3 << endl;

??? string s4(4, 'b');

??? cout << "s4=" << s4 << endl;

}



int main()

{

??? test01();

??? return 0;

}

结果:

2.3 string容器 赋值操作

给string字符串赋值。

#include<iostream>

using namespace std;

#include<string>



/*

函数原型:

string& operator= (const char* s);//char*类型字符串赋值给当前字符串

string& operator=(const string& s);//把字符串s赋值给字符串

string& operator=(char c);? //字符赋值给当前字符串

string& assign = (const char* s);//把字符串s 赋值给当前字符串

string& assign = (const char* s,int n);//把字符串s的前n个字符 赋值给当前字符串

string& assign = (const string& s);//把字符串s 赋值给当前字符串

string& assign = (int n,char c);//把n个字符c赋值给当前字符串



*/



void test01()

{

??? string s1;//默认构造 创建一个空字符串

??? s1 = "hello world";

??? cout << "s1=" << s1 << endl;



??? string s2;

??? s2 = s1;

??? cout << "s2=" << s2 << endl;



??? string s3;

??? s3 = 'c';

??? cout << "s3=" << s3 << endl;



??? string s4;

??? s4.assign("hello c++");

??? cout << "s4=" << s4 << endl;



??? string s5;

??? s5.assign("hello c++",5);

??? cout << "s5=" << s5 << endl;

??? string s6;

??? s6.assign(s5);

??? cout << "s6=" << s6 << endl;

??? string s7;

??? s7.assign(10,'b');

??? cout << "s7=" << s7 << endl;



}



int main()

{

??? test01();

??? return 0;

}

2.4 string字符串的拼接

#include<iostream>

using namespace std;

#include<string>

/*

函数原型:

string& operator+= (const char c);//重载+=操作符

string& operator+=(const string& str);//重载+=操作符

string& append(const char* s);//把字符串s 连接到当前字符串末尾

string& append(const char* s,int n);//把字符串s 的前n个字符连接到当前字符串末尾

string& append(const string &s);//同string& operator=(const string& str)

string& append(const strin &s,int ops,int n);//把字符串s 中从ops开始的n个字符连接到当前字符串末尾

*/


void test01()

{

??? string str1 = "我";

??? str1 += "爱玩游戏";

??? cout << str1 << endl;

??? str1 += ':';

??? cout << "str1 = "<<str1 << endl;

??? string str2 = "LOL and DNF";

??? str1 += str2;

??? cout << "str1+=str2 = " << str1 << endl;

??? string str3 = "I";

??? str3.append(" love");

??? cout << "str3 = " << str3 << endl;

??? str3.append("game hhhhh", 4);

??? cout << "str3 = " << str3 << endl;

??? string str4;

??? str4.append(str3);

??? cout << "str4=" << str4 << endl;

??? str4.append(str2, 0, 3);

??? cout << "str4=" << str4 << endl;

}


int main()

{

??? test01();

??? return 0;

}

输出:

2.5 string查找和替换

查找:查找指定字符串是否存在

void test01()

{
??? string str1 = "defmage";
??? int pos1 = str1.find("de"); //从左往右查 找到字符串返回第一个字符在字符串中的位置 未找到则返回-1

??? cout << "pos1=" << pos1 << endl;
??? int pos2 = str1.rfind("de");//从右往左查 找到字符串返回第一个字符在字符串中的位置 未找到则返回-1
??? cout << "pos2=" << pos2<< endl;

}

替换:在指定位置替换字符串

string str2 = "I am cute";

??? str2.replace(5, 4, "ugly");//表示从5号位置其往后四个字符全部替换为指定字符

cout << "str2=" << str2 << endl;

输出:

2.6 字符串的比较

比较方式:按字符的ASCII码逐个进行对比

=:返回0? < :返回-1? >:返回1

函数原型:

int compare(const string &s) const;//与字符串s进行比较

int compare(const char *s) const;

//比较

void test01()

{

??? string str1 = "hello";

??? string str2 = "hello";

??? if (str1.compare(str2) == 0)

??? {

??????? cout << "str1=str2" << endl;

??? }

??? else if (str1.compare(str2) > 0)

??? {

??????? cout << "str1>str2" << endl;

??? }

??? else

??? {

??????? cout << "str1<str2" << endl;

??? }

}

2.7 string 字符的存取

void test01()

{

??? string str1 = "hello";

??? //通过[]访问单个字符

??? for (int i = 0; i < str1.size(); i++)

??? {

??????? cout << str1[i] << " ";

??? }

??? cout << endl;

??? //通过at方式访问单个字符

??? for (int i = 0; i < str1.size(); i++)

??? {

??????? cout << str1.at(i)<< " ";

??? }

??? cout << endl;



??? //修改单个字符

??? str1[0] = 'X';

??? cout <<"str1="<< str1 << endl;

??? str1.at(1) = 'M';

??? cout << "str1=" << str1 << endl;

}

2.8 字符串的插入和删除

str.insert(1,”111”); 在1号位置插入111

str.erase(1,3);从1号位置开始删除3个字符?

2.9 子串的获取

str.substr(int pos,int n) ;返回从pos开始的n个字符组成的字符串

//实用操作

void test01()

{

??? string email = "zhangsan@sina.com";

??? //从邮件地址获取用户名

??? string username;

??? int pos = email.find("@");

??? username = email.substr(0, pos);

??? cout << "username = " << username << endl;

}

输出:

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-08-19 18:45:50  更:2022-08-19 18:49:29 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/13 4:57:12-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码