由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - c++ 中如何把str转换为float?
相关主题
请教关于float的精度和比较a C++ interview question..........
一个c语言的问题question on divide by zero
C的问题,困惑中大家帮我看看这个C程序为什么出错了
请教一道c/c++题问个c++问题
问几个问题c++ pointer conversion question
最新某公司onsite面试题 (转载)sscanf problem in MSVC 7
有关objec access path的问题大家看看这个简单的qsort排序的问题
请问这是什么错误呀谁帮我解释一下这个代码
相关话题的讨论汇总
话题: atof话题: sscanf话题: float话题: value话题: string
进入Programming版参与讨论
1 (共1页)
a***a
发帖数: 92
1
从外部输入时是使用str输入。输入以后想计算,所以想换回float类型。如何写?
T*******i
发帖数: 4992
2
stringstream

【在 a***a 的大作中提到】
: 从外部输入时是使用str输入。输入以后想计算,所以想换回float类型。如何写?
T*****9
发帖数: 2484
3
string s;
float f;
stringstream ss;
ss << s;
ss >> f;

【在 a***a 的大作中提到】
: 从外部输入时是使用str输入。输入以后想计算,所以想换回float类型。如何写?
a***a
发帖数: 92
4
3x a lot!!!

【在 T*****9 的大作中提到】
: string s;
: float f;
: stringstream ss;
: ss << s;
: ss >> f;

T*******i
发帖数: 4992
5
靠,俺先说的

【在 a***a 的大作中提到】
: 3x a lot!!!
a***a
发帖数: 92
6
两个都谢谢!

【在 T*******i 的大作中提到】
: stringstream
f******y
发帖数: 2971
7
atof 在C++里不常用吗?
a***a
发帖数: 92
8
新手。。。学了没几天。。。网上查到有用这个的。。

【在 f******y 的大作中提到】
: atof 在C++里不常用吗?
p***o
发帖数: 1252
9
I always use that, why bother to use xxxxstream ...

【在 f******y 的大作中提到】
: atof 在C++里不常用吗?
f******y
发帖数: 2971
10
hehe, i always use that too. But it seems C++ prefer xxxxstream. I guess it
is just like printf and cout.
相关主题
最新某公司onsite面试题 (转载)a C++ interview question..........
有关objec access path的问题question on divide by zero
请问这是什么错误呀大家帮我看看这个C程序为什么出错了
进入Programming版参与讨论
p***o
发帖数: 1252
11
There is no such preference as far as I know.
I usually found printf is more readable than cout.

it

【在 f******y 的大作中提到】
: hehe, i always use that too. But it seems C++ prefer xxxxstream. I guess it
: is just like printf and cout.

r****o
发帖数: 1950
12
stringstream is dealing with strings, and string is more convenient than
char,

【在 p***o 的大作中提到】
: I always use that, why bother to use xxxxstream ...
p***o
发帖数: 1252
13
You don't get it. Convert std::string to const char * is just one function
call. atof is the other and that's all.
Why bother to create/name an xxxxstream object, send in the string and
call the overloaded operator >>?

【在 r****o 的大作中提到】
: stringstream is dealing with strings, and string is more convenient than
: char,

t****t
发帖数: 6806
14
if you know you have char* and you want to double, then it is easier to use
atof
but stream works with template. e.g. you may say
template
void convert_to_whatever(const string& s, T& value)
{
istringstream iss(s);
iss>>value;
}
a good programmer can choose whatever tool that best fits the situation.

【在 p***o 的大作中提到】
: You don't get it. Convert std::string to const char * is just one function
: call. atof is the other and that's all.
: Why bother to create/name an xxxxstream object, send in the string and
: call the overloaded operator >>?

p***o
发帖数: 1252
15
Yeah, but hardly to believe a guy knows how to write convert_to_whatever
will ask that question. And I would specialize the template to use atof
for double (need to change your design though).

use

