C++ 第二天 字符串
字符串
字符串是最常用的一种数据类型了,在python中声明字符串和声明其他类型的数据一样,都非常的简单。但是在c++中,对于字符串的操作,相对来说要稍微复杂一些。
C++ 提供了以下两种类型的字符串表示形式:
C 风格字符串
C++ 引入的 string 类类型
C风格字符串
C 风格的字符串起源于 C 语言,并在 C++ 中继续得到支持。字符串实际上是使用 null 字符 \0 终止的一维字符数组。如下面的声明和初始化创建了一个 "Hello" 字符串。由于在数组的末尾存储了空字符,所以字符数组的大小比单词 "Hello" 的字符数多一个。
int main(){
char greeting[] = {'H', 'e', 'l', 'l', 'o', '\0'};
//可以简写成:
char greeting2[] = "Hello";
return ;
}
c风格字符串常见操作
1.遍历字符串
#include<iostream>
int main(){ //最后总会跟着一个\0的空字符,此时括号中如果写长度,必须大于等于6
char name[] = "hello"; for (int i = ; i < sizeof(name ) / sizeof(char); ++i) {
std::cout << name[i] << std::endl;
}
}
2.字符串的其他操作
C语言中提供了针对字符串操作的大量函数,不过在使用之前,需要先引入 #include<cstring>
以下函数的使用,需要引入 #include<ctype.h> 头文件

拷贝、拼接字符串
#include <iostream>
int main(){
//拷贝字符串
char name[] = "hello";
char name2[];
//参数一: 目标字符串, 参数二:源字符串
strcpy(name2 , name);
std::cout << name2 << std::endl;
//拼接字符串
strcat(name2 , " , 张三");
std::cout << name2 << std::endl;
// 返回字符串长度
int len = strlen(name2);
std::cout << "name2的长度:" << len << std::endl;
return ;
}
C++风格字符串
C++ 标准库提供了 string 类类型,支持上述所有的操作,另外还增加了其他更多的功能。需要引入 #include<string> ,由于string类声明在命名空间 std ,所以在使用的首要注意 命名空间的联合使用
#include <iostream>
//必须引入string库
#include <string> using namespace std; int mian(){ string s1;
string s2 {"北京"};
string s3{s2}; string s4 = "你好"; s1 = s3;
return ;
}
C++风格字符串常见操作
1.拼接字符串
c++字符串的拼接跟Python一样,直接用+拼接即可。
#include <iostream>
#include<string>
using namespace std; int main(){
string part1 {"c++"};
string part2 {" is a powerful"};
string sentence ;
sentence = part1 + part2 ;
cout << sentence << endl; // 结果是 c++ is a powerful
return ;
}
2.获取指定位置的字符
可以使用[]和 at()操作字符串
#include <iostream>
#include<string>
using namespace std; int main(){
string s1 {"i love c++"}; cout << s1[]<<endl; // o
cout << s1.at() << endl; // i
return ;
}
3.遍历字符串
#include <iostream>
#include<string>
using namespace std; int main(){
string s1 {"abcdef"}; for(char s : s1){
cout << s << " "; // 打印的结果 :a b c d e f
}
cout << endl; for(int s : s1){
// 因为s1是字符串类型,指定的s又为int类型,所以这里面打印出来是字符所对应的ascii码
cout << s << " "; // 打印的结果:97 98 99 100 101 102
}
cout << endl;
return ;
}
4.字符串比较
字符串也是可以比较大小的。
#include <iostream>
#include<string>
using namespace std; int main(){ string s1{"Apple"};
string s2{"Banana"};
string s3 {"kiwi"};
string s3 {"apple"};
string s3 {s1}; s1 == s5 // true
s1 == s2 // false
s1 != s2 // true
s1 < s2 // True
s1 > s2 // false
s1 == "Apple" // false return ;
}
5.截取字符串
#include <iostream>
#include<string>
using namespace std; int main(){ substr(开始索引, 截取长度); string s1 {"This is a test"};
cout << s1.substr( , ) ; // This return ;
}
6.获取字符(字符串)在字符串中的索引
#include <iostream>
#include<string>
using namespace std;
int main(){
find(搜索的字符) string s1 {"This is a test"};
cout << s1.find("This") ; // 0
cout << s1.find("is") ; //
cout << s1.find("test") ; // 10 return ;
}
7.获取字符串长度
length() : 返回字符串长度
size():返回字符串长度
#include <iostream>
#include<string>
using namespace std; int main(){
string s1 {"abcd"};
cout << "s1的字符串长度为" << s1.length() << endl; // 4
cout << "s1的字符串长度为" << s1.size() << endl; // 4
return 0 ; }
C++中string类的size和length到底有没有区别?
这是c++标准库中的string中length和size的源码
_NODISCARD size_type length() const noexcept { // return length of sequence
return _Get_data()._Mysize;
}
_NODISCARD size_type size() const noexcept { // return length of sequence
return _Get_data()._Mysize;
}
由此可以看出,length()与size()没有区别,都是返回string对象中元素数量,即返回std::distance(begin(),end()) 。length是因为沿用C语言的习惯而保留下来的,string类最初只有length,引入STL之后,为了兼容又加入了size,它是作为STL容器的属性存在的,便于符合STL的接口规则,以便用于STL的算法。
原文链接:https://blog.csdn.net/qq_43152052/article/details/95861329
每天一个表情包,美滋滋

