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的所有字符

遍历的三种方式

  1. 迭代器(Iterator):暂略
  2. 范围for
  3. 下标与一般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)

相关说明

  1. 可以使用下标直接访问并修改对应的字符值【随机访问并且可以直接修改s[inxdex] = 'n'

  2. 下标index的类型是 string::size_type,即unsigned int,但也可以被自动类型转换为int

  3. index范围: 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类型的更多相关文章

  1. ElasticSearch 5学习(9)——映射和分析(string类型废弃)

    在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...

  2. 每日一记-mybatis碰到的疑惑:String类型可以传入多个参数吗

    碰到一个觉得很疑惑的问题,Mybatis的parameterType为String类型的时候,能够接收多个参数的吗? 背景 初学Mybatis的时候,看的教程和书籍上都是在说基本的数据类型如:int. ...

  3. C#string类型总结

    字符串的特性:不可变性,每对字符串做拼接或者重新赋值之类的操作,都会在内存中产生一个新的实例.  所以说,在.Net平台下,如果你对一个字符串进行大量的拼接赋值等操作,会产生大量的垃圾.    --- ...

  4. 把《c++ primer》读薄(3-1 标准库string类型初探)

    督促读书,总结精华,提炼笔记,抛砖引玉,有不合适的地方,欢迎留言指正. 问题1:养成一个好习惯,在头文件中只定义确实需要的东西 using namespace std; //建议需要什么再using声 ...

  5. 【原创】Java和C#下String类型中的==和equals的原理与区别

    一.Java下 1.几个例子 public static void main(String[] arge) { String str1 = new String("1234"); ...

  6. String类型的属性和方法

    × 目录 [1]属性 [2]对象通用方法 [3]访问字符方法[4]字符串拼接[5]创建子串方法[6]大小写转换[7]查找子串位置[8]正则匹配方法[9]去除首尾空格[10]字符串比较 前面的话 前面已 ...

  7. String类型传值以及对象传值

    package Virtual; class Stan{ String mm = "hello"; } class Virtual { public static void mai ...

  8. java动手动脑和课后实验型问题String类型

    1.请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? true true false 总结: 使用new关键字创建字符串对象时, 每次申请 ...

  9. java内存分配和String类型的深度解析

    [尊重原创文章出自:http://my.oschina.net/xiaohui249/blog/170013] 摘要 从整体上介绍java内存的概念.构成以及分配机制,在此基础上深度解析java中的S ...

  10. javascript类型系统——字符串String类型

    × 目录 [1]定义 [2]引号 [3]反斜线[4]特点[5]转字符串 前面的话 javascript没有表示单个字符的字符型,只有字符串String类型,字符型相当于仅包含一个字符的字符串 字符串S ...

随机推荐

  1. TOML 使用

    TOML Reference TOML Tom's Obvious, Minimal Language TOML 被设计成可以无歧义地映射为哈希表.(相当于 JSON 对象吧) 注释 # 这是一个全行 ...

  2. Maven / Gradle 依赖管理

    添加外部依赖 向你的 Maven / Gradle 项目添加依赖的过程可分为如下几步: 搜索依赖 搜索你要安装的依赖,比如你需要 MySQL Connector/J,可以在谷歌搜索"MySQ ...

  3. Linux 检查端口监听情况

    使用 lsof $ sudo lsof -i :22 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME sshd 963 root 3u IPv4 ...

  4. Android RecyclerView 获取当前滚动到的Item项

    背景:RecyclerView  左右滑动时,需要获取当前显示在页面上的选项卡 步骤: 1. RecyclerView  添加addOnScrollListener,回调中可以直接获取对应Item I ...

  5. Unrecognized SSL message, plaintext connection?

    报错:Unrecognized SSL message, plaintext connection? 修改:把 requestContext.setScheme(Scheme.HTTPS);修改为 r ...

  6. 传染病模型 SI

    参考了这篇写的很好的[1],讲了各种模型 因为是各种模型都是用微分方程写的,所以又去学习了一下微分方程 ,真的忘了有没有学过这个,反正一点印象也没有了. 好在[2] 这个文章又把我带回去了. SI 的 ...

  7. 详解JVM 内存结构与实战调优总结

    详解JVM 内存结构与实战调优总结 GC优化案例做个总结: 1在进行GC优化之前,需要确认项目的架构和代码等已经没有优化空间.我们不能指望一个系统架构有缺陷或者代码层次优化没有穷尽的应用,通过GC优化 ...

  8. c++ 命名的强制类型转换

    显式转换:显式将一种类型转换为另一种类型. References: C++中的显示数据类型转换 与命名的强制类型转换相比,旧式的强制类型转换从表现形式上来说不那么清晰明了,容易被看漏,所以一旦转换过程 ...

  9. 如何将图片转换为向量?(通过DashScope API调用)

    本文介绍如何通过模型服务灵积DashScope将 图片转换为向量 ,并入库至向量检索服务DashVector中进行向量检索. 模型服务灵积DashScope,通过灵活.易用的模型API服务,让各种模态 ...

  10. TypeScript 学习笔记 – Handbook 1

    前言 一转眼, 一年多没有写 TypeScript 了. 最近又要开始写了, 乘此机会打算系统学习一下, 顺便写一个学习笔记. 我接触 TypeScript 比较早, 那是 Angular 2 bet ...