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++(转)的更多相关文章

  1. C++语法小记---string和int的相互转换

    string和int的相互转换 string转int istringstream is(""); //构造输入字符串流,流的内容初始化为“12”的字符串 int i; is > ...

  2. string和int的相互转换方法

    string转为int string str = "100000"; stringstream ss; ss << str; int i; ss >> i; ...

  3. Java学习之String与int的相互转换

    •String 转 int 两种方式 int a = Integer.parseInt(s);int b = Integer.valueOf(s).intValue(); 代码 public clas ...

  4. Java中String和Int的相互转换

    一.将字串 String 转换成整数 intA. 有2个方法:1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([Strin ...

  5. string与int的相互转换以及把一个字符加入到string的末尾

    #include "stdafx.h" #include<sstream> #include<string> #include<iostream> ...

  6. C++ char*,const char*,string,int 的相互转换

    C++ char*,const char*,string,int 的相互转换   1. string转const char* string s ="abc";const char* ...

  7. std::string和int类型的相互转换(C/C++)

    字符串和数值之前转换,是一个经常碰到的类型转换. 之前字符数组用的多,std::string的这次用到了,还是有点区别,这里提供C++和C的两种方式供参考: 优缺点:C++的stringstream智 ...

  8. C++ 中 string, char*, int 类型的相互转换

    一.int 1.int 转换成 string 1) to_string函数 —— c++11标准增加了全局函数std::to_string: string to_string (int val); s ...

  9. String,Integer,int类型之间的相互转换

    String, Integer, int 三种类型之间可以两两进行转换 1. 基本数据类型到包装数据类型的转换 int -> Integer (两种方法) Integer it1 = new I ...

随机推荐

  1. eclipse添加高版本tomcat问题

    eclipse添加高版本tomcat会报错,提示无法匹配高版本的容器installation is expected 解决方法: 1.找到tomcat的lib目录下的catalina.jar包,用压缩 ...

  2. OJ(Online Judge)

    OJ:它是Online Judge系统的简称,用来在线检测程序源代码的正确性.著名的OJ有RQNOJ.URAL等.国内著名的题库有北京大学题库.浙江大学题库等.国外的题库包括乌拉尔大学.瓦拉杜利德大学 ...

  3. maven 手动构建项目

    maven 手动构建项目 在空目录下面: D:\test>mvn -B archetype:generate -DarchetypeGroupId=org.apache.maven.archet ...

  4. Spring Boot中微信全局token的缓存实现

    为什么要缓存token? 这里的token指的是微信JSAPI中基础支持的ACCESS_TOKEN,并非网页授权ACCESS_TOKEN.网页授权Token每天的调用次数没有限制,不需要缓存. 接口 ...

  5. list.ensureCapacity竟然会变慢

    list.ensureCapacity竟然会变慢 jdk1.8 应该是做了优化了: public class Test10 { public static void main(String[] arg ...

  6. 从头认识Spring-2.3 注解装配-@autowired(4)-required(2)

    这一章节我们来继续具体讨论一下@autowired里面的參数required.在多构造器注入的情况. 1.domain(重点) 蛋糕类: package com.raylee.my_new_sprin ...

  7. MySQL 高可用架构在业务层面的分析研究

    )读多写少 虚线表示跨机房部署,比方电子商务系统.一个Master既有读也有些写.对读数据一致性须要比較重要的.读要放在Master上面. M(R)仅仅是一个备库.仅仅有M(WR)挂了之后,才会切换到 ...

  8. 我和nupt集训队的故事

    纯水文,如有不适请ctrl+w撤离 亚洲赛刚结束.看了不少巨巨的退役贴以及岛娘在知乎上的那篇感天动地的人生经历.多少有点夜深忽梦少年事的错觉.作为一个两年前就打出gg的高龄选手,之后又强行以1次队员和 ...

  9. C - The C Answer (2nd Edition) - Exercise 1-5

    /* Modify the temperature conversion program to print the table in reverse order, that is, from 300 ...

  10. C++ - RTTI(RunTime Type Information)执行时类型信息 具体解释

    RTTI(RunTime Type Information)执行时类型信息 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details ...