1、

用itoa 和atoi  在头文件#include<cstidlib>

itoa用法:

char *  itoa ( int value, char * str, int base );
value
Value to be converted to a string.
str
Array in memory where to store the resulting null-terminated string.
base
Numerical base used to represent the value as a string, between and , where means decimal base, hexadecimal, octal, and binary.

其中返回值和str是一样的。

atoi用法:

int atoi (const char * str);
/* atoi example */
#include <stdio.h> /* printf, fgets */
#include <stdlib.h> /* atoi */ int main ()
{
int i;
char buffer[];
printf ("Enter a number: ");
fgets (buffer, , stdin);
i = atoi (buffer);
printf ("The value entered is %d. Its double is %d.\n",i,i*);
return ;
}

2、sprintf 在头文件#include<cstdio>

int sprintf ( char * str, const char * format, ... );
Return value:
On success, the total number of characters written is returned. This count does not include the additional null-character automatically appended at the end of the string.
On failure, a negative number is returned.
/* sprintf example */
#include <stdio.h> int main ()
{
char buffer [];
int n, a=, b=;
n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
printf ("[%s] is a string %d chars long\n",buffer,n);
return ;
}

3、字符串流的使用

头文件:#include<sstream>

stringstream strm;             创建自由的stringstream对象
stringstream strm(s); 创建存储s的副本的stringstream对象,其中s是string类型的对象
strm.str();           返回strm中存储打的string类型对象
strm.str(s);           将string类型的s复制给strm,返回void
string line,word;
while(ginline(cin,line))
{
istringstrem strem(line);
while(strem >> word)
{
//do something
}
}

class istream  可用来读取数据

class ostream 可用来写出数据

#include <sstream>
#include <iostream>
using namespace std;
int main ()
{ int val1=, val2=;
ostringstream format_message;
format_message << "val1: " << val1 << endl
<< "val2: " << val2 << endl;
istringstream input_istring(format_message.str());
cout << input_istring.str() << endl;
cout << format_message.str() << endl;
string dump;
int val11,val22;
input_istring >> dump >> val11 >> dump >> val22; cout << val11 << " " << val22 << endl;
return ;
}

最近遇到的C++数字和字符串的转换问题的更多相关文章

  1. c++数字和字符串的转换

    1  利用stringstream 添加头文件 #include<sstream> 数字转字符串 #include <string>   #include <sstrea ...

  2. c++ 中的数字和字符串的转换

    理由:一直忘记数字型的字符串和数字之间的转换,这次总结一下,以便后面再次用到. 其实 C++ 已经给我们写好了相应的函数,直接拿来用即可 QA1:如何把一个数字转换为一个数字字符串?(这个不是很常用) ...

  3. C++中数字和字符串的转换

    1.字符串数字之间的转换 (1)string --> char *   string str("OK");   char * p = str.c_str(); (2)char ...

  4. java 数字转 字符串 互相转换

    各种数字类型转换成字符串型:   String s = String.valueOf( value); // 其中 value 为任意一种数字类型.   字符串型转换成各种数字类型:   String ...

  5. js对数字的处理:取整、四舍五入、数字与字符串的转换

    取整.四舍五入 向下取整Math.floor() 向上取整Math.ceil() 四舍五入Math.round()) 保留有效数位n.toFixed() 产生大于等于0小于1的随机数Math.rand ...

  6. C语言数字与字符串转换 atoi()函数、itoa()函数、sprintf()函数

    在编程中经常需要用到数字与字符串的转换,下面就总结一下. 1.atoi() C/C++标准库函数,用于字符串到整数的转换. 函数原型:int atoi (const char * str); #inc ...

  7. JAVA将数字字符串强制转换成整型变量----求参数之和实验代码(附流程图)

    一.设计思想 先将参数个数输出,并利用循环结果将参数逐个输出,再将字符串强制转化成整型,利用循环结构相加求和 二.程序流程图 三.源程序代码 package demo; public class Co ...

  8. Python数字,字符串

    数字 支持整数,浮点数,和奇怪的类型,如复数. 特殊的运算符为**,表示次方操作,如2**100,表示2的100次方. len()可以得到一个字符串对象的长度,str()可以将数字转换为字符串. pr ...

  9. Freemarker 内置函数 数字、字符串、日期格式化用法介绍

    在用FreeMarker过程中,感觉FreeMarker的字符串,日期,集合等处理能力还是很强大的,上网搜了一些资料,整理如下,以便能帮助大家更熟练的应用Freemarker完成项目开发. 一.Seq ...

随机推荐

  1. Re:从零开始的Linux之路(文件权限)

    基于 Red Hat Enterprise Linux 7.5 或者 CentOS 7.4 基本概念 Linux最核心的一个概念就是:Linux里面任何东西都可以被视为一个文件,包括系统本身(说到底L ...

  2. NFS网络共享服务 挂载参数及优化 内核优化建议

    配置NFS服务端 nfs01上安装软件 [root@nfs01 ~]# yum install nfs-utils rpcbind -y nfs-utils:NFS服务的主程序,包括rpc.nfsd. ...

  3. verilog behavioral modeling --loop statement

    1.forever 2.repeat 3.while 4.for The for statement accomplishes the same results as the following ps ...

  4. PAT Basic 1072

    1072 开学寄语 下图是上海某校的新学期开学寄语:天将降大任于斯人也,必先删其微博,卸其 QQ,封其电脑,夺其手机,收其 ipad,断其 wifi,使其百无聊赖,然后,净面.理发.整衣,然后思过.读 ...

  5. HDU 3506 DP 四边形不等式优化 Monkey Party

    环形石子合并问题. 有一种方法是取模,而如果空间允许的话(或者滚动数组),可以把长度为n个换拓展成长为2n-1的直线. #include <iostream> #include <c ...

  6. CSS 媒体查询 响应式

    媒体查询 从 CSS 版本 2 开始,就可以通过媒体类型在 CSS 中获得媒体支持.如果您曾经使用过打印样式表,那么您可能已经使用过媒体类型.清单 1 展示了一个示例. 清单 1. 使用媒体类型 &l ...

  7. 【03】github的markdown语法

    [03]github的markdown语法 https://guides.github.com/features/mastering-markdown/(下图)(魔芋:已录入)   http://ma ...

  8. luogu1129 [ZJOI2007]矩阵游戏

    其实,只用考虑某一行能否放到某一行就行了 #include <iostream> #include <cstring> #include <cstdio> usin ...

  9. Spark——为数据分析处理提供更为灵活的赋能

    本文来自网易云社区 作者:王佳楠 一.概述 现如今在大规模数据处理分析的技术领域中,Hadoop及其生态内的各功能组件占据了绝对的统治地位.Hadoop原生的MapReduce计算框架由于任务抽象简单 ...

  10. 去掉WordPress顶部工具条

    WordPress为了方便管理员,或者登陆用户快速的从前台进入后台来管理网站,在WordPress网站顶部强制加入了一个工具条(admin bar),而且默认是对所有登陆用户都显示的,有时候看着挺烦心 ...