string与int的相互转换C++(转)
string与int之间的相互转换C++(转)
#include<iostream>
#include<string>
#include<sstream> using namespace std; int main()
{
/////////////////////////// string 转为 int
string str="1234";
int n;
istringstream iss;//istringstream从string读入,和cin一样仅仅重载了>>,可以把string转为int
iss.clear();//每次使用前先清空
iss.str(str);
iss>>n;//将输入流中的内容写入到int n,
cout<<n<<endl; //////////////////////////////// int 转为 string n=111;
ostringstream oss;//用于向string写入,和cout<<一样,仅仅重载了<<
oss<<n;
str=oss.str();
cout<<str<<endl; ///////////////////////////////// string 转为 int
str="22222";
sscanf(str.c_str(),"%d",&n); //scanf前面加s用于把str输入到n中
cout<<n<<endl; /////////////////////////////// int 转为 string int ss=1000;
char temp[64];
sprintf(temp,"%d",ss); //printf前面加s用于将ss按整数形式输出到数组temp中,不能直接给str.c_str();
str=temp;//再把数组temp赋值给str;
cout<<str<<endl;
return 0;
}
原文地址:http://www.cnblogs.com/wang7/archive/2012/04/17/2454470.html
string与int的相互转换C++(转)的更多相关文章
- C++语法小记---string和int的相互转换
string和int的相互转换 string转int istringstream is(""); //构造输入字符串流,流的内容初始化为“12”的字符串 int i; is > ...
- string和int的相互转换方法
string转为int string str = "100000"; stringstream ss; ss << str; int i; ss >> i; ...
- Java学习之String与int的相互转换
•String 转 int 两种方式 int a = Integer.parseInt(s);int b = Integer.valueOf(s).intValue(); 代码 public clas ...
- Java中String和Int的相互转换
一.将字串 String 转换成整数 intA. 有2个方法:1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([Strin ...
- string与int的相互转换以及把一个字符加入到string的末尾
#include "stdafx.h" #include<sstream> #include<string> #include<iostream> ...
- C++ char*,const char*,string,int 的相互转换
C++ char*,const char*,string,int 的相互转换 1. string转const char* string s ="abc";const char* ...
- std::string和int类型的相互转换(C/C++)
字符串和数值之前转换,是一个经常碰到的类型转换. 之前字符数组用的多,std::string的这次用到了,还是有点区别,这里提供C++和C的两种方式供参考: 优缺点:C++的stringstream智 ...
- C++ 中 string, char*, int 类型的相互转换
一.int 1.int 转换成 string 1) to_string函数 —— c++11标准增加了全局函数std::to_string: string to_string (int val); s ...
- String,Integer,int类型之间的相互转换
String, Integer, int 三种类型之间可以两两进行转换 1. 基本数据类型到包装数据类型的转换 int -> Integer (两种方法) Integer it1 = new I ...
随机推荐
- 洛谷 P4057 [Code+#1]晨跑
P4057 [Code+#1]晨跑 题目描述 “无体育,不清华”.“每天锻炼一小时,健康工作五十年,幸福生活一辈子” 在清华,体育运动绝对是同学们生活中不可或缺的一部分.为了响应学校的号召,模范好学生 ...
- Ubuntu 16.04在搭建Redis Cluster搭建时,使用gem install redis时出现:ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /var/lib/gems/2.3.0 directory.
注意:千万不要使用sudo来执行gem install redis. 解决方法: sudo apt-get update sudo apt-get install git-core curl zlib ...
- 移动端 js 实现图片上传 预览
方法一: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv=&q ...
- Sublime Text 2 编辑器实用技巧大全
http://blog.163.com/lgh_2002/blog/static/440175262012429146486/
- qt-qml移动开发之在ios上开发和部署app流程简单介绍
qt5.3已经全面支持移动开发,除了mac,windows,linux.还支持ios,android,wp,meego等移动平台,本教程是作者依据自己的经验,从头讲怎么样在ios上公布自己的app.因 ...
- tesnorflow Conv2DTranspose
tensorflow/python/layers/convolutional.py # Infer the dynamic output shape: out_height = utils.decon ...
- cocos2dx游戏开发学习笔记2-从helloworld開始
一.新建project 具体安装和新建project的方法在cocos2dx文件夹下的README.md文件里已经有具体说明,这里仅仅做简介. 1.上官网下载cocos2dx-3.0的源代码.http ...
- overwrite 复制
[root@myv xiaole_dl_img]# cp upfc/mainDEBUGmysqllogTEST.py online_package_test_/tmp/ cp: overwrite ‘ ...
- HDU 1505 City Game(DP)
City Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- 美国诚实签经验——我们准备了XX万的存款,这足以应付我预算表中涉及的所有开支,如果有意外情况我们的资金不足以应付,我们双方的父母也会支援我们,绝对不会占用美国的任何福利
闲话少絮,直接分享本人诚实签的经验,希望对有此打算的朋友有帮助! 个人经验有限,经历仅供参考~~ 第一步:决定赴美生子. 第二步:选择诚实签. 第三步:填写DS160表格(网址https://ceac ...