C++ int 和string互相转化
2024-09-06 08:35:41
一.初始化
string string1;
string string2="hello world";
string string3("hello world");
string string4(,'h');
二.对象的比较
可以用 <、<=、==、!=、>=、> 运算符比较 string 对象。按照字典序比较字符串的大小。
还可以使用+运算符拼接字符串
string string2="hello ";
string string3("world");
string string4=string2+string3;//hello world
三.求 string 对象的子串
substr 成员函数可以用于求子串 (n, m),函数原型如下:
string substr(int n = , int m = string::npos) const;
调用时,如果省略 m 或 m 超过了字符串的长度,则求出来的子串就是从下标 n 开始一直到字符串结束的部分。
string string1="hello world";
string string2=string1.substr(,);//ello wo
string string3=string1.substr();//ello world
四.string和int的互相转化
1.int转换成string
string str=to_string(-);//"-12"
string str1=to_string();//"12"
string str2=to_string(-+);//"1"
2.string转换成int
string str=to_string(-);//"-12"
//先转化为char*型,而后再转化为int
int a=atoi(str.c_str());//-12
最新文章
- 3D旋转菜单
- javascript遍历数组最优写法
- hdu 2141 Can you find it?
- [转]ASP.NET MVC 入门5、View与ViewData
- [React] React Fundamentals: State Basics
- Google为何这么屌
- java设计模式---原型模式
- QuickReport多页打印
- Python beautifulsoup 选择器 select 选择<;meta/>;等不需要成对结尾标签未写‘/’
- Centos7搭建kubernetes搭建
- js页面事件方法
- PageRank算法--从原理到实现
- mssql卸载后重新安装 错误解决
- mysql添加字段并且设置默认值
- 关于Python veriable scope 的一点疑问
- django模板语言和过滤
- November 09th, 2017 Week 45th Thursday
- MySQL的innoDB锁机制以及死锁处理
- 一次性选中word中全部Table
- HDU - 3577 Fast Arrangement 线段树