标签: stringstream

num_get facet和stringstream转换为boolean - 失败并初始化布尔值?

我继承了一个模板来将字符串转换为数值,并希望将其应用于转换为布尔值.我对stringstream和locale类不太熟悉.我似乎得到了一些奇怪的行为,我想知道是否有人可以向我解释一下?

template<typename T> T convertFromString( const string& str ) const {

std::stringstream SStream( str );  
T num = 0; 
SStream >> num;

return num;  
}  
Run Code Online (Sandbox Code Playgroud)

这工作正常,直到我尝试布尔转换

string str1("1");
int val1 = convertFromString<int>(str1); // ok
string str2("true");
bool val2 = convertFromString<bool>(str2); // val2 is _false_
Run Code Online (Sandbox Code Playgroud)

我花了一些时间来追查问题.我已经确认locale的truename()返回"true".

问题似乎是变量num的初始化.我可以将模板更改为此,它可以工作:

template<typename T> T convertFromString( const string& str ) const {

std::stringstream SStream( str );  
T num;  // <----------------------- Changed here
SStream >> num;

return num;  
}  
string str2("true");
bool val2 …
Run Code Online (Sandbox Code Playgroud)

c++ templates initialization std stringstream

2
推荐指数
1
解决办法
1625
查看次数

避免从字符串流中抓取任何内容

我正在为一个非常基本的ISA工作.目前我正在实现解析器函数,我正在使用字符串流来从行中获取单词.这是汇编代码的示例:

; This program counts from 10 to 0
        .ORIG x3000
        LEA R0, TEN     ; This instruction will be loaded into memory location x3000
        LDW R1, R0, #0
START   ADD R1, R1, #-1
        BRZ DONE
        BR  START
                        ; blank line
DONE    TRAP    x25     ; The last executable instruction
TEN     .FILL   x000A   ; This is 10 in 2's comp, hexadecimal
        .END
Run Code Online (Sandbox Code Playgroud)

不要担心汇编代码的性质,只需看第3行,右边有注释的那一行.我的解析器功能不完整,但这就是我所拥有的:

// Define three conditions to code
enum {DONE, OK, EMPTY_LINE};
// Tuple containing a condition and a string vector …
Run Code Online (Sandbox Code Playgroud)

c++ string parsing stringstream

2
推荐指数
1
解决办法
284
查看次数

'stringstream'参数命令麻烦

我遇到了一个奇怪的问题stringstream.

#include "stdafx.h"
#include "iostream"
#include "sstream"

using namespace std;

struct Test
{
    float f;
};

wstringstream& operator <<( wstringstream& sstream , const Test& test )
{
    sstream << test.f;
    return sstream;
}

int _tmain(int argc, _TCHAR* argv[])
{
    Test a;
    a.f = 1.2f;

    wstringstream ss;
    ss << L"text" << a << endl; // error C2679!
    ss << a << L"text" << endl; // it works well..

    getchar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

问题出在这里:

ss << L"text" << a << …
Run Code Online (Sandbox Code Playgroud)

c++ stringstream visual-studio-2008-sp1 visual-studio

2
推荐指数
1
解决办法
474
查看次数

snprintf vs std :: stringstream

我正在努力消除SECURITY CODING产品中的违规行为.我的代码有很多sprintf,覆盖率工具建议我使用snprintf,但C++也有std::stringstream.用它std::stringstream代替是一个好主意snprintf

c++ printf stringstream secure-coding

2
推荐指数
1
解决办法
1675
查看次数

C++ std :: stringstream >> std :: string只存储第一个条目?

可能重复:
如何强制std :: stringstream operator >>读取整个字符串?

我试图将结构转换为字符串,类似于toString()将如何用于Java或C++中的对象.为此,我将格式化数据写入std :: stringstream,然后将其写入std :: string.

这是我有的:

std::stringstream ss;
std::string       packet;

ss <<  "Packet Length: " << p->header->len
   << " (" << p->header->caplen << ")" << std::endl
   << "Collected: " <<  timedate << "."
   << std::dec << p->header->ts_usecs << std::endl
   << "Eth:\tFrom: " << to_address(p->from) << std::endl
   <<     "\tTo:   " << to_address(p->to)  << std::endl
   <<     "\tType: " << to_hex(p->type, false)
   << " (" << p->type_name << ")" << std::endl;
Run Code Online (Sandbox Code Playgroud)

但出于某种原因,当我将此流写入std :: string数据包时:

ss >> packet; …
Run Code Online (Sandbox Code Playgroud)

c++ stringstream

2
推荐指数
1
解决办法
2386
查看次数

C++ stringstream

执行此操作时,stringstream如何工作:

stringstream ss;
ss << "123" << "abc";
Run Code Online (Sandbox Code Playgroud)

它是否会创建一个一次性"123abc"字符串,或者它是否会连续执行两个字符串流操作?

我想复制那个功能,但没有重载我似乎使用两个参数,如上面的代码...

c++ string stringstream

2
推荐指数
2
解决办法
1298
查看次数

使用字符数组作为字符串流缓冲区

我正在寻找一种干净的STL方式来使用现有的C缓冲区(char*和size_t)作为字符串流.我更喜欢使用STL类作为基础,因为它具有内置的安全措施和错误处理.

注意:我不能使用额外的库(否则我会使用QTextStream)

c++ stl stringstream

2
推荐指数
1
解决办法
2331
查看次数

为什么stoi比没有-O3的stringstream慢得多?

今天我在谈论C++ 11中的新闻,如线程,to_string和stoi.

但事实上,所有这些在C++ 98中已经成为可能.

然后我决定比较旧库和新闻库:

C++ 11:

g ++ -std = c ++ 11 main.cpp

#include <iostream>
#include <string>
#include <ctime>

using namespace std;

int main()
{
    clock_t tStart = clock();
    string input = "50";

    for (int i = 0; i < 50000; i++)
    {
        int number = stoi(input);
    }

    cout << (double)(clock() - tStart) << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

C++ 98

g ++ main.cpp

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

using namespace std;

int main()
{
    clock_t …
Run Code Online (Sandbox Code Playgroud)

c++ stringstream c++11 c++98

2
推荐指数
1
解决办法
711
查看次数

如何读取标准输入到std :: stringstream直到Ctrl-D(传输结束)?

如何对多行进行标准输入并将其存储到a中,std::streamstring直到用户按下Ctrl-D以指示输入结束?我想这样做这样,但对于输入的变量线.如何Ctrl-D通过用户按下的信号检查传输信号的结束?

    string std_input;
    stringstream stream(std_input);
    while (getline(cin, std_input))
        stream(std_input);     
Run Code Online (Sandbox Code Playgroud)

c++ stringstream

2
推荐指数
1
解决办法
1040
查看次数

创建一个字符串流重复项

我知道我无法复制a stringstream,但是我想知道是否可以复制它,以便可以使用相似的输出说明符创建两个字符串。

像这样

std::stringstream s1;
s1 << std::scientific << std::setprecision(4);
s1 << 0.01;

// Later on I want to create s2 given s1
std::stringstream s2;
// Copy formatting specifiers from s1 to s2, so that the effect is 
// s2 << std::scientific << std::setprecision(4);
s2 << 0.02;
Run Code Online (Sandbox Code Playgroud)

我的原因是我正在编写一个接受字符串流的函数,并且我想在修改字符串流之前了解某些输出的宽度。

我已经看完了stringstream这里的大部分问题,但找不到这个特定案例的答案。

c++ std stringstream

2
推荐指数
1
解决办法
40
查看次数