string概述

作为c++语言用作字符串处理的标准库,和Python字符串类型有较多相似之处。可以使用‘=’进行赋值,“==”来比较,“+”进行拼接。

构造函数

  • string s;直接声明变量
  • string s(str);创建一个str的副本s
  • string s(str,idx);将str字符串索引值及其后面的部分当做s初值
  • string s(str,idx,len);将str从idx开始,长度为len的内容赋值给s
  • string s(c-str);将char*字符串作为字符串s初值
  • string s(chars,len);以字符数组的前len个元素作为s初值
  • string s(num,c);生成num个c字符的字符串。c为int类型
  • string s(begin,end);将[begin, end]区间内的字符做为s初值,begin,end为可迭代对象
  • string s(init-list);将init-list内字符做为s初值

示例代码

点击查看代码
#include <iostream>
#include <string>
#include <cstring>
using namespace std; int main() {
/*构造字符串*/
string str1="hello world";
cout<<"str1:"<<str1<<endl;
string str2(str1);
cout<<"str2:"<<str2<<endl;
string str3(str1,2);
cout<<"str3:"<<str3<<endl;
string str4(str1,2,3);
cout<<"str4:"<<str4<<endl;
char word[20]="new string";
string str5(word,5);
cout<<"str5:"<<str5<<endl;
string str6(word);
cout<<"str6:"<<str6<<endl;
string str7(&str6[2],&str6[7]);
cout<<"str7:"<<str7<<endl;
char *a="c-----string";
string str8(a,5);
cout<<"str8:"<<str8<<endl;
string str9(a);
cout<<"str9:"<<str9<<endl;
string str10(10,121);
cout<<"str10:"<<str10<<endl;
return 0;
}

输出结果为:

----------------------------------------
str1:hello world
str2:hello world
str3:llo world
str4:llo
str5:new s
str6:new string
str7:w str
str8:c----
str9:c-----string
str10:yyyyyyyyyy 进程已结束,退出代码为 0
--------------------------------------------------

string值操作

  • =,assign(str);直接赋值,相互等价
  • swap(str1,str2);将str1与str2值交换
  • +,append(str);字符串拼接
  • push_back(c);c为int类型
  • pop_back();移除末尾字符
  • insert(pos,str);指定pos处添加str
  • erase(i);保留前i位字符,i<=str.size()
  • resize(len);保留前len位字符,长度不够则补位;

示例代码

点击查看代码
#include <iostream>
#include <string>
#include <cstring> using namespace std; int main() {
string str1="hello world";
cout<<"str1:"<<str1<<endl;
/*赋值操作*/
str1="你好";
cout<<"str1:"<<str1<<endl;
str1.assign("a new string!");
cout<<"str1:"<<str1<<endl;
string str2;
str2.assign("1234567");
cout<<"str2:"<<str2<<endl;
swap(str1,str2);
cout<<"str1:"<<str1<<endl;
cout<<"str2:"<<str2<<endl;
str2.append(",ok");
cout<<"str2:"<<str2<<endl;
str1+="10";
cout<<"str1:"<<str1<<endl;
str1.push_back('1');
cout<<"str1:"<<str1<<endl;
str1.pop_back();
cout<<"str1:"<<str1<<endl;
str2.insert(3,"insert data");
cout<<"str2:"<<str2<<endl;
str2.erase(22);
cout<<"str2:"<<str2<<endl;
cout<<"str2大小:"<<str2.size()<<endl;
str1.resize(16);
cout<<"str1:"<<str1<<endl;
}

输出结果为:

----------------------------------------
str1:hello world
str1:你好
str1:a new string!
str2:1234567
str1:1234567
str2:a new string!
str2:a new string!,ok
str1:123456710
str1:1234567101
str1:123456710
str2:a ninsert dataew string!,ok
str2:a ninsert dataew strin
str2大小:22
str1:123456710

进程已结束,退出代码为 0

--------------------------------------------------

替换字符str.replace()

  • str.replace(pos,n,str2);将str 从[pos]开始的n个元素用str2替代;
  • str.replace(pos1,n1,str2,pos2,n2);将str 从[pos1]开始的n1个元素用str2从[pos2]开始的n2个元素替代;
  • str.replace(i1,i2,k1,k2);将i1-i2元素用k1-k2元素代替,i1,i2,k1,k2均为可迭代对象
