3-2 C++ string类型
3.2.1 定义和初始化
- 直接初始化(direct initialization):直接调用对应的构造函数,对于string而言,不带“=”即为直接初始化,主要就是列表初始化(间2-2-1),可用圆括号或者大括号
- 拷贝初始化(copy initialization):间接调用构造函数(一般是拷贝构造函数),对于string而言,带“=”即为拷贝初始化
- 具体辨析
3.2.2 对string的操作
(一)重要操作一览
(二)读写操作
cin/cout
- 读入
cin>>s;
:遇到空格就停止读入 - 输出
cout<<s;
#include<iostream>
#include<string>
using namespace std;
int main(){
string s1;
cin>>s1; //读入s1
cout<<s1; //输出s1
return 0;
}
当输入Hello World
时,s1只读取到Hello
,World
在缓冲区没被读取,所以值输出Hello
getline : 可读取整行
基本语法
getline(cin, s);
- 参数:输入流,字符串s
- 副作用:
- 读取输入流的一行,包括
/n
- 将内容去除
/n
后存入字符串s
- 读取输入流的一行,包括
- 返回值:输入流对象
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
getline(cin, s); //读入整行
cout<<s<<endl; //输出
return 0;
}
输入Hello World
时,s全部读取,输出Hello World
注意
- 如果一行的首字符就是
/n
,那么字符串s为空 - 因为
getline
不读取换行符,所以在输出时一般加上<<endl
进行换行并清空缓冲区
(三)获取大小 .size()
string s; unsigned int = s.size();
是string类的一个成员函数
返回字符串s的字符个数,类型是unsigned int【其实是string::size_type,但string::size_type底层就是unsigned int】
避免unsigned int 与 int 混用
string s = "hello";
if(s.size() < -1) //该条件判断式永真,因为-1会转换为一个很大的unsigned int
cout<<"in if statement"<<endl; int length = s.size();
if(length < -1) //判断式为假,因为length是int类型
cout<<"in if statement"<<endl;
(四)比较、赋值与增加
比较: ==
- 参数:string 或 字符串字面量(string literals)
- 副作用:按字典序比较
- 返回值:返回比较结果,bool类型
#include<iostream>
#include<string>
using namespace std;
int main(){
string s1 = "h";
string s2 = "h";
char s3[] = "h";
bool i = (s1 == s2); //比较s1与s2
bool j = (s1 == s3); //比较s1和s3
cout<<"i = "<<i<<endl; //输出 “i = 1”
cout<<"j = "<<j<<endl; //输出 “j = 1“
return 0;
}
- 注
- string == string:比较内容
- string == string literals:比较内容
- string == char:不可比较
- string literals == string literals:比较指针地址【比较内容要用strcmp()函数】
赋值: =
参数:string对象 或 字符串字面量
副作用:把string对象或字符串字面量的值赋值到另一个string中
返回值:string对象
#include<iostream>
#include<string>
using namespace std;
int main(){
string s1 = "h"; //把字符串字面量的值赋给s1
string s2;
s2 = s1; //把s1的值赋给s2
cout<<s1<<endl; //输出 "h"
return 0;
}
增添: +
参数:string对象 或 字符串字面量 或 字符字面量, 但是
+
的其中一个操作数必须是string对象副作用:生成一个新string对象,把
+
左边操作数的内容赋给生成的string对象,再把右边操作数的内容赋给string对象返回值:string对象的引用
#include<iostream>
#include<string>
using namespace std;
int main(){
string s1 = "Hello";
string s2 = "World";
string s3 = s1 + s2; //链接s1,s2,把值赋给s3
cout<<s3<<endl; //输出 “HelloWorld”
return 0;
}
辨析:+`的其中一个操作数必须是string对象
string s1 = "a";
string s2 = s1 + "bc" + '\n'; //合法: 第一个+返回一个string对象,继续和'\n'相加【类似cin/cout链式法则】
string s3 = "bc" + '\n' + s1; //不合法:第一个+两侧的操作数没有string对象。
3.2.3 对string中字符的操作
(一)处理string的所有字符
遍历的三种方式
- 迭代器(Iterator):暂略
- 范围for
- 下标与一般for
范围for
语法
for (declaration : expression) //expression:可遍历序列; declaration:expression基本元素的类型
statement; //对每个元素执行的操作
举例:输出string的每个字符
#include<iostream>
#include<string>
using namespace std;
int main(){
string s = "hello world";
for(auto c : s)
cout<<c<<endl;
return 0;
}
cctype头文件
继承自c语言的ctype.h,但在c++最好写作cctype
isnum(c)
isalpha(c)
isupper(c)
&&toupper(c)
islower(c)
&&tolower(c)
isdigit(c)
ispunct(c)
isspace(c)
几个应用实例
统计输入的字符串
s
中 标点符号的个数#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main(){
string s = "Hello,,world!!!";
int num = 0;
for(char &c : s){
if(ispunct(c))
num++;
}
//输出 "the number of punctuation is 5"
cout<<"the number of punctuation is "<<num<<endl;
return 0;
}
将输入的字符串
s
中的 所有小写字母转为大写【注意使用引用!】#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main(){
string s = "Hello,,world!!!";
for(char &c : s){
if(islower(c))
c = toupper(c);
}
//输出 “HELLO,,WORLD!!!”
cout<<s<<endl;
return 0;
}
(二)处理string中的部分字符:下标(subscripts)
相关说明
可以使用下标直接访问并修改对应的字符值【随机访问并且可以直接修改】
s[inxdex] = 'n'
下标
index
的类型是 string::size_type,即unsigned int,但也可以被自动类型转换为intindex范围: 0<= index < s.size()
用下标遍历
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main(){
string s = "Hello world!!!";
for(decltype(s.size()) index = 0; index < s.size(); ++index) //index类型也可以直接写为int
cout<<s[index];
return 0;
}
引用实例:输出string s的第一个单词
以空格符号为分隔
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main(){
string s = "Hello world!!!";
for(int index = 0; index != s.size() && !isspace(s[index]);
++index)
cout<<s[index];
//输出 “Hello”
return 0;
}
也可写作
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main(){
string s = "Hello world!!!";
for(char c : s){
if(isspace(c)) break;
cout<<c;
}
//最终输出 "Hello"
return 0;
}
3-2 C++ string类型的更多相关文章
- ElasticSearch 5学习(9)——映射和分析(string类型废弃)
在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...
- 每日一记-mybatis碰到的疑惑:String类型可以传入多个参数吗
碰到一个觉得很疑惑的问题,Mybatis的parameterType为String类型的时候,能够接收多个参数的吗? 背景 初学Mybatis的时候,看的教程和书籍上都是在说基本的数据类型如:int. ...
- C#string类型总结
字符串的特性:不可变性,每对字符串做拼接或者重新赋值之类的操作,都会在内存中产生一个新的实例. 所以说,在.Net平台下,如果你对一个字符串进行大量的拼接赋值等操作,会产生大量的垃圾. --- ...
- 把《c++ primer》读薄(3-1 标准库string类型初探)
督促读书,总结精华,提炼笔记,抛砖引玉,有不合适的地方,欢迎留言指正. 问题1:养成一个好习惯,在头文件中只定义确实需要的东西 using namespace std; //建议需要什么再using声 ...
- 【原创】Java和C#下String类型中的==和equals的原理与区别
一.Java下 1.几个例子 public static void main(String[] arge) { String str1 = new String("1234"); ...
- String类型的属性和方法
× 目录 [1]属性 [2]对象通用方法 [3]访问字符方法[4]字符串拼接[5]创建子串方法[6]大小写转换[7]查找子串位置[8]正则匹配方法[9]去除首尾空格[10]字符串比较 前面的话 前面已 ...
- String类型传值以及对象传值
package Virtual; class Stan{ String mm = "hello"; } class Virtual { public static void mai ...
- java动手动脑和课后实验型问题String类型
1.请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? true true false 总结: 使用new关键字创建字符串对象时, 每次申请 ...
- java内存分配和String类型的深度解析
[尊重原创文章出自:http://my.oschina.net/xiaohui249/blog/170013] 摘要 从整体上介绍java内存的概念.构成以及分配机制,在此基础上深度解析java中的S ...
- javascript类型系统——字符串String类型
× 目录 [1]定义 [2]引号 [3]反斜线[4]特点[5]转字符串 前面的话 javascript没有表示单个字符的字符型,只有字符串String类型,字符型相当于仅包含一个字符的字符串 字符串S ...
随机推荐
- AvaloniaChat:一个基于大语言模型用于翻译的简单应用
简介 一个使用大型语言模型进行翻译的简单应用. A simple application using a large language model for translation. 使用指南 访问Gi ...
- Python 开发中,使用bcrypt 或 Passlib 对系统用户密码进行哈希和验证处理
在设计一个系统的时候,肯定都有会有用户身份认证的问题,一般对用户校验的时候,都是对用户存在数据库总的密码哈希值进行判断,从而避免密码泄露和反向解密,那么在Python 开发中,我们可以引入bcrypt ...
- 移动端100vh的问题与解决方案
之所以100vh在移动端出现问题,原因大致如上图,真搞不懂,为什么总是有反人类的设计出现. 经过多方参考,实测有效的方案如下: <style> :root { --vh: 1vh; } & ...
- JavaScript设计模式样例二十二 —— 访问者模式
访问者模式(Visitor Pattern) 定义:使用一个访问者类,改变元素类的执行算法.通过这种方式,元素的执行算法可以随着访问者改变而改变.目的:将数据结构与数据操作分离.场景:您在朋友家做客, ...
- 在NextChat中接入SiliconCloud API 体验不同的开源先进大语言模型
NextChat介绍 One-Click to get a well-designed cross-platform ChatGPT web UI, with GPT3, GPT4 & Gem ...
- 零基础学习人工智能—Python—Pytorch学习(九)
前言 本文主要介绍卷积神经网络的使用的下半部分. 另外,上篇文章增加了一点代码注释,主要是解释(w-f+2p)/s+1这个公式的使用. 所以,要是这篇文章的代码看不太懂,可以翻一下上篇文章. 代码实现 ...
- VMware Workstation虚拟机 + 许可证密钥
VMware Workstation虚拟机 + 许可证密钥 VMware Workstation是什么? VMware简介 VMware 安装 VMware系统要求 VMware 版本下载地址 许可证 ...
- 【Docker学习教程系列】8-如何将本地的Docker镜像发布到私服?
通过前面的学习,我们已经知道,怎么将本地自己制作的镜像发布到阿里云远程镜像仓库中去.但是在实际工作开发中,一般,我们都是将公司的镜像发布到公司自己搭建的私服镜像仓库中,那么一个私服的镜像仓库怎么搭建? ...
- Jenkins 运行pipeline 报错:A Jenkins administrator will need to approve this script before it can be us
之前没有注意过这个问题,是因为之前运行pipeline时,默认勾选了"使用 Groovy 沙盒" 这次不小心取消了勾选导致,重新加上勾选即可
- 数据库中查询含有某个emoji表情的行数据
数据库中查询含有某个emoji表情的行数据 MySQL的情况 代码如下 create table tt6(id int, name varchar(800)); insert into tt6 s ...