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: ...
随机推荐
- word中使用MathType能做什么
在Office中写论文,特别是一些比较专业的论文需要用到各种公式的.会发现有很多地方Office自带的公式编辑器都无法完成,所以要用到MathType公式编辑器这个好用的工具了.MathType是一款 ...
- Docker for window 无法共享磁盘
Docker for window 无法共享主机磁盘,环境如下: 操作系统: windown10 Docker version 18.09.0, build 4d60db4 症状如下: 如图,点击ap ...
- 到底什么是hash
1.什么是hash算法 Hash(散列.杂凑)算法,是把任意长度的输入通过特定的算法变换成固定长度的输出,输出的值就是hash值.这个特定的算法就叫hash算法,hash算法并不是一个固定不变的算法. ...
- 第三篇:POSIX标准中的 “ 限制 ”
前言 在POSIX标准中,定义了许多限制.这些限制大约分为五类,不同类型的限制获取的方式不一样. 限制值分类 1. 不变的最小值 这类型的限制值是静态的,固定的. 2. 不变值 同上 3. 运行时可以 ...
- 获取系统DPI
public partial class Form1 : Form { public Form1() { InitializeComponent(); ...
- python 之 多进程
阅读目录 1. Process 2. Lock 3. Semaphore 4. Event 5. Queue 6. Pipe 7. Pool 序. multiprocessingpython中的多线程 ...
- windows平台 - 0基础学习node.js(一)
首先得明白node.js做什么用的: 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Nod ...
- linux ftp 上传与下载命令解析
month=`date -d "last month" +"%Y%m"` year=`date +"%Y"` rm /home/yourDi ...
- Leetcode-Combinations Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【IDEA】重装基本设置+插件安装
基本配置:2.1 显示:2.1.1.选中展示Toolbar2.1.2.显示内存占用:2.1.3.显示行号和方法线:2.1.4.代码软分行:2.2.修改快捷键:2.2.1 修改Ctrl + D 快捷键: ...