C++字符串相关内容

server/2025/2/8 11:55:53/

字符串

字符串,本质上是一个接一个字符的一组字符。字母、数字、符号等。

const char*  字符串名

字符后面会有一个空终止符,为0。

字符串从指针的内存地址开始,然后继续下去,直到它碰到0,然后意识到字符串终止了。

#include <iostream>
int main()
{const char* name = "Miles";  //使用const之后,就不能再改变该字符串的内容。//name[2] = "a";char name2[5] = {'M','i','l','e','s'};std::cout << name << std::endl;std::cout << name2 << std::endl;std::cin.get();
}

为了使得代码更加简洁,通常会引入string。

string中有一个构造函数,它接收char*或const char*参数。

#include <iostream>
#include <string>
int main()
{std::string name1 = "Miles";std::cout << name1 << std::endl;std::cin.get();
}

std::string 是一个有很多功能的类。

name.size()可以找到其尺寸;strlen(),字符串的长度;strcpy,复制字符串;

字符串相加,可以考虑重载运算符:“+=”。

#include <iostream>
#include <string>
int main()
{std::string name1 = "Miles";name1 += " hello!";std::cout << name1 << std::endl;std::cin.get();
}
//输出:
//Miles hello!

字符串相加,或者可以用另一种方法进行:将两个相加的字符数组的其中一个,显式调用一个string构造函数。相当于创建了一个字符串,然后附加这个字符串给另一个。

#include <iostream>
#include <string>
int main()
{std::string name1 = std::string("Miles")+ " hello!";std::cout << name1 << std::endl;std::cin.get();
}
//输出:
//Miles hello!

如果要寻找字符串中的文本,可以使用name.find()。

#include <iostream>
#include <string>
int main()
{std::string name1 = std::string("Miles")+ " hello!";bool contains = name1.find("es") != std::string::npos;  std::cout << name1 << std::endl;std::cin.get();
}

其中,std::string::npos代表一个不存在的位置。将结果给contains。

#include <iostream>
#include <string>
void PrintString(std::string string)
{string += "h";std::cout << string << std::endl;
}
int main()
{std::string name1 = std::string("Miles")+ " hello!";PrintString(name1);bool contains = name1.find("es") != std::string::npos;  std::cout << name1 << std::endl;std::cin.get();
}
//输出:
//Miles hello!h
//Miles hello!

需要分配一个全新的char数组,来存储我们已经得到的完全相同的文本。

字符串复制,实际上相当慢。

当我们需要传递一个字符串,而且在只读的情况下,需要确保通过常量引用传递它。此时通常在前面加上const和引用&。

void PrintString(const std::string& string)
{//string += "h";std::cout << string << std::endl;
}

字符串字面量

字符串字面量,是在双引号之间的一串字符。

字符串后面有一个额外的字符,叫做:空终止字符。

#include <iostream>
#include <string>#include <stdlib.h>
int main()
{const char name[7] = "Mi\0les";std::cout << strlen(name) << std::endl; //打印出字符串的长度。std::cin.get();
}

字符串的相加,还能用到string_literals库。在字符串后面加上s,即可实现字符串的相加。

#include <iostream>
#include <string>
#include <stdlib.h>
int main()
{using namespace std::string_literals;std::string name = "Miles"s + " hello!";std::cout << name << std::endl; std::cin.get();
}
//输出:
//Miles hello!

其中,s实际上是一个函数。操作符函数,它返回标准字符串(对象)。

R可以用来忽略转义字符。使得代码更加简洁。

#include <iostream>
#include <string>#include <stdlib.h>
int main()
{const char* example = R"(Line1
line2
line3)"  ;//R忽略转义字符const char* ex = "Line1\n""line2\n""line3\n";std::cout << example << std::endl;std::cout << ex << std::endl;std::cin.get();
}

字符串字面量永远保存在内存的只读区域,

关键字const

关键字

const,称为伪关键字。因为它在改变生成代码方面做不了什么。有点像类和结构体的可见性。

#include<iostream>
#include<string>
int main()
{const int MAX_AGE = 90;std::cin.get();
}

在堆上建立一个整数,就能得到一个指针。此时有两个方案:

  • 逆向引用dereference
  • 重新分配实际的指针,这样会指向别的东西

用const强制定义一个变量为只读常量。但可以强制改变这个定义,如下,利用(int*)可以将const只读常量的值,逆向引用传给另一个变量。

#include<iostream>
#include<string>
int main()
{const int MAX_AGE = 90;int* a = new int;*a = 2;a = (int*)&MAX_AGE;std::cout << *a << std::endl;std::cin.get();
}
//输出:
//90

但,const int* 意味着,不能修改指针指向的内容。该用法和int const*一致。

const int* a = new int;
int const* a = new int;

int* const可以改变指针指向的内容,但不能把实际的指针本身重新赋值,指向别的东西

#include<iostream>
#include<string>
class Entity
{
private:int m_X, m_Y;
public:int GetX() const //此用法,只能读,不能写。该方法只能进行读操作,不能进行其他的修改操作。{return m_X;}void SetX(int x){m_X = x;}
};
int main()
{const int MAX_AGE = 90;int* a = new int;*a = 2;a = (int*)&MAX_AGE;std::cout << *a << std::endl;std::cin.get();
}

如果用再次复制Entity类的方式,需要分配空间,那样会很慢。可以通过常量引用传递的方式。也就是:const Entity& e

