string容器语法

news2024/9/17 9:01:31

文章目录

  • string容器
    • string基本概念
      • 本质:
      • string和char * 区别:
      • 特点:
    • string构造函数
      • 示例
    • string赋值操作
      • 示例:
    • string字符串拼接
      • 示例:
    • string查找和替换
      • 示例:
    • string字符串比较
      • 示例:
    • string字符存取
      • 示例:
    • string插入和删除
      • 示例:
    • string子串
      • 示例:
    • 数值转换:string与Int
      • string转Int
      • int转string
    • 数值转换:string与其他数据类型

string容器

string基本概念

本质:

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

string和char * 区别:

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

特点:

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

例如:查找find,拷贝copy,删除delete 替换replace,插入insert

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

string构造函数

构造函数:

  • string(); //创建一个空的字符串 例如: string str;
    string(const char* s); //使用字符串s初始化
  • string(const string& str); //使用一个string对象初始化另一个string对象
  • string(int n, char c); //使用n个字符c初始化

示例

#include <string>
//string构造
void test01()
{
	string s1; //创建空字符串,调用无参构造函数
	cout << "str1 = " << s1 << endl;

	const char* str = "hello world";
	string s2(str); //把c_string转换成了string

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

	string s3(s2); //调用拷贝构造函数
	cout << "str3 = " << s3 << endl;

	string s4(10, 'a');
	cout << "str3 = " << s3 << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:string的多种构造方式没有可比性,灵活使用即可

string赋值操作

功能描述:

  • 给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 str1;
	str1 = "hello world";
	cout << "str1 = " << str1 << endl;

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

	string str3;
	str3 = 'a';
	cout << "str3 = " << str3 << endl;

	string str4;
	str4.assign("hello c++");
	cout << "str4 = " << str4 << endl;

	string str5;
	str5.assign("hello c++",5);
	cout << "str5 = " << str5 << endl;


	string str6;
	str6.assign(str5);
	cout << "str6 = " << str6 << endl;

	string str7;
	str7.assign(5, 'x');
	cout << "str7 = " << str7 << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:

​ string的赋值方式很多,operator= 这种方式是比较实用的

string字符串拼接

功能描述:

  • 实现在字符串末尾拼接字符串

函数:

  • string& operator+=(const char* str); //重载+=操作符
  • 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); //同operator+=(const string& str)
  • string& append(const string &s, int pos, int n);//字符串s中从pos开始的n个字符连接到字符串结尾

示例:

//字符串拼接
void test01()
{
	string str1 = "我";

	str1 += "爱玩游戏";

	cout << "str1 = " << str1 << endl;
	
	str1 += ':';

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

	string str2 = "LOL DNF";

	str1 += str2;

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

	string str3 = "I";
	str3.append(" love ");
	str3.append("game abcde", 4);
	//str3.append(str2);
	str3.append(str2, 4, 3); // 从下标4位置开始 ,截取3个字符,拼接到字符串末尾
	cout << "str3 = " << str3 << endl;
}
int main() {

	test01();

	system("pause");

	return 0;
}

总结:字符串拼接的重载版本很多,初学阶段记住几种即可

string查找和替换

功能描述:

  • 查找:查找指定字符串是否存在
  • 替换:在指定的位置替换字符串

函数:

  • int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找
  • int find(const char* s, int pos = 0) const; //查找s第一次出现位置,从pos开始查找
  • int find(const char* s, int pos, int n) const; //从pos位置查找s的前n个字符第一次位置
  • int find(const char c, int pos = 0) const; //查找字符c第一次出现位置
  • int rfind(const string& str, int pos = npos) const; //查找str最后一次位置,从pos开始查找
  • int rfind(const char* s, int pos = npos) const; //查找s最后一次出现位置,从pos开始查找
  • int rfind(const char* s, int pos, int n) const; //从pos查找s的前n个字符最后一次位置
  • int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置
  • string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str
  • string& replace(int pos, int n,const char* s); //替换从pos开始的n个字符为字符串s

示例:

//查找和替换
void test01()
{
	//查找
	string str1 = "abcdefgde";

	int pos = str1.find("de");

	if (pos == -1)
	{
		cout << "未找到" << endl;
	}
	else
	{
		cout << "pos = " << pos << endl;
	}
	

	pos = str1.rfind("de");

	cout << "pos = " << pos << endl;

}

void test02()
{
	//替换
	string str1 = "abcdefgde";
	str1.replace(1, 3, "1111");

	cout << "str1 = " << str1 << endl;
}

