news 2026/8/21 20:10:13

c++的字符串string

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
c++的字符串string

目录

string的概念

string的基本操作

创建

输入

​编辑

获取长度

迭代器

begin()和end()

尾插函数

+=和+运算

尾删pop_back()

insert()插入

find查找

substr()

关系运算

字符串和int 值的转换。


string的概念

string是c++专门创建的字符串类型,为了方便操作字符串。

string中很多的方法。

string的基本操作

首先要包含头文件string.

创建

有初始化的创建,它的末尾不以'\0'为结尾。

#include <iostream> #include<string> using namespace std; int main() { string a="hello world"; //string a("hello world"); cout<<a; return 0; }

string可以直接赋值

#include <iostream> #include<string> using namespace std; int main() { string a; string b("hello world"); a=b; cout<<a; return 0; }

输入

cin输入,不能输入空格

#include <iostream> #include<string> using namespace std; int main() { string a; cin>>a; cout<<a; return 0; }

getline是c++标准函数,读取一行文本存储为字符。

istream&getline(istream& is,string&str);遇到'\n'停止。

#include <iostream> #include<string> using namespace std; int main() { string a; getline(cin,a); cout<<a; return 0; }

istream&getline(istream& is,string&str,char delim);直到遇到delim字符停止

#include <iostream> #include<string> using namespace std; int main() { string a; getline(cin,a,'m');//读到m停止 cout<<a; return 0; }

获取长度

size(),string有很多函数,用 . 使用

#include <iostream> #include<string> using namespace std; int main() { string b; string a="abcd"; string c="abcdefg"; string d="j k l l l"; cout<<a.size()<<endl; cout<<b.size()<<endl; cout<<c.size()<<endl; cout<<d.size()<<endl; return 0; }

string的数据也有下标和正常创建的字符数组一样从0开始。

迭代器

用来遍历容器元素。

iterator类似指针,数组下标。

需要*解引用。

begin()和end()

begin()指向第一个位置,end()指向最后的下一个位置,不存在于字符串。

可以比较大小,加减。

#include <iostream> #include<string> using namespace std; int main() { string a="hello world"; string::iterator it1=a.begin(); string::iterator it2=a.end(); if(it1<it2) cout<<"<"<<endl; else cout<<">"<<endl; return 0; }

可以通过*解引用,输出存储的字符。

#include <iostream> #include<string> using namespace std; int main() { string a="hello world"; string::iterator it1=a.begin(); string::iterator it2=a.end(); cout<<*it1<<endl;//[0] it1++; cout<<*it1<<endl;//[1] it1--; cout<<*it1<<endl;//[0] return 0; }

使用迭代器遍历。

#include <iostream> #include<string> using namespace std; int main() { string a="abcdef"; string::iterator it1=a.begin(); string::iterator it2=a.end(); for (string::iterator i = a.begin(); i != a.end(); ++i) { cout << *i; } cout<<endl; for(auto i=it2-1;i>=it1;--i){ cout<<*i; } return 0; }

还有反向迭代器,reverse_iterator 类型的rbegin(),rend()刚好相反。

尾插函数

a.push_back();在字符串尾部插入字符

#include <iostream> #include<string> using namespace std; int main() { string a="abcdef"; cout<<a<<endl; a.push_back('g'); cout<<a<<endl; return 0; }

+=和+运算

也可以头部拼接。

尾删pop_back()

空的情况下删除会报错。

insert()插入

a.insert(pos , str),在a字符串中的pos位置前插入,str字符串。str可以是string类型的内容,可以是字符串如下图。

a.insert(pos,n,str)插入n个str的字符到pos前。

#include <iostream> #include<string> using namespace std; int main() { string a="abcdef"; cout<<a<<endl; a.insert(3,"6"); cout<<a<<endl; return 0; }

find查找

a.find()返回第一次出现的位置。它的返回值是size_t,相当于下标

a.find(str,位置(默认0))

#include <iostream> #include<string> using namespace std; int main() { string a="abcdef abc def l g k"; string b="de"; string c="l"; size_t t=a.find(b); cout<<t<<endl; cout<<a.find(b,t+1)<<endl; return 0; }

a.find(char*,size_t pos);

可以查找c风格的字符串。

a.find(str,size_t pos ,n)a字符串中查找str前n给字符

也可以找字符。str

找不到会放回-1;无符号整型的-1。2^32-1;

substr()

a.substr(pos,len=npos);pos截取的开始,npos截取的长度。pos默认为0,npos默认到字符串末尾