#include<iostream>
#include<string>
class Entity
{
private:int m_X, m_Y;
public:int GetX() const{return m_X;}void SetX(int x){m_X = x;}
};
void PrintEntity(const Entity& e)
{std::cout << e.GetX() << std::endl; //由于GetX()用到了const,才能在这里用到e.GetX()
}
int main()
{const int MAX_AGE = 90;int* a = new int;*a = 2;a = (int*)&MAX_AGE;std::cout << *a << std::endl;std::cin.get();
}

如果要对int GetX() const中的变量进行修改的话,可以引入mutable:

#include<iostream>
#include<string>
class Entity
{
private:int m_X, m_Y;mutable int var;
public:int GetX() const{var = 2;return m_X;}void SetX(int x){m_X = x;}
};

mutable允许函数是常量的方法,可以修改变量。

关键字mutable

mutable的两种不同用途:

  • 与const一同使用。
  • 用在lambda表达式中。

或者mutable可以同时覆盖两种情况。

#include <iostream>
#include <string>
class Entity
{
private:std::string m_Name;mutable int m_DebugCount;
public:const std::string& GetName() const{m_DebugCount++;  //每次调用get的时候,都会记一次数return m_Name;}
};
int main()
{const Entity e;e.GetName();std::cin.get();
}

在类成员中使用mutable。

lambda基本上像一个一次性的小函数,可以写出来并赋值给一个变量。

#include <iostream>
#include <string>class Entity
{
private:std::string m_Name;mutable int m_DebugCount;
public:const std::string& GetName() const{m_DebugCount++;  return m_Name;}
};
int main()
{const Entity e;e.GetName();
//lambda的使用如下:int x = 8;auto f = [&](){x++;std::cout << x << std::endl;};f();std::cin.get();
}

lambda还有其他的用法,三种auto用法一致。

int x = 8;
auto f = [&]()
{x++;std::cout << x << std::endl;
};
auto f = [=]() mutable
{x++;std::cout << x << std::endl;
};
auto f = [=]()
{int y = x;y++;std::cout << x << std::endl;
};
f();


http://www.ppmy.cn/server/165944.html

相关文章

零基础Vue入门6——Vue router

本节重点&#xff1a; 路由定义路由跳转 前面几节学习的都是单页面的功能&#xff08;都在专栏里面https://blog.csdn.net/zhanggongzichu/category_12883540.html&#xff09;&#xff0c;涉及到项目研发都是有很多页面的&#xff0c;这里就需要用到路由&#xff08;vue route…

【Docker】 Manifest与Buildx:多架构镜像管理的解析与实践

一.manifest的概述 manifest包含了镜像的层、标签、作者等关键信息&#xff0c;并支持多架构镜像的管理。通过Manifest List&#xff0c;开发者能够为同一应用提供适用于不同架构的镜像&#xff0c;从而确保其在各类平台上的兼容性。实际上是把不同操作系统和架构打包成独立的一…

组学数据分析实操系列 | (二)一键实现MaxQuant非标定量蛋白质组学搜库结果的生信分析

前言 上一篇我们介绍了如何通过MaxQuant软件对非标定量蛋白质组学原始数据进行分析得到搜库结果&#xff0c;下一步该怎么做呢&#xff1f;今天和大家分享一个非常实用的工具——LFQ-Analyst。该工具由蒙纳士大学的组学和生信平台开发&#xff0c;是一个交互式Web应用程序&…

I.MX6ULL 中断介绍下

GIC重点寄存器 1.中断分发器寄存器&#xff08;Distributor register &#xff09; a.Distributor Control Register(中断分发控制寄存器), GICD_CTLR Purpose Enables the forwarding of pending interrupts from the Distributor to the CPU interfaces 使能将挂起的中断从…

高端入门:Ollama 本地高效部署DeepSeek模型深度搜索解决方案

目录 一、Ollama 介绍 二、Ollama下载 2.1 官网下载 2.2 GitHub下载 三、模型库 四、Ollmal 使用 4.1 模型运行&#xff08;下载&#xff09; 4.2 模型提问 五、Ollama 常用命令 相关推荐 一、Ollama 介绍 Ollama是一个专为在本地机器上便捷部署和运行大型语言模型&…

深度探索未来的搜索引擎 —— DeepSeek

随着信息时代的进步&#xff0c;我们每天都在生成、分享和消费大量的数据&#xff0c;如何从海量的内容中迅速找到有价值的信息&#xff0c;成为了现代社会的重要课题。传统的搜索引擎虽然在很长时间内引领了互联网的发展&#xff0c;但随着技术的进步和用户需求的变化&#xf…

Flutter List 的 every 如果回调函数抛出异常 应该如何处理

在使用 List 的 every 方法时&#xff0c;如果回调函数抛出异常&#xff0c;可以通过以下几种方式进行处理&#xff1a; 1. 在回调函数内部捕获异常 在回调函数内部使用 try-catch 语句捕获可能抛出的异常&#xff0c;并根据具体情况进行处理。这样可以避免异常直接导致 ever…

git撤销上一次的提交

1、撤销提交 如果需要撤销上一次的提交&#xff0c;只是提交到了本地&#xff0c;可以通过命令&#xff1a; // 撤销最近的提交&#xff08;保留修改&#xff09; git reset --soft HEAD~1 这个操作可以保留之前的提交和当前的修改。最近一次的提交到本地的修改的提交会回到…