int main() {

	//test01();
	//test02();

	system("pause");

	return 0;
}

总结:

  • find查找是从左往后,rfind从右往左
  • find找到字符串后返回查找的第一个字符位置,找不到返回-1
  • replace在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串

string字符串比较

功能描述:

  • 字符串之间的比较

比较方式:

  • 字符串比较是按字符的ASCII码进行对比

= 返回 0

> 返回 1

< 返回 -1

函数:

  • int compare(const string &s) const; //与字符串s比较
  • int compare(const char *s) const; //与字符串s比较

示例:

//字符串比较
void test01()
{

	string s1 = "hello";
	string s2 = "aello";

	int ret = s1.compare(s2);

	if (ret == 0) {
		cout << "s1 等于 s2" << endl;
	}
	else if (ret > 0)
	{
		cout << "s1 大于 s2" << endl;
	}
	else
	{
		cout << "s1 小于 s2" << endl;
	}

}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大

string字符存取

string中单个字符存取方式有两种

  • char& operator[](int n); //通过[]方式取字符
  • char& at(int n); //通过at方法获取字符

示例:

void test01()
{
	string str = "hello world";

	for (int i = 0; i < str.size(); i++)
	{
		cout << str[i] << " ";
	}
	cout << endl;

	for (int i = 0; i < str.size(); i++)
	{
		cout << str.at(i) << " ";
	}
	cout << endl;


	//字符修改
	str[0] = 'x';
	str.at(1) = 'x';
	cout << str << endl;
	
}

int main() {

	test01();

	system("pause");

	return 0;
}

总结:string字符串中单个字符存取有两种方式,利用 [ ] 或 at

string插入和删除

功能描述:

  • 对string字符串进行插入和删除字符操作

函数原型:

  • string& insert(int pos, const char* s); //插入字符串
  • string& insert(int pos, const string& str); //插入字符串
  • string& insert(int pos, int n, char c); //在指定位置插入n个字符c
  • string& erase(int pos, int n = npos); //删除从Pos开始的n个字符

示例:

//字符串插入和删除
void test01()
{
	string str = "hello";
	str.insert(1, "111");
	cout << str << endl;

	str.erase(1, 3);  //从1号位置开始3个字符
	cout << str << endl;
}

int main() {

	test01();

	system("pause");

	return 0;
}

**总结:**插入和删除的起始下标都是从0开始

string子串

功能描述:

  • 从字符串中获取想要的子串

函数:

  • string substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串

示例:

//子串
void test01()
{

	string str = "abcdefg";
	string subStr = str.substr(1, 3);
	cout << "subStr = " << subStr << endl;

	string email = "hello@sina.com";
	int pos = email.find("@");
	string username = email.substr(0, pos);
	cout << "username: " << username << endl;

}

int main() {

	test01();

	system("pause");

	return 0;
}

**总结:**灵活的运用求子串功能,可以在实际开发中获取有效的信息

数值转换:string与Int

std::basic_string - cppreference.com

string转Int

在这里插入图片描述

#include <iostream>
#include <string>
 
int main()
{
    std::string str1 = "45";
    std::string str2 = "3.14159";
    std::string str3 = "31337 with words";
    std::string str4 = "words and 2";
 
    int myint1 = std::stoi(str1);
    int myint2 = std::stoi(str2);
    int myint3 = std::stoi(str3);
    // 错误: 'std::invalid_argument'
    // int myint4 = std::stoi(str4);
 
    std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n';
    std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n';
    std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n';
    //std::cout << "std::stoi(\"" << str4 << "\") is " << myint4 << '\n';
}

输出:

std::stoi("45") is 45
std::stoi("3.14159") is 3
std::stoi("31337 with words") is 31337

int转string

#include <iostream>
#include <string>
 
int main() 
{
    double f = 23.43;
    double f2 = 1e-9;
    double f3 = 1e40;
    double f4 = 1e-40;
    double f5 = 123456789;
    std::string f_str = std::to_string(f);
    std::string f_str2 = std::to_string(f2); // 注意:返回 "0.000000"
    std::string f_str3 = std::to_string(f3); // 注意:不返回 "1e+40".
    std::string f_str4 = std::to_string(f4); // 注意:返回 "0.000000"
    std::string f_str5 = std::to_string(f5);
    std::cout << "std::cout: " << f << '\n'
              << "to_string: " << f_str  << "\n\n"
              << "std::cout: " << f2 << '\n'
              << "to_string: " << f_str2 << "\n\n"
              << "std::cout: " << f3 << '\n'
              << "to_string: " << f_str3 << "\n\n"
              << "std::cout: " << f4 << '\n'
              << "to_string: " << f_str4 << "\n\n"
              << "std::cout: " << f5 << '\n'
              << "to_string: " << f_str5 << '\n';
}