点击查看代码
#include <iostream>
#include <string> using namespace std; int main() {
string str3 = "0123456780000";
string str4 = "asdfghjkl@#$";
cout << "str3:" << str3 << endl;
cout << "str4:" << str4 << endl;
str4.replace(5, 5, str3);
cout << "str4:" << str4 << endl;
str4.replace(0, 7, str3, str3.find("2"), 4);
cout << "str4:" << str4 << endl;
str4.replace(str4.begin() + 3, str4.begin() + 5, str3.begin(), str3.end() - 3);
cout << "str4:" << str4 << endl;
}
输出结果:
--------------------------------------
str3:0123456780000
str4:asdfghjkl@#$
str4:asdfg0123456780000#$
str4:234523456780000#$
str4:23401234567803456780000#$
进程已结束,退出代码为 0
---------------------------------------

字符串与数字相互转换

字符串转数字

  • stoi();转换为int型
  • stol();转换为long int型
  • stoll();转换为long long 型
  • stoul();转换为 unsigned long型
  • stoull();转换为unsigned long long 型
  • stof();转换为float型
  • stod();转换为double型
  • stold();转换为long double型
  • to_string();将数字转换为string;
点击查看代码
#include <iostream>
#include <string> using namespace std; int main() {
string str1 = "-12345";
string str2 = "-345.78888";
auto num1 = stoi(str1);
auto num2 = stol(str1);
auto num3 = stold(str1);
cout << "num1=" << num1 << endl;
cout << "num2=" << num2 << endl;
cout << "num3=" << num3 << endl;
string str3 = "12345";
auto num4 = stoul(str3);
auto num5 = stoull(str3);
cout << "num4=" << num4 << endl;
cout << "num5=" << num5 << endl;
auto num6 = stof(str2);
auto num7 = stod(str2);
auto num8 = stold(str2);
cout << "num6=" << num6 << endl;
cout << "num7=" << num7 << endl;
cout << "num8=" << num8 << endl;
auto num10=-12.3456;
auto str4= to_string(num10);
cout << "num10->str:" << str4 << endl;
}
输出结果
----------------------------------
num1=-12345
num2=-12345
num3=-12345
num4=12345
num5=12345
num6=-345.789
num7=-345.789
num8=-345.789
num10->str:-12.345600
进程已结束,退出代码为 0
------------------------------------------------
***不可将“-123"转换为unsigned 型,不可将“123.12”转换为int型,可以将“-1234”转换为浮点类型***

字符搜索和查找

  • find()
  • rfind()
  • find_first_of()
  • find_last_of()
  • find_first_not_of()
  • find_last_not_of()

    实参(value,pos,n):

    value:要查找的字符值

    pos,开始查找的位置

    n,查找范围

    返回值,索引

示例代码

点击查看代码
#include <iostream>
#include <string> using namespace std; int main() {
string str1 = "In addition, some swarm intelligence methods that mimic\n "
"biological behaviors are also gradually applied to regional\n"
"coverage planning tasks. Reference proposed a rule-\n"
"based collaborative search algorithm based on the kinetic\n"
"model-Boids.";
cout<<str1.size()<<endl;
string str2(str1,100,10);
cout<<str2<<endl;
cout<<str1.find_first_of("t",100,100);
}
输出结果:
-------------------------------------
244
lied to re
104
进程已结束,退出代码为 0
-----------------------------------------------

