C++ string 常用函数总结
头文件:#include<string>
[注]:文中关于个数的参数文档中为 size_type 型,更本质是 size_t 型,因为typedef size_t size_type,而 size_t 在不同系统内实现不同,大概为 unsigned int 型,为简便起见,本文均写为 int 型。另外,string 的许多函数都有各种重载,本文所写的只是较常用的。
赋值
string类型的变量可以直接赋值:string str = "hello world"; //可以直接赋值
cout << str << endl; //string 不支持 C 语言,因此输出只能使用 cout 不能使用 printf
输出:
hello world
使用
string的构造函数来实现拷贝的效果:string substr = string(string str, int start, int num);
由此得到的
substr是截取字符串str从start起num个字符。关于子串还有函数
substr(int start, int num),其效果同上。举例如下:
//返回子字符串 substr
string str = "012345678";
string substr = str.substr(1,3);
cout << substr << endl; substr = string(str, 2, 5);
cout << substr << endl;
输出:
123
23456
长度
函数 string.size() 和 string.length() 均可返回本字符串长度,返回值类型为 int(size_t) 。
运算符
字符串连接 +
举例如下:
string str1 = "abc", str2 = "def";
str = str1 + str2;
cout << str << endl;
输出:
abcdef
字典序比较:
> < >= <= != ==
遍历/访问
使用下标
[]访问同字符数组。
使用迭代器访问
举例如下:
string str = "hello world"; //可以直接赋值
printf("按元素下标访问:%c %c\n", str[0], str[str.size()-1]);
//可以按照元素下标访问 //通过迭代器 iterator 访问 迭代器类似于指针
printf("迭代器访问str:\t");
for(string::iterator it = str.begin(); it != str.end(); ++it)
{
printf("%c ", *it);
}
printf("\n");
printf("str.begin() = #%c#", *str.begin()); //迭代器类似于指针 要加 *
printf("str.end() = #%c#", *str.end());输出:
按元素下标访问:h d
迭代器访问str: h e l l o w o r l d
str.begin() = #h#str.end() = # #
增删改查
插入
string.insert(int pos, string str)其作用为在字符串
string第pos个字符处插入字符串str。删除
string.erase(int pos, int len)其作用为从字符串
string第pos个字符处删除len个字符。清空字符串
string.clear()判断字符串是否为空
string.empty()举例如下:
string str = "hello world"; //插入
str.insert(0, "Start "); //在开头插入
cout << "开头插入:" << str << endl;
str.insert(str.size(), " End."); //在末尾插入
cout << "末尾插入:" << str << endl;
str.insert(6, "Middle "); //在中间插入
cout << "中间插入:" << str << endl; //删除
str.erase(0,1); //删除 从第 0 位开始的 1 个
cout << "删除第一个元素:" << str << endl;
str.erase(0, 2); //删除 从第 0 位开始的 2 个
cout << "删除前两个元素:" << str << endl;
cout << str.empty() << endl;
str.clear(); //清空
cout << str.empty() << endl;
输出:
开头插入:Start hello world
末尾插入:Start hello world End.
中间插入:Start Middle hello world End.
删除第一个元素:tart Middle hello world End.
删除前两个元素:rt Middle hello world End.
0
1
替换
string.replace(int pos, int len, string temp)其作用为替换
string字符串从pos起len个字符为 字符串temp。举例如下:string str = "123456";
string temp = "abc";
str.replace(0, 1, temp);
cout << str << endl;
输出为:
abc23456
查询
string.find()本函数用于在字符串
string中寻找字符或字符串,若找到则返回其第一个字符所在下标位置,若没有对应字符或字符串,则返回string.npos,即-1。举例如下:string str = "hello world"; int found = str.find("world");
if(found != str.npos) //npos = -1
{
printf("%d\n", found);
} found = str.find('l');
if(found != str.npos)
{
printf("%d\n", found);
} found = str.find('.');
if(found == str.npos)
printf("Not found!\n");
输出为:
6
2
Not found!
C++ string 常用函数总结的更多相关文章
- C++ string 常用函数
C++ String常用函数 一,类型别名 size_type 无符号整型 iterator 迭代器类型 const_iterator 只读迭代器 reverse_iterator 逆序迭代器 con ...
- C#string常用函数总结
补充: 1:在C语言里 char占1个字节 而在C#,Java里char占两个字节 数据库里char 中汉占两个字节 字母数字占一个字 2:string ...
- 【STL】string 常用函数
string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...
- 【转】string常用函数
原文地址:http://hi.baidu.com/baowup/blog/item/3a27465c86d71546faf2c066.html/cmtid/de1ef3f0de7554a0a40f52 ...
- String 类的实现(5)String常用函数
2 #include<iostream> 3 #include<stdio.h> 4 #include<assert.h> 5 #include <iom ...
- stl string常用函数
string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...
- C++中的string常用函数用法
标准c++中string类函数介绍 注意不是CString 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而 ...
- c++标准库中的string常用函数总结《转》
标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有 ...
- STL string 常用函数(转)
string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...
- c++中的string常用函数用法总结!
标准c++中string类函数介绍 注意不是CString 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作 ...
随机推荐
- cv::copyMakeBorder()中用0值对齐矩阵,方便后续加速傅里叶变换
int M = cv::getOptimalDFTSize(mul_result.rows); // 获得最佳DFT尺寸,为2的次方 int N = cv::getOptimalDFTSize(mul ...
- WebGPU图形编程(1):建立开发环境 <学习引自徐博士教程>
首先感谢徐博士提供的视频教程,我的博客记录也是学习徐博士进行的自我总结,老徐B站学习视频链接网址:WebGPU图形编程 - 免费视频教程(1):建立开发环境_哔哩哔哩_bilibili 创建之前你需要 ...
- axios请求的封装
/* axios的请求封装 */ //axios的原生写法get,post请求 //第一个参数为请求地址,第二个参数为请求的参数,params是将参数拼接在url的后面 ...
- gin中提供静态文件服务
package main import ( "github.com/gin-gonic/gin" "net/http" ) func main() { // 静 ...
- ansible 常用模块和playbook
- 使用Xamarin开发移动应用示例——数独游戏(四)产生新游戏算法改进
项目代码可以从Github下载:https://github.com/zhenl/ZL.Shudu .代码随项目进度更新. 前面我们使用一个数组保存预制的游戏,然后随机从中抽取一个游戏作为新游戏,如果 ...
- Java多线程专题5: JUC, 锁
合集目录 Java多线程专题5: JUC, 锁 什么是可重入锁.公平锁.非公平锁.独占锁.共享锁 可重入锁 ReentrantLock A ReentrantLock is owned by the ...
- Learning local feature descriptors with triplets and shallow convolutional neural networks 论文阅读笔记
题目翻译:学习 local feature descriptors 使用 triplets 还有浅的卷积神经网络.读罢此文,只觉收获满满,同时另外印象最深的也是一个浅(文章中会提及)字. 1 Cont ...
- 集合remove()方法相关问题
学习集合的过程中,了解到一个有关于remove()方法的有关特性,特此记录 首先remove方法的格式: collection.remove(Object o); 这是指对集合collection内的 ...
- 在IDE中添加widfly依赖
动机:在IDE中添加widfly依赖 原由:widfly实现了servlet接口,有我们对外交互时所需求的jar包 步骤: 第一步: 找到module依赖的地方 第二步:点击左侧的添加按钮,点击Lib ...