【在 t****t 的大作中提到】
: if you know you have char* and you want to double, then it is easier to use
: atof
: but stream works with template. e.g. you may say
: template
: void convert_to_whatever(const string& s, T& value)
: {
: istringstream iss(s);
: iss>>value;
: }
: a good programmer can choose whatever tool that best fits the situation.

r*******y
发帖数: 290
16
atof has its limitations:
http://www.cplusplus.com/reference/clibrary/cstdlib/atof.html
double atof ( const char * str );
Return Value
On success, the function returns the converted floating point number as a
double value.
If no valid conversion could be performed, or if the correct value would
cause underflow, a zero value (0.0) is returned.
If the correct value is out of the range of representable values, a positive
or negative HUGE_VAL is returned.

【在 p***o 的大作中提到】
: Yeah, but hardly to believe a guy knows how to write convert_to_whatever
: will ask that question. And I would specialize the template to use atof
: for double (need to change your design though).
:
: use

w***g
发帖数: 5958
17
C++ is going to be dead if converting from string to float takes this effort
. This is obviously not simple enough to be a good method.

【在 T*****9 的大作中提到】
: string s;
: float f;
: stringstream ss;
: ss << s;
: ss >> f;

h*******e
发帖数: 225
18
Please differentiate between a language and a library.

effort

【在 w***g 的大作中提到】
: C++ is going to be dead if converting from string to float takes this effort
: . This is obviously not simple enough to be a good method.

n*e
发帖数: 50
19
这也太麻烦了吧?
有没有更快的方法?

【在 T*****9 的大作中提到】
: string s;
: float f;
: stringstream ss;
: ss << s;
: ss >> f;

r*********r
发帖数: 3195
20
可以用boost 的 lexical_cast("12.34").
它内部用stringstream 和 <<,>> 实现。
相关主题
问个c++问题大家看看这个简单的qsort排序的问题
c++ pointer conversion question谁帮我解释一下这个代码
sscanf problem in MSVC 7c++ template中如何判断类型
进入Programming版参与讨论
r*********r
发帖数: 3195
21
what do u mean?
n*e
发帖数: 50
22
虽然你调用了一个已有的function, 但是那个function本身绕道太远。

【在 r*********r 的大作中提到】
: what do u mean?
r*********r
发帖数: 3195
23
well, you have to put the usage in the right context.
if you are parsing millions of strings into floats (maybe
in high throughput networking environment?), then yeah,
atof or sscanf run much faster.
if you are only parsing the user input in an interactive
environment, why do you even care the complexity of the function
call?
n*e
发帖数: 50
24
I agree.

【在 r*********r 的大作中提到】
: well, you have to put the usage in the right context.
: if you are parsing millions of strings into floats (maybe
: in high throughput networking environment?), then yeah,
: atof or sscanf run much faster.
: if you are only parsing the user input in an interactive
: environment, why do you even care the complexity of the function
: call?

r*********r
发帖数: 3195
25
这些function的适用性和性能成反比。
atof 只能用在 float/double。
sscanf 只能用在 integer, float.
lexical_cast 则可以使用在任何有operator>>定义的类型。
从性能上看,我猜的是 sscanf 比 lexical_cast 快5到10倍。
atof 又比 sscanf 快5倍左右。
还有,sscanf 是最容易用错的一个函数,对初学者简直是噩梦。
w***g
发帖数: 5958
26
请展开说说sscanf有什么要注意的。我用这个函数很多,但是因为写的不是什么正经程
序,所以一直没出过问题。

【在 r*********r 的大作中提到】
: 这些function的适用性和性能成反比。
: atof 只能用在 float/double。
: sscanf 只能用在 integer, float.
: lexical_cast 则可以使用在任何有operator>>定义的类型。
: 从性能上看,我猜的是 sscanf 比 lexical_cast 快5到10倍。
: atof 又比 sscanf 快5倍左右。
: 还有,sscanf 是最容易用错的一个函数,对初学者简直是噩梦。