C++ 第二天 字符串的更多相关文章
- 剑指Offer:从第一个字符串中删除第二个字符串中出现过的所有字符
// 从第一个字符串中删除第二个字符串中出现过的所有字符 #include <stdio.h> char* remove_second_from_first( char *first, c ...
- 面试35-删除字符串重复字符-删除出现在第二个字符串中的字符-第一个只出现一次的字符-hash表计数
#include<iostream>#include<algorithm>#include<functional>using namespace std;char ...
- Python学习-第二天-字符串和常用数据结构
Python学习-第二天-字符串和常用数据结构 字符串的基本操作 def main(): str1 = 'hello, world!' # 通过len函数计算字符串的长度 print(len(str1 ...
- <CPP学习 第二天> 字符串的输入 及 String类
今天简单的学习了字符串的输入以及C++的String类. 1.面向行的输入: getline(); getline()函数读取整行,通过回车键输入的换行符来确定输入结尾.要调用这种方法,可以使用cin ...
- python基础知识第二篇(字符串)
基本数据类型 数字 整形 int ---int 将字符串 ...
- python学习第二天--字符串及格式化输出
# 字符串# 字符串取值:字符串名[索引值] 只能取单个值# 正序访问,从0开始str1 = "hello world"print(str1[3]) # 输出"l&quo ...
- 写一个程序可以对两个字符串进行测试,得知第一个字符串是否包含在第二个字符串中。如字符串”PEN”包含在字符串“INDEPENDENT”中。
package lovo.test; import java.util.Scanner; public class Java { @param args public static void main ...
- Windows核心编程第二章,字符串的表示以及宽窄字符的转换
目录 Windows核心编程,字符串的表示以及宽窄字符的转换 1.字符集 1.1.双字节字符集DBCS 1.2 Unicode字符集 1.3 UTF-8编码 1.4 UTF - 32编码. 1.5 U ...
- 【原创】Python第二章——字符串
字符串是一个字符序列,(提醒:序列是Python的一个重要的关键词),其中存放UNICODE字符.Python中的字符串是不可变的(immutable),即对字符串执行操作时,总是产生一个新的字符串而 ...
- SQL Server(第二章) 字符串函数、日期时间函数、转换函数
--1.CONCAT 函数:字符串连接(支持sql server2012 SQL规则 如果与NULL连接返回NILL) SELECT empid,CONCAT(firstname,lastname) ...
随机推荐
- 错误记录-MySql.Data.MySqlClient.MySqlException (0x80004005): Timeout expired.
-- ::25.026 +: [ERR] Connection id "0HLQH64H76UL5", Request id "0HLQH64H76UL5:0000000 ...
- vs2013, EF6.0.0.0 使用Migrations来更新数据库时报错
1.vs中,程序包管理器控制台 2.执行,Enable-Migrations 报错: Migrations have already been enabled in project 'dd'. To ...
- CenterOS的安装配置(配图解)
CenterOS的安装配置 一. 配置虚拟机 打开Virtual Machine(虚拟机),点击create new virtual machine 进入新建虚拟机向导页面,选择[Custom( ...
- 每日一题 - 剑指 Offer 32 - II. 从上到下打印二叉树 II
题目信息 时间: 2019-06-25 题目链接:Leetcode tag: 队列 BFS 难易程度:简单 题目描述: 从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行. 示 ...
- 小程序被冻结,忘记原始ID,如何找回?
登录成功,提示被冻结,选择"账号找回": 阅读须知:账号类型选择“小程序”,需要输入小程序的原始ID,此时已经不记得了~~ 微信:搜索 “ 公众平台安全助手 ” 并关注 点击查 ...
- 【板子】数论基础(持续更新ing...)
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #inclu ...
- 洛谷 P4408 [NOI2003]逃学的小孩
题目传送门 题目描述 Chris家的电话铃响起了,里面传出了Chris的老师焦急的声音:“喂,是Chris的家长吗?你们的孩子又没来上课,不想参加考试了吗?”一听说要考试,Chris的父母就心急如焚, ...
- Redis哨兵集群创建脚本--v1
基础环境 操作系统版本 CentOS Linux release 7.6.1810 (Core) Docker 版本 19.03.11, build 42e35e61f3 Redis 版本 3 ...
- css3 文本行的斑马线
背景知识 CSS 渐变, background-size ,“条纹背景”,“灵活的背景定位 难题 几年前,在刚刚获得 :nth-child() / :nth-of-type() 伪类之后,我们最常用其 ...
- 十年老苹果(A1286)强升Catalina及Win10踩坑记
前言 手头有一台十年老苹果,MacBook Pro,A1286,连视网膜屏都没有,电池也早就衰减以后直接拆掉了(减重). 早些年用得还挺多,后来家里也弄了台式,用得逐渐少了,再后来时不时Windows ...