C++中int转为char 以及int 转为string和string 转int和字符串的split
1、对于int 转为char
直接上代码:
正确做法:
void toChar(int b) {
char u;
char buffer[];
_itoa( b, buffer, ); //正确解法一
u = buffer[];
//u = b + 48; //正确解法二
u = (char)b ;//GCC下错误
u = static_cast <char> (b);//GCC下错误
}
不要想当然以为(char)b 就可以,在GCC下这是不行的,推荐用_itoa,标准库函数
2、对于int 转string
直接用函数to_string
3、对于string 类型的变量input转int
atoi(input.c_str())
4、字符串的split,分两种
一、用空格分隔字符串 str = "I love China"
istringstream in(str);
for(string word; in>>word; i++) {
cout <<"word is " << word << endl;
}
输出 :
I
love
China
二、用特殊符号(比如,)分隔,例如string input = "ab,cd,e"
istringstream in(input);
string s;
vector<string> v;
while(getline(in,s,',')) {
v.push_back(s);
}
最后v = {"ab","cd","e"}
C++中int转为char 以及int 转为string和string 转int和字符串的split的更多相关文章
- int main(int argc, char *argv[])解释
int main(int argc, char *argv[]) 详解: #include <stdio.h> int main(int argc, char *argv[]) { int ...
- 实战c++中的string系列--string的替换、查找(一些与路径相关的操作)
今天继续写一些string操作. string给我们提供了非常多的方法,可是每在使用的时候,就要费些周折. 场景1: 得到一个std::string full_path = "D:\prog ...
- C++中int转为char 以及int 转为string和string 转int和空格分隔字符串
1.对于int 转为char 直接上代码: 正确做法: void toChar(int b) { char u; ]; _itoa( b, buffer, ); //正确解法一 u = buffer[ ...
- Qt中 QString 和int, char等的“相互”转换
转载:http://blog.csdn.net/ei__nino/article/details/7297791 Qt中 int ,float ,double转换为QString 有两种方法 1.使用 ...
- [C#]List<int>转string[],string[]转为string
// List<int>转string[] public string[] ListInt2StringArray(List<int> input) { return Arra ...
- C++ 中int,char,string,CString类型转换
1. c++中string到int的转换 1) 在C标准库里面,使用atoi: #include <cstdlib> #include <string> std::stri ...
- C++ 中 int,char*,string,CString之间相互转换-整理
<多字符集下> #include <string> //使用C++标准库的string类时, 定义时 std::string str; using namespace std; ...
- Qt中 QString 和int, char等的“相互”转换,关键是QString.toLocal8Bit().data();
Qt中 int ,float ,double转换为QString 有两种方法 1.使用 QString::number(); 如: long a = 63; QString s = QString:: ...
- 用c++语言编写函数 int index(char *s,char * t),返回字符串t在字符串s中出现的最左边的位置,如果s中没有与t匹配的子串,则返回-1。类似于索引的功能。
首先,分析一下程序的思路: 1:从s的第i个元素开始,与t中的第1个元素匹配,如果相等,则将s的第i+1元素与t中的第2个元素匹配,以此类推,如果t所有元素都匹配,则返回位置i;否则,执行2; 2: ...
随机推荐
- Java知识点梳理——多态
1.定义:多态是同一个行为具有多个不同表现形式或形态的能力,即一个接口不同的实例执行不同的操作: 2.优点:消除类型之间的耦合关系.可替换性.可扩展性.接口性.灵活性.简化性: 3.多态存在的3个必要 ...
- UIActionSheet样式问题心得
下午在做一个iPad的项目,需要用到一个 UIActionSheet. 点击popView中的“sort”按钮,触发出一个ActionSheet. self.action = [[[UIActionS ...
- 将坐标转化为与X轴正半轴夹角模板
//还需加PI 和 mabs 函数 double chg(double x,double y) { double tmps; )<1e-) { ) tmps=90.0; else tmps=27 ...
- HDU 5892 Resident Evil
题目链接:传送门 题目大意:有50种动物,给你n*n的矩阵,m次操作,P代表加入操作,在左上角 x1,y1 到右下角 x2,y2,的矩形范围内加入 种类为x,数量为y的动物. Q代表询问操作,在左上角 ...
- CodeForces 670C Cinema(排序,离散化)
C. Cinema time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Avalondock 第四步 边缘停靠
前一章介绍了分组功能,这一章主要介绍细节信息,LayoutRoot的side属性 LayoutRoot包含四个属性,LeftSide,RightSide,TopSide,BottomSide,分别用于 ...
- 修改docker时区
在实际业务场景中,经常碰到启动了一个容器,容器的时区是UTC的导致还需要重新运行: 我们在具体处理时也出现了该显现 业务场景: 数据库系统定时备份脚本, 定时备份脚本按照每天备份, 通过k8s启动容器 ...
- explorer.exe中发生未处理的win32异常
explorer.exe中发生未处理的win32异常的错误提示,是windows系统比较常见的错误事件,多数在开机遇到,也有在电脑使用过程中遇到. 了解explorer.exe进程 从百度百科了解到, ...
- 用gunicorn+gevent启动Flask项目
转自:https://blog.csdn.net/dutsoft/article/details/51452598 Flask,webpy,Django都带着 WSGI server,当然性能都不好, ...
- 命令行操作flask
Flask-Script 先安装pip3 install Flask-Script from sansa import create_app from flask_script import Mana ...