C++标准库string学习笔记的更多相关文章

  1. 《C标准库》学习笔记整理

    简介 <C标准库>书中对 C 标准库中的 15 个头文件的内容进行了详细的介绍,包括各头文件设计的背景知识.头文件中的内容.头文件中定义的函数和变量的使用.实现.测试等. 我学习此书的目的 ...

  2. C++标准库第二版笔记 2.1

    C++标准库第二版笔记 2.1 1 Range-Based for 循环 for ( decl : coll ) { statements; } // collaborate 类似C# foreach ...

  3. C++标准库第二版笔记 2

    C++标准库第二版笔记 2 微小但重要的语法提升 template表达式内的空格: vector< list<int> >; // OK in each C++ version ...

  4. go标准库的学习-net/http

    参考:https://studygolang.com/pkgdoc 概念解释: request:用户请求的信息,用来解析用户的请求信息,包括post.get.cookie.url等信息 respons ...

  5. go标准库的学习-database/sql

    参考:https://studygolang.com/pkgdoc 导入方式: import "database/sql" sql包提供了保证SQL或类SQL数据库的泛用接口. 使 ...

  6. 【C++ Primer每日刷】之三 标准库 string 类型

    标准库 string 类型 string 类型支持长度可变的字符串.C++ 标准库将负责管理与存储字符相关的内存,以及提供各种实用的操作.标准库string 类型的目的就是满足对字符串的一般应用. 与 ...

  7. [Python ]小波变化库——Pywalvets 学习笔记

    [Python ]小波变化库——Pywalvets 学习笔记 2017年03月20日 14:04:35 SNII_629 阅读数:24776 标签: python库pywavelets小波变换 更多 ...

  8. C++标准库string类型

    string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作.标准库string类型的目的就是满足对字符串的一般应用. 本文地址:http://www.cn ...

  9. C++标准库<string>简单总结

    C++标准库<string>简单总结 在C++中,如果需要对字符串进行处理,那么它自带的标准库<string>无疑是最好的选择,它实现了很多常用的字符处理函数. 要想使用标准C ...

  10. C++标准库string

    C++标准库string 定义和初始化 string s1 默认初始化,s1是一个空串 string s2(s1) s2是s1的副本 string s2 = s1 等价于s2(s1),s2是s1的副本 ...

随机推荐

  1. WCF 服务容器化的一些问题

    背景 目前项目当中存有 .NET Framework 和 .NET Core 两种类型的项目,但是都需要进行容器化将其分别部署在 Windows 集群和 Linux 集群当中.在 WCF 进行容器化的 ...

  2. [OpenCV实战]17 基于卷积神经网络的OpenCV图像着色

    目录 1 彩色图像着色 1.1 定义着色问题 1.2 CNN彩色化结构 1.3 从 中恢复彩色图像 1.4 具有颜色再平衡的多项式损失函数 1.5 着色结果 2 OpenCV中实现着色 2.1 模型下 ...

  3. mysql基础命令语法

    删除空格 update 表名 set 字段名 = replace(字段名 ,' ','') ; 临时表创建与删除 -- 创建临时表 create temporary table if not exis ...

  4. 一种面向业务配置基于JSF广播定时生效的工具

    作者:京东物流 王北永 姚再毅 李振 1 背景 目前,ducc实现了实时近乎所有配置动态生效的场景,但是配置是否实时生效,不能直观展示每个机器上jvm内对象对应的参数是否已变更为准确的值,大部分时候需 ...

  5. 从0到1手把手教你实现vite系列--重写依赖请求路径,处理/@modules/vue引用

    前面以及写了三篇了,这是第四篇,等我写完就合并起来哦 这个是第一篇的链接:vite原理,创建项目,基础知识 这个是第二篇的链接Vite-中篇-通过服务访问静态资源以及重写请求路径 这个是第三篇的链接# ...

  6. angular Ionic CLI组件建立,使用图标,弹窗,按钮,卡片,列表,无尽滚动,刷新

  7. 【分析笔记】NXP PCF85263 设备驱动分析笔记

    驱动移植 供应商无法提供相应的驱动程序,不过在 linux 最新的内核倒是有一份 pcf85363 的驱动,看代码并核对寄存器功能,是可以兼容 pcf85263 芯片.只是我们用的内核比较老 linu ...

  8. 【分析笔记】Linux gpio_wdt.c 看门狗设备驱动源码分析

    基本原理 该看门狗的设备驱动实现原理很简单,比较主要的有两点: 一.定时器喂狗 通过定时器根据配置文件配置的喂狗方式(如脉冲切换.电平切换),对指定的 gpio 进行脉冲切换或电平切换实现喂狗. 脉冲 ...

  9. 逗号(,)运算符在Javascript中

    逗号运算符 逗号运算符是二元运算符,它能够先执行运算符左侧的操作数,然后再执行右侧的操作数,最后返回右侧操作数的值. 逗号表达式: 一般形式:表达式1,表达式2,表达式3,......表达式n 求解过 ...

  10. Vue3 企业级优雅实战 - 组件库框架 - 10 实现组件库 cli - 下

    上文创建了一堆 utils.component-info,并实现了新组件模块相关目录和文件的创建.本文继续实现后面的内容. 1 组件样式文件并导入 在 src/service 目录中创建 init-s ...