的长度。

用于截取a字符串pos位置开始n个的字符。

#include <iostream> #include<string> using namespace std; int main() { string a="abcdefghi"; string b=a.substr(3,3); string c=a.substr(3); cout<<b<<endl; cout<<c<<endl; return 0; }

关系运算

==,至少有一个str的字符才可以比较,比较的ascii值对应位置。

#include <iostream> #include<string> using namespace std; int main() { string a="abcdefghi"; string b="abcdefghi"; if(a==b) cout<<"=="<<endl; string c="abcdefgh"; if(a!=c) cout<<"!="<<endl; string d="b"; if(a<d) cout<<"<"<<endl; string e="aa"; if(a>e) cout<<">"<<endl; return 0; }

字符串和int 值的转换。

stoi/stol,stol是int ,stol是long int。

int stoi(str,size_t* idx=0,int base=0)

long stol(str,size_t* idx=0,int base=0)

str是string类型的字符串。

idx是一个指针,从idx位置开始到第一个无法匹配的下标返回

如“123q”返回q的位置

base表示被解析的数字的进制,

2表示被解析的数字当作2进制数字,转化为10进制。

0是自动推到如0x是16,0是8;

#include <iostream> #include<string> using namespace std; int main() { string a="123p"; size_t t=0; int b=stoi(a,&t,10); int c=stoi(a,&t,8); cout<<b<<endl; cout<<c<<endl; return 0; }

stod转化为double,stof转化为float

stod(str,size_t* idx);只有10进制。

stof(str,size_t* idx);

to_string

把数字转换为字符串。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/21 9:57:29

Scrypted:零延迟智能家居视频集成平台的革命性突破

Scrypted&#xff1a;零延迟智能家居视频集成平台的革命性突破 【免费下载链接】scrypted Scrypted is a high performance home video integration and automation platform 项目地址: https://gitcode.com/gh_mirrors/sc/scrypted Scrypted是一个高性能的家庭视频集成…

作者头像 李华
网站建设 2026/8/21 17:45:12

JL — 695X — 板级配置文件常用配置

一、什么是板级配置 板级配置本质上来说&#xff0c;就是一些宏定义 其相当于把整个SDK的功能都抽象成了一个个宏定义&#xff0c;并做成了一个合集&#xff0c;统一存放在一个文件里面&#xff0c;用于开启 / 关闭SDK的特定功能 打开板级配置的文件夹&#xff0c;我们可以发现…

作者头像 李华
网站建设 2026/8/21 23:25:56

​ 技术解析 | SpaSEG:基于无监督CNN的空间转录组多任务分析框架

一、核心问题与解决思路挑战&#xff1a;​ 空间转录组数据分析面临多任务&#xff08;聚类、差异基因、整合、互作&#xff09;分离、跨平台兼容性差、大规模数据计算效率低等痛点。SpaSEG方案&#xff1a;​ 提出一个基于无监督卷积神经网络&#xff08;CNN&#xff09;​ 的…

作者头像 李华
网站建设 2026/8/21 14:01:09

3步搞定Playground v2.5:零基础AI绘画实战指南

3步搞定Playground v2.5&#xff1a;零基础AI绘画实战指南 【免费下载链接】playground-v2.5-1024px-aesthetic 项目地址: https://ai.gitcode.com/hf_mirrors/ai-gitcode/playground-v2.5-1024px-aesthetic 你是否曾经对着空白的画布发呆&#xff0c;想要创作一幅惊艳…

作者头像 李华
网站建设 2026/8/22 1:41:12

BiliRoaming 开源项目安装与使用指南

BiliRoaming 开源项目安装与使用指南 【免费下载链接】BiliRoaming 哔哩漫游&#xff0c;解除B站客户端番剧区域限制的Xposed模块&#xff0c;并且提供其他小功能。An Xposed module that unblocks bangumi area limit of BILIBILI with miscellaneous features. 项目地址: h…

作者头像 李华
网站建设 2026/8/21 12:35:35

2025年12月11日最热门的开源项目(Github)

这份榜单展示了多个开源项目的最新趋势、Star数据和基本情况&#xff0c;以下是对榜单的详细分析&#xff1a; 一、项目整体趋势概述 新兴项目增长迅速&#xff1a;如排名第一的thedotmack/claude-mem&#xff08;创建时间2025年8月31日&#xff09;&#xff0c;虽刚发布&…

作者头像 李华