最近遇到的C++数字和字符串的转换问题
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++数字和字符串的转换问题的更多相关文章
- c++数字和字符串的转换
1 利用stringstream 添加头文件 #include<sstream> 数字转字符串 #include <string> #include <sstrea ...
- c++ 中的数字和字符串的转换
理由:一直忘记数字型的字符串和数字之间的转换,这次总结一下,以便后面再次用到. 其实 C++ 已经给我们写好了相应的函数,直接拿来用即可 QA1:如何把一个数字转换为一个数字字符串?(这个不是很常用) ...
- C++中数字和字符串的转换
1.字符串数字之间的转换 (1)string --> char * string str("OK"); char * p = str.c_str(); (2)char ...
- java 数字转 字符串 互相转换
各种数字类型转换成字符串型: String s = String.valueOf( value); // 其中 value 为任意一种数字类型. 字符串型转换成各种数字类型: String ...
- js对数字的处理:取整、四舍五入、数字与字符串的转换
取整.四舍五入 向下取整Math.floor() 向上取整Math.ceil() 四舍五入Math.round()) 保留有效数位n.toFixed() 产生大于等于0小于1的随机数Math.rand ...
- C语言数字与字符串转换 atoi()函数、itoa()函数、sprintf()函数
在编程中经常需要用到数字与字符串的转换,下面就总结一下. 1.atoi() C/C++标准库函数,用于字符串到整数的转换. 函数原型:int atoi (const char * str); #inc ...
- JAVA将数字字符串强制转换成整型变量----求参数之和实验代码(附流程图)
一.设计思想 先将参数个数输出,并利用循环结果将参数逐个输出,再将字符串强制转化成整型,利用循环结构相加求和 二.程序流程图 三.源程序代码 package demo; public class Co ...
- Python数字,字符串
数字 支持整数,浮点数,和奇怪的类型,如复数. 特殊的运算符为**,表示次方操作,如2**100,表示2的100次方. len()可以得到一个字符串对象的长度,str()可以将数字转换为字符串. pr ...
- Freemarker 内置函数 数字、字符串、日期格式化用法介绍
在用FreeMarker过程中,感觉FreeMarker的字符串,日期,集合等处理能力还是很强大的,上网搜了一些资料,整理如下,以便能帮助大家更熟练的应用Freemarker完成项目开发. 一.Seq ...
随机推荐
- cvs 文件无法上传debug
当时文件始终上传不成功时(一般先update后commit): cvs update filename report:move away filename ,it is in the way cvs ...
- Day15模块(导入,使用)
Day15模块 什么是模块: 一系列功能的集合体 一个py文件就是一个模块 aaa.py就是aaa模块 模块四种形式: 内置的模块 py文件 第三方的 包 模块三种来源:内置的,第三方,自定义 为什么 ...
- JavaScript正则表达式-字符类
字符列表 在方括号内指定一个或者多个字符组成的字符列表,与字符列表中任意字符匹配,都被认为是匹配的.每次匹配只能匹配列表中的一个字符. str = "bird,head,fed,meadow ...
- 解决php7.3 configure: error: off_t undefined
//解决该问题:php7.3 configure: error: off_t undefined; check your library configuration # 添加搜索路径到配置文件echo ...
- checkStyle使用手册
1. Annotations(注解:5个) Annotation Use Style(注解使用风格) 这项检查可以控制要使用的注解的样式. Missing Deprecated(缺少deprecad) ...
- Debian 修改时间时区
http://blog.51cto.com/zhujiangtao/1554976 第一种图形化方面推荐使用 第二种修改文件的形式 只是在当前的terminal生效 笔者使用的是: debian9.3
- Eclipse截取android报错log
Eclipse截取android报错log: 1.前提条件:已安装eclipse 2. LogCat界面设置: Logcat是Android 编程中一个命令行工具,可以用于得到程序的 log 信息,可 ...
- Result实现类
package org.apache.struts2.dispatcher; import com.opensymphony.xwork2.ActionInvocation; import com.o ...
- “玲珑杯”ACM比赛 Round #13 B -- 我也不是B,倍增+二分!
B 我也不是B 这个题做了一下午,比赛两个小时还是没做出来,比完赛才知道要用一个倍增算法确定区间,然后再二分右端点. 题意:定义一个序列的混乱度为累加和:b[i]*v[i],b[i]为这个序 ...
- 洛谷P3327 - [SDOI2015]约数个数和
Portal Description 共\(T(T\leq5\times10^4)\)组数据.给出\(n,m(n,m\leq5\times10^4)\),求\[\sum_{i=1}^n\sum_{j= ...