如何从stringstream中提取double

时间:2017-07-09 19:03:27

标签: c++ double stringstream

在处理我的C ++项目时,我在使用stringstream个对象时发现了一些奇怪的事情:我无法使用doublestringstream中提取>> #include <string> #include <sstream> int main(int argc, const char * argv[]) { std::string string("1000 ; 523277527397538 ; 0.183 ; 0.453 ; 0.5 ; 0.5 ; 0.033 ; 0 ; 0 ;"); std::stringstream stringstream(string); int integer; char character; double doubleprec; stringstream >> integer >> character; stringstream >> integer >> character; stringstream >> doubleprec >> character; stringstream >> doubleprec >> character; return 0; }

考虑这个例子:

integer

使用我的调试器我注意到变量1000首先取值523277527397538,然后取值doubleprec(正如我所料),但是0始终使用值{{1}}。

为什么?我错过了有关流如何工作的内容吗?

2 个答案:

答案 0 :(得分:3)

只有第一个流输出语句:

def RCL(feed_forward_input,num_of_filter, filtersize):
    conv = Conv2D(filters=num_of_filter, kernel_size=filtersize, padding='same')
    recurrent_input = conv(feed_forward_input)
    merged = merge([feed_forward_input,recurrent_input], mode='sum')
    conv_relu = relu(merged)
    conv_relu_batchnorm = BatchNormalization()(conv_relu)
    return conv_relu_batchnorm

成功。剩下的三个不是因为你的int变量不能保持stringstream >> integer >> character; 的值。将其更改为523277527397538

long long

当流提取未能将long long integer; 数字文字放入您的变量时,则设置字符串流的failbit,并且后续对字符串流提取的调用也会失败。一种好的方法是在使用流IO时使用523277527397538语句:

if

答案 1 :(得分:2)

快速演示以加强Ron的回答:(c ++ 14)

#include <string>
#include <sstream>
#include <iostream>


int main(int argc, const char * argv[]) {


    std::string string("1000 ; 523277527397538 ; 0.183 ; 0.453 ; 0.5 ; 0.5 ; 0.033 ; 0 ; 0 ;");
    std::stringstream stringstream(string);

    auto okfail = [](auto&& b) {
        if (not b) return std::string("fail");
        return std::string("ok");
    };

    auto read = [&](auto&& var, auto&& f) {
        std::cout << "reading " << var << ". Stream is " << okfail(stringstream) << std::endl;
        f();
        std::cout << "read " << var << ". Stream is " << okfail(stringstream) << std::endl;
    };

    int integer1;
    int integer2;
    char character;
    double doubleprec1, doubleprec2;
    read("integer1", [&] { stringstream >> integer1 >> character; });
    read("integer2", [&] { stringstream >> integer2 >> character; });
    read("doubleprec1", [&] { stringstream >> doubleprec1 >> character; });
    read("doubleprec2", [&] { stringstream >> doubleprec2 >> character; });

    std::cout << integer1<< " " << integer2 << " " << doubleprec1 << " " << doubleprec2 << " " << std::endl;
    return 0;
}

示例输出(imac 64位):

reading integer1. Stream is ok
read integer1. Stream is ok
reading integer2. Stream is ok
read integer2. Stream is fail
reading doubleprec1. Stream is fail
read doubleprec1. Stream is fail
reading doubleprec2. Stream is fail
read doubleprec2. Stream is fail
1000 2147483647 0 0 

现在将integer2更改为long int:

reading integer1. Stream is ok
read integer1. Stream is ok
reading integer2. Stream is ok
read integer2. Stream is ok
reading doubleprec1. Stream is ok
read doubleprec1. Stream is ok
reading doubleprec2. Stream is ok
read doubleprec2. Stream is ok
1000 523277527397538 0.183 0.453