r*********r
发帖数: 3195
27
the signature of sscanf is:
int sscanf(const char *, const char *,...)
the arguments are supposed to be passed by their address,
but most beginners often forget to use the "&" operator.
the compiler won't complain since there's no type checking at all.
running the executable will cause a segmentation fault.
another funny fact is that if you switch the string to be scanned
and the format string, it still runs! like the following:
float x = 4.3;
...
sscanf("%f", "12.34", &x);
the x will still be 4
c*****t
发帖数: 1879
28
beginner...什么事情到了 beginner 的手里都有可能发生。。。
gcc 的 -Wall warning 对 printf, scanf 都很管用。
另外 sscanf 不是还有个 return value 你怎么不查一下。里面告诉你
有几个成功 assign 的。

【在 r*********r 的大作中提到】
: the signature of sscanf is:
: int sscanf(const char *, const char *,...)
: the arguments are supposed to be passed by their address,
: but most beginners often forget to use the "&" operator.
: the compiler won't complain since there's no type checking at all.
: running the executable will cause a segmentation fault.
: another funny fact is that if you switch the string to be scanned
: and the format string, it still runs! like the following:
: float x = 4.3;
: ...

r*********r
发帖数: 3195
29
ft, 谁不是从 beginner 过来的。。。
我记得以前就经常忘了用&.
那时候用turbo c, 不记得有没有类似 -Wall 的东西。

【在 c*****t 的大作中提到】
: beginner...什么事情到了 beginner 的手里都有可能发生。。。
: gcc 的 -Wall warning 对 printf, scanf 都很管用。
: 另外 sscanf 不是还有个 return value 你怎么不查一下。里面告诉你
: 有几个成功 assign 的。

r*********r
发帖数: 3195
30
现在的c++初学者,根本没必要去折腾那些c的库函数。
甚至连指针也可以放到最后再学。否则cs101 只会被java占据。
相关主题
C code参数传递出错可能的原因一个c语言的问题
一个dot net浮点运算的问题C的问题,困惑中
请教关于float的精度和比较请教一道c/c++题
进入Programming版参与讨论
t****t
发帖数: 6806
31
怎么能拿早年间东西的标准来要求现在的编译器.

【在 r*********r 的大作中提到】
: ft, 谁不是从 beginner 过来的。。。
: 我记得以前就经常忘了用&.
: 那时候用turbo c, 不记得有没有类似 -Wall 的东西。

r*********r
发帖数: 3195
32
cpu 和 内存都涨了 1000 倍不止。
编程工具(compiler,library,debugger etc)进步很小,
感觉有新意的就只是stl和boost的一些东西。
m*****e
发帖数: 4193
33
no pointer? then you'd better just learn java instead

【在 r*********r 的大作中提到】
: 现在的c++初学者,根本没必要去折腾那些c的库函数。
: 甚至连指针也可以放到最后再学。否则cs101 只会被java占据。

r*********r
发帖数: 3195
34
the error condition checking of c functions is so inconsistent.
in the case of atoi, atof, a return value of 0 simply doesn't tell whether
there is an scanning error or a 0 is correctly scanned.

【在 c*****t 的大作中提到】
: beginner...什么事情到了 beginner 的手里都有可能发生。。。
: gcc 的 -Wall warning 对 printf, scanf 都很管用。
: 另外 sscanf 不是还有个 return value 你怎么不查一下。里面告诉你
: 有几个成功 assign 的。

1 (共1页)
进入Programming版参与讨论
相关主题
谁帮我解释一下这个代码问几个问题
c++ template中如何判断类型最新某公司onsite面试题 (转载)
C code参数传递出错可能的原因有关objec access path的问题
一个dot net浮点运算的问题请问这是什么错误呀
请教关于float的精度和比较a C++ interview question..........
一个c语言的问题question on divide by zero
C的问题,困惑中大家帮我看看这个C程序为什么出错了
请教一道c/c++题问个c++问题
相关话题的讨论汇总
话题: atof话题: sscanf话题: float话题: value话题: string