string -> int 及 stringstream 用法和注意点
2009-11-21 16:20
246 查看
首先介绍3个使用广泛的string->int方法,及被支持的开发平台
// stringstream gcc + vc +
#include <sstream>
string str("123");
std::stringstream ss;
ss << str;
int c;
ss >> c;
//atoi gcc - vc+
#include <string>
string str="12345";
int b=atoi(str.c_str());
// 需boost lib 没用过
#include <lexical_cast>
lexical_cast intValue = lexical_cast<int>(strValue) ;
现在具体谈一下,stringstream class 方法,该方法也可以实现到char*,bool等等及其你过程。
当使用stringstream的时候需要注意两点
1.多次转换,转换之间需要调用stringstream.clear函数
std::stringstream s1;
s1>>choices;
s1<<ps[1];
s1.clear();
s1<<ps[2];
s1>>blanks;
否则,变量被赋随机值
2.多次转换的时候,不要初始化instance
std::stringstream s1(ps[1]);
s1>>choices;
s1.clear();
s1<<ps[2];
s1>>blanks;
否则,clear()无效
参考文献: stringstream的用法 http://www.1-100.org/other/35899.htm
// stringstream gcc + vc +
#include <sstream>
string str("123");
std::stringstream ss;
ss << str;
int c;
ss >> c;
//atoi gcc - vc+
#include <string>
string str="12345";
int b=atoi(str.c_str());
// 需boost lib 没用过
#include <lexical_cast>
lexical_cast intValue = lexical_cast<int>(strValue) ;
现在具体谈一下,stringstream class 方法,该方法也可以实现到char*,bool等等及其你过程。
当使用stringstream的时候需要注意两点
1.多次转换,转换之间需要调用stringstream.clear函数
std::stringstream s1;
s1>>choices;
s1<<ps[1];
s1.clear();
s1<<ps[2];
s1>>blanks;
否则,变量被赋随机值
2.多次转换的时候,不要初始化instance
std::stringstream s1(ps[1]);
s1>>choices;
s1.clear();
s1<<ps[2];
s1>>blanks;
否则,clear()无效
参考文献: stringstream的用法 http://www.1-100.org/other/35899.htm
相关文章推荐
- 二叉树的所有路径,因为有些IDE不支持itoa方法,自定义的int->stringstream->string,
- <C++>istringstream, ostringstream, stringstream的用法
- IOS Number 处理(int-->NSNumber,NSNumber-->nsinteger,string -->double,CGFloat --> dobule)
- IOS Number 处理(int-->NSNumber,NSNumber-->nsinteger,string -->double,CGFloat --> dobule)
- Func<int, int, string>返回string
- map<string,map<string,string>>:map中map用法示例
- js string 转 int 注意的问题——parseInt
- javascript——js string 转 int 注意的问题——parseInt
- string.c_str()用法与注意事项 及string::copy()与string.data()
- IOS Number 处理(int-->NSNumber,NSNumber-->nsinteger,string -->double,CGFloat --> dobule)
- C++用map记录字符串,怎么用map<string,int>才会不超时
- CharsRefIntHashMap并不比HashMap<String, Integer>快
- 关于jstl EL用法的注意点(java.lang.NumberFormatException: For input string: "userName")
- qtcpsocket qtcpserver,在用block写数据包的时候,注意">>"和"<<"用法
- java中的String转int需要注意的问题
- Wpf TextBox 控件如何绑定 Dictionary <int, string>
- String的Split方法的用法与要注意事项
- <<< List<HashMap<String, Object>> 及 HashMap<String, Object> 的用法
- java代码----数据类型的转换-----int --->String
- [小白]java:日期对象转换用法(string,int)