输出:

std::cout: 23.43
to_string: 23.430000
 
std::cout: 1e-09
to_string: 0.000000
 
std::cout: 1e+40
to_string: 10000000000000000303786028427003666890752.000000
 
std::cout: 1e-40
to_string: 0.000000
 
std::cout: 1.23457e+08
to_string: 123456789.000000

数值转换:string与其他数据类型

std::stoi, std::stol, std::stoll - cppreference.com

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/726206.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

第六章 rabbitmq高可用集群

在服务之间会采用mq进行消息通信,而rabbitmq本身也如同consul一样,如果只有一个节点那么就可能出现宕机的问题,并且基于mq的特点我们是可以在多个服务之间使用同一个mq来相互通信,因此高可用的架构设计就必不可少 1、rabbitmq集群方案 主备 远程 镜像 多活 构建 2、…

[uni-app]设置运行到微信小程序

1、设置微信小程序开发工具路径 2、检查微信小程序开发工具是否开启了服务端口 服务端口要是没有开启&#xff0c;会报 initialize。 3、在uni-app开发工具中点击运行微信开发者工具&#xff0c;微信开发工具运行成功。

Spark架构体系

StandAlone模式是spark自带的集群运行模式&#xff0c;不依赖其他的资源调度框架&#xff0c;部署起来简单。 StandAlone模式又分为client模式和cluster模式&#xff0c;本质区别是Driver运行在哪里&#xff0c;如果Driver运行在SparkSubmit进程中就是Client模式&#xff0c;如…

谷歌Med-PaLM 2霸榜医学问答领域

谷歌IO大会上&#xff0c;谷歌CEO桑达尔・皮查伊&#xff08;Sundar Pichai&#xff09;向全世界AI开发者发布了谷歌最新的大型语言模型&#xff08;LLMs&#xff09;PaLM 2&#xff0c;作为对标OpenAI最新大模型GPT-4的竞品&#xff0c;PaLM 2展现出了强大的多语言和推理能力。…

概率论与数理统计发展历史简介

概率论与数理统计发展历史简介 1 介绍1.1 概述1.2 概率论发展历史1.3 统计学发展历史1.4 概率论演化 2 在线学习在线 概率与统计 视觉化学习 -- 布朗大学何志坚老师的数理统计讲义鸢尾花书--统计至简 参考 1 介绍 1.1 概述 概率论是与概率有关的数学分支。虽然有几种不同的概…

Linux内核模块开发 第 8 章

The Linux Kernel Module Programming Guide Peter Jay Salzman, Michael Burian, Ori Pomerantz, Bob Mottram, Jim Huang译 断水客&#xff08;WaterCutter&#xff09;源 LKMPG 8 sysfs: 与模块交互 sysfs 允许用户通过读写模块中的变量实现与内核模块的交互。这个特性在…

leetcode907. 子数组的最小值之和(单调栈-java)

子数组的最小值之和 leetcode907. 子数组的最小值之和题目描述单调栈解法一代码演示单调栈解法二 单调栈专题 leetcode907. 子数组的最小值之和 来源&#xff1a;力扣&#xff08;LeetCode&#xff09; 链接&#xff1a;https://leetcode.cn/problems/sum-of-subarray-minimums…

如何快速学习一门计算机语言

如何快速学习一门计算机语言 掌握一门语言的基本数据类型和基本语法。掌握语言里数组和集合工具类的使用掌握循环分支控制掌握一下该计算机语言面向对象或者函数式编程的特征对异常或者错误的处理文件读写&#xff0c;输入输出流字符串的处理日志的打印运行时module或者librar…

【C语言基础】函数

C语言中的函数是模块化编程的基础&#xff0c;通过函数的定义、实参与形参的传递以及函数的调用流程&#xff0c;我们可以实现代码的重用和逻辑的封装。本文将深入探讨C语言函数的定义方式、实参与形参的传递机制&#xff0c;以及函数的调用流程和局部变量与栈内存的关系。 一、…

企业快递管理制定教程

