相关疑难解决方法(0)

你如何清除stringstream变量?

我已经尝试了几件事,

std::stringstream m;
m.empty();
m.clear();
Run Code Online (Sandbox Code Playgroud)

两者都不起作用.

c++ stringstream

464
推荐指数
6
解决办法
36万
查看次数

如何清除ostringstream

    ostringstream s;

    s << "123";
    cout << s.str().c_str() << endl;

    // how to clear ostringstream here?
    s << "456";
    cout << s.str().c_str() << endl;

输出是:

123
123456

我需要:

123
456

如何重置ostringstream以获得所需的输出?

c++ stream

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

如何重用stringstream

这些线程不回答我:

重置字符串流

你如何清除stringstream变量?

        std::ifstream file( szFIleName_p );
        if( !file ) return false;

        // create a string stream for parsing

        std::stringstream szBuffer;

        std::string szLine;     // current line
        std::string szKeyWord;  // first word on the line identifying what data it contains

while( !file.eof()){

            // read line by line

            std::getline(file, szLine);

            // ignore empty lines

            if(szLine == "") continue;

            szBuffer.str("");
            szBuffer.str(szLine);
            szBuffer>>szKeyWord;
Run Code Online (Sandbox Code Playgroud)

szKeyword将始终包含第一个单词,szBuffer未被重置,无法在任何地方找到关于如何使用stringstream的明确示例.

回答后的新代码:

...
            szBuffer.str(szLine);
            szBuffer.clear();
            szBuffer>>szKeyWord;
...
Run Code Online (Sandbox Code Playgroud)

好的,那是我的最终版本:

        std::string szLine;     // current line
        std::string szKeyWord;  // first word …
Run Code Online (Sandbox Code Playgroud)

c++ parsing text file stringstream

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

stringstream string to int

下面的C++代码执行intstringstringint的转换.然后,它再次重复这些步骤.在stringstreamint线stream1 >> i3; 更是打破了代码.我在这里错过了什么?

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
 int i1 = 101;
 int i2 = 202;
 int i3 = 0;
 string s1 = "";
 string s2 = "";
 stringstream stream1;

 for (int i=0;i<2;i++)
 {

   //int to string
   stream1.str("");
   cout << "i1 " << i1 << endl;
   stream1 << i1;
   s1 = stream1.str();
   cout << "s1 " << s1 << …
Run Code Online (Sandbox Code Playgroud)

c++

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

这些天有什么用于'目的'?

几天前我遇到了一个微妙的错误,代码看起来像这样:

ostringstream ss;
int anInt( 7 );

ss << anInt << "HABITS";
ss << ends;
string theWholeLot = ss.str();
Run Code Online (Sandbox Code Playgroud)

的问题是,ends被粘一个"\ 0"到ostringstream这样theWholeLot本来的样子"7HABITS\0"(即,在端部的空)

现在这没有出现,因为theWholeLot当时被用来使用该const char *部分.string::c_str()这意味着null被掩盖,因为它变成了一个分隔符.但是,当这改变为使用整个字符串时,null突然意味着某些东西和比较,例如:

if ( theWholeLot == "7HABITS" )
Run Code Online (Sandbox Code Playgroud)

会失败的.这让我想到了:据推测,原因ends是回归到ostrstream流通常没有以null结束的时候,并且必须如此str()(然后抛弃而不是stringa char *)才能正常工作.

但是,既然不可能char *从a中删除ostringstream,使用ends不仅是多余的,而是潜在的危险,我正在考虑从客户代码中删除它们.

任何人都可以看到ends在一个std::string唯一的环境中使用的明显理由吗?

c++ stl ostringstream

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

如何清除std :: ostringstream实例以便可以重用它?

可能重复:
如何重用ostringstream?

我一直在使用std::ostringstream转换floatint值到字符串但我无论如何都找不到重用实例.为了说明我的意思,以下是我尝试用来清除流的方法

 #include <iostream>
 #include <sstream>
 using namespace std;

 int main() {
   ostringstream stream;
   stream << "Test";
   cout << stream.str() << endl;  
   stream.flush();                
   stream << "----";
   cout << stream.str() << endl; 
   stream.clear();
   stream << "****";
   cout << stream.str() << endl;
   return 0;
 }
Run Code Online (Sandbox Code Playgroud)

生成输出

 Test
 Test----
 Test----****
Run Code Online (Sandbox Code Playgroud)

这给了我一个问题,因为我不得不创建许多ostringstream浪费的实例.显然clear(),flush()不要做我需要的,所以有办法做到这一点吗?我查看了http://www.cplusplus.com/reference/iostream/ostringstream/上的文档,但似乎没有任何内容可以满足我的需求.有没有办法重置或清除流?

c++ string std stringstream

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

ostringstream和结束

我一直在和其他人的代码一起工作,并注意到在ostringsteam的所有用途中,他们都习惯于明确追加std::ends.

这是我从未做过的事情,从未遇到过问题.

它似乎没有,但应该std::ends在以下代码中有任何区别?

ostringstream message;
message << "Hello world, version " << 2 /* << std::ends ??? */;
printf( "%s\n", message.str().c_str() );
Run Code Online (Sandbox Code Playgroud)

c++ string stream

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

为什么std :: ends导致字符串比较失败?

我昨天花了大约4个小时试图在我的代码中解决这个问题.我将问题简化为下面的示例.

我们的想法是将字符串存储在以std :: ends结尾的字符串流中,然后将其检索并将其与原始字符串进行比较.

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

int main( int argc, char** argv )
{
    const std::string HELLO( "hello" );

    std::stringstream testStream;

    testStream << HELLO << std::ends;

    std::string hi = testStream.str();

    if( HELLO == hi )
    {
        std::cout << HELLO << "==" << hi << std::endl;
    }

    return 0;
}

正如您可能猜到的,上面的代码在执行时不会打印出任何内容.

虽然,如果打印出来,或者在调试器(VS2005)中查看,HELLO和hi看起来相同,它们的.length()实际上相差1.这就是我猜测导致"=="运算符失败的原因.

我的问题是为什么.我不明白为什么std :: ends是一个不可见的字符添加到字符串hi,使hi和HELLO的长度不同,即使它们具有相同的内容.此外,这个看不见的角色不会被增强修剪修剪.但是,如果使用strcmp比较两个字符串的.c_str(),则比较正常.

我首先使用std :: ends的原因是因为我在过去遇到了一些问题,因为stringstream在流的末尾保留了垃圾数据.std :: ends为我解决了这个问题.

c++ boost stl

0
推荐指数
1
解决办法
2218
查看次数

如何清空std :: string?

我的代码的这一部分在一个无限循环中运行并显示一个数字,但我需要在使用后清空该字符串,因为它在循环中运行,它保持相乘.(对不起我的英文,这不是我的母语)

text ---> text text ----> text text text

cartemap << "Carte: " << currentmap;  //cartemap is a std:string currentmap a integer'

MESSAGE1 = TTF_RenderText_Solid( font,  cartemap.str().c_str() , noir );

apply_surface( 70, 70, MESSAGE1, SCREEN );

SDL_FreeSurface(MESSAGE1);
Run Code Online (Sandbox Code Playgroud)

c++ iostream

0
推荐指数
1
解决办法
228
查看次数

标签 统计

c++ ×9

stringstream ×3

stl ×2

stream ×2

string ×2

boost ×1

file ×1

iostream ×1

ostringstream ×1

parsing ×1

std ×1

text ×1