标签: stringstream

如何读取动态大小的stringstream?

我想尝试使用stringstream进行分配,但我对它的工作原理有点困惑.我做了一个快速搜索,但找不到任何能回答我问题的东西.

假设我有一个动态大小的流,我怎么知道何时停止写入变量?

 string var = "2 ++ asdf 3 * c";
 stringstream ss;

 ss << var;

 while(ss){
  ss >> var;
  cout << var << endl;
 }
Run Code Online (Sandbox Code Playgroud)

我的输出将是:

2  
++  
asdf  
3  
*  
c  
c  
Run Code Online (Sandbox Code Playgroud)

我不知道为什么我最后得到额外的'c',特别是因为_M_in_cur = 0x1001000d7""

c++ stringstream

9
推荐指数
1
解决办法
1万
查看次数

如何在使用字符串流时停止双精度转换为科学记数法

我正在创建一个函数来返回十进制和整数位数,并typename使用sstreams 将插入的数字转换为字符串.

但是,转换为字符串时的数字用科学记数法表示,这对于计算正常数字中的数字位数没有用.如何在下面的函数中阻止这种情况发生?

enum { DECIMALS = 10, WHOLE_NUMBS = 20, ALL = 30 };

template < typename T > int Numbs_Digits(T numb, int scope)
{
    stringstream ss(stringstream::in | stringstream::out);
    stringstream ss2(stringstream::in | stringstream::out);
    unsigned long int length = 0;
    unsigned long int numb_wholes;

    ss2 << (int) numb;
    numb_wholes = ss2.str().length();
    ss2.flush();
    bool all = false;

    switch (scope) {
    case ALL:
        all = true;

    case DECIMALS:
        ss << numb;
        length += ss.str().length() - (numb_wholes + 1); …
Run Code Online (Sandbox Code Playgroud)

c++ scientific-notation stringstream

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

Stringstream提取整数

为什么我无法将一个整数值提取到Num变量中?

#include <sstream>
#include <vector>
#include <iostream>

using namespace std;

int main()
{
    string Digits("1 2 3");
    stringstream ss(Digits);
    string Temp;
    vector<string>Tokens;

    while(ss >> Temp)
        Tokens.push_back(Temp);

    ss.str(Tokens[0]);

    int Num = 0;
    ss >> Num;
    cout << Num;    //output: 0
}
Run Code Online (Sandbox Code Playgroud)

c++ stringstream

8
推荐指数
3
解决办法
1万
查看次数

有没有办法减少ostringstream malloc/free?

我正在写一个嵌入式应用程序.在某些地方,我经常使用std :: ostringstream,因为它对我来说非常方便.但是,我刚刚发现性能受到了极大的影响,因为向流中添加数据会导致大量调用malloc和free.有什么办法可以避免吗?

我的第一个想法是使ostringstream静态并使用ostringstream :: set("")重置它.但是,由于我需要函数可重入,因此无法完成此操作.

c++ memory stl stringstream ostringstream

8
推荐指数
1
解决办法
3483
查看次数

使用ostringstream整理浮动

我有一个关于使用ostringstream从float转换为c ++字符串的问题.这是我的路线:

void doSomething(float t)
{
    ostringstream stream; 
    stream << t;
    cout << stream.str();
}
Run Code Online (Sandbox Code Playgroud)

当t的值为-0.89999时,它会向下舍入为-0.9,但是当它的值为0.0999或小于此值时为1.754e-7时,它只是打印而不是舍入.什么可以解决这个问题.

c++ stringstream rounding ostringstream

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

我可以判断一个std :: string是否使用stringstream表示一个数字?

显然,这可以用于显示字符串是否为数字,例如"12.5"== yes,"abc"== no.但是我得到了一个没有注意的输入.

std::stringstream ss("2");
double d; ss >> d;
if(ss.good()) {std::cout<<"number"<<std::endl;}
else {std::cout<<"other"<<std::endl;}
Run Code Online (Sandbox Code Playgroud)

c++ numbers words stringstream

8
推荐指数
3
解决办法
8566
查看次数

stringstream-> rdbuf() - > pubsetbuf没有设置缓冲区

我试图修改stringstream对象的stringbuffer而不必复制字符串,使用方法pubsetbuf,但它无法正常工作.我正在关注http://www.cplusplus.com/reference/iostream/streambuf/pubsetbuf/中的文档.这是我的示例代码:

#include <iostream>
#include <sstream>

int main(int argc, char* argv[])
{
    std::stringstream stream("You say goodbye");
    char replace[] = {"And I say hello"};
    std::cout << stream.str() << std::endl; // Checking original contents
    stream.rdbuf()->pubsetbuf(replace, 16); // Should set contents here
    std::cout << stream.str() << std::endl; // But don't :(
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

You say goodbye
You say goodbye
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用stream.str(替换),但是这个方法复制'replace'的值,我不想复制.

我错过了什么?

更新:我正在使用VS2010

c++ stl stringstream stringbuffer

8
推荐指数
1
解决办法
2882
查看次数

在cout中禁用逗号?

在我正在进行的项目中,我链接到一个专有的动态库.一旦我运行库的initialize功能,记录和打印数字的行为就会改变.

逗号每三位小数插入一次.IE浏览器.

cout << 123456789 << endl
Run Code Online (Sandbox Code Playgroud)

用于打印出来123456789,现在打印出来123,456,789.这非常令人讨厌,因为这种行为不是我想要的.

经过一些研究后,我怀疑是一个地方问题.我在调用initialize函数后尝试使用这行代码

setlocale(LC_ALL,"C");
Run Code Online (Sandbox Code Playgroud)

认为它可能会将我的本地重置为默认值; 但无济于事.逗号坚持!!

我错过了什么?

我在这里发布了一个相关的关注点.

c++ locale cout stringstream comma

8
推荐指数
1
解决办法
681
查看次数

如何初始化stringstream对象

我有一个stringstream对象,我想知道如何初始化它.

stringstream os;
for(int i = 0; i < 10; ++i){
        value = rand() % 100;
        os<<value;
        cout<<os.str()<<" "<<os<<endl;
        ntree->insert(os.str());
        //I want my os object to be reset here
    }
Run Code Online (Sandbox Code Playgroud)

c++ stringstream

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

strstream可以直接使用标准字符串的缓冲区吗

鉴于strstream有以下构造函数:

strstream( char* s, int n, std::ios_base::openmode mode);
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以直接在“只读”模式下使用标准字符串底层的缓冲区,以避免缓冲区重新分配产生奇怪的副作用:

std::string buffer("Dummy Data");
std::strstream ss(
  const_cast<char *>(buffer.data()), buffer.length(), std::ios_base::in);

std::string other;
ss >> other;
std::cout << other << std::endl;
Run Code Online (Sandbox Code Playgroud)

令我惊讶的是,上面的代码最终生成了other一个空字符串。在文档中有一个示例,其中C 数组用作 的静态缓冲区strstream,所以我想知道它不能使用缓冲区内容的原因是什么std::string

注意:我无法使用,std::stringstream因为相关平台的 VS 实现仅限于 4GB 大小,如此处所述


更新:

我发现使用std::ios_base::app(如示例中所示)几乎可以使我的代码正常工作。现在最大的问题(根据我收到的评论)是它strstream已被长期弃用。也许boost iostreams中有一些东西可以提供帮助,但我不太了解该库。

c++ stringstream c++11

8
推荐指数
1
解决办法
189
查看次数