在经济飞速发展的助力之下&#xff0c;现代企业接触到的制度越来越多&#xff0c;除了我们熟知的CRM、OA等等&#xff0c;管理制度进一步细分。企业寄件在企业内部运转中的地位越发重要&#xff0c;随之也产生了快递管理制度。不少人就会问&#xff1a;有必要这么细分吗&#x…

跨应用连接同一个redis,从redis取缓存,对象属性值都为null

本地idea部署和docker部署问题&#xff0c;连接同一个redis&#xff0c;idea项目的redis缓存&#xff0c;docker中取不到&#xff0c;docker中缓存的redis本地取不到 ✅ 原因&#xff1a;idea本地代码实体类未进行代码混淆&#xff0c;docker代码实体类进行了混淆&#xff0c;…

Caused by: java.io.IOException: CreateProcess error=206, 文件名或扩展名太长

java.io.IOException: Cannot run program "D:\javaAPP\jdk\bin\java.exe" (in directory "D:\java\demo"): CreateProcess error206, 文件名或扩展名太长。 Caused by: java.io.IOException: CreateProcess error206, 文件名或扩展名太长。 删除项目.ide…

Vue -- 生命周期 数据共享

1 组件的生命周期 1.1 生命周期 & 生命周期函数 生命周期&#xff08;Life Cycle&#xff09;是指一个组件从创建 -> 运行 -> 销毁的整个阶段&#xff0c;强调的是一个时间段。 生命周期函数&#xff1a;是由 vue 框架提供的内置函数&#xff0c;会伴随着组件的生命…

leetcode极速复习版-第一章数组

目录 数组 数组理论基础 704二分查找 27移除元素 977.有序数组的平方 209.长度最小的子数组 59.螺旋矩阵II 数组部分总结 数组 数组理论基础 数组的元素是不能删的&#xff0c;只能覆盖。 二维数组&#xff1a; 704二分查找 二分法 middle int(left right)的int 直接对着一个…

SSM学习笔记-------SpringMVC(一)

SSM学习笔记-------SpringMVC_day01 SpringMVC_day011、SpringMVC简介1.1 SpringMVC概述 2、SpringMVC入门案例2.1 需求分析2.2 案例制作步骤1:创建Maven项目&#xff0c;并导入对应的jar包步骤2:创建控制器类步骤3:创建配置类步骤4:创建Tomcat的Servlet容器配置类步骤5:配置To…

【2022吴恩达机器学习课程实验翻译笔记】 Python 和 Jupyter Notebook 简介

为了看着比较连贯&#xff0c;我直接翻译了&#xff0c;不放英文原文对照了 选修实验课: Python 和 Jupyter Notebook 简介 欢迎来到第一节选修实验课 选修实验课的目的是&#xff1a; 提供信息&#xff0c;就像这个notebook一样通过实例加深对课程的理解展示在课程中使用的…

【Unity实战】制作类元气骑士、挺进地牢——俯视角射击游戏多种射击效果(二)(附源码)

文章目录 前言一、火箭筒1. 编写火箭筒脚本2. 创建火箭弹和新爆炸特效的预制体3. 编写火箭弹脚本4. 设置好火箭弹和火箭筒的脚本和参数5. 运行效果 二、激光枪1. 编写激光枪脚本2. 先运行游戏&#xff0c;看看效果3. 美化射线4. 完善代码5. 再次运行游戏6. 升级URP项目7. 后处理…

剑指offer13.机器人的运动范围

一开始没看清题目&#xff0c;没看到要一步一步移动&#xff0c;我以为是看所有格子中有几个格子符合条件&#xff0c;就直接遍历所有格子&#xff0c;把每个格子的i&#xff0c;j每个位数上的数相加看看是否小于k&#xff0c;是就给counts加一最后返回couts&#xff0c;我还说…

OSPF小实验

OSPF小实验 要求&#xff1a; 1、地址配置 R1&#xff1a; R2&#xff1a; R3&#xff1a; R4&#xff1a; R5&#xff1a; R6&#xff1a; 2、启用R1-R3的ospf&#xff0c;划分为区域0 R1&#xff1a; R2&#xff1a; R3&#xff1a; 3、R1-R2之间采用ppp的pap单向认证 R1为…

select + option 获取 value 来 innerHTML 插入内容或元素

目录 select optioninnerHTML 在元素中插入内容 select option 可以实现一个下拉选择&#xff0c;选择到那个&#xff0c;就可以获取其value&#xff0c;并且弹窗。 <!DOCTYPE HTML> <html><head><meta charset"utf-8"><style>.st…