标签: stringstream

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
查看次数

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
查看次数

忽略stringstream中的值(如sscanf中的%*f)

我正试图摆脱旧的不安全的C函数,包括sscanf().现在我正在使用

#include <sstream>
std::string str = "111 222.2 333 444.4 555";
std::stringstream sstr(str);
int i, j, k;
float dummy1, dummy2;
sstr >> i >> dummy1 >> j >> dummy2 >> k;  
Run Code Online (Sandbox Code Playgroud)

我只需要那个整数.有没有办法避免那些讨厌的虚拟变量?

在此先感谢您,祝您度过愉快的一天!

c++ stringstream

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

std :: basic_stringstream <unsigned char>将无法使用MSVC 10进行编译

我试图让UTF-8字符与ANSI 8位字符共存.我的策略是表示utf-8字符,unsigned char以便可以对两种字符类型使用适当的函数重载.

例如

    namespace MyStuff
    {
        typedef uchar utf8_t;
        typedef std::basic_string<utf8_t> U8string;
    }
    void SomeFunc(std::string &s);
    void SomeFunc(std::wstring &s);
    void SomeFunc(MyStuff::U8string &s);
Run Code Online (Sandbox Code Playgroud)

这一切都很好,直到我尝试使用stringstream.

    std::basic_ostringstream<MyStuff::utf8_t> ostr;
    ostr << 1;
Run Code Online (Sandbox Code Playgroud)

MSVC Visual C++ Express V10无法编译:

c:\program files\microsoft visual studio 10.0\vc\include\xlocmon(213): 
    warning C4273: 'id' : inconsistent dll linkage
    c:\program files\microsoft visual studio 10.0\vc\include\xlocnum(65) : 
            see previous definition of 
            'public: static std::locale::id std::numpunct<unsigned char>::id'
    c:\program files\microsoft visual studio 10.0\vc\include\xlocnum(65) : 
            while compiling class template static data member 'std::locale::id 
            std::numpunct<_Elem>::id'
    with
    [ …
Run Code Online (Sandbox Code Playgroud)

c++ unicode stringstream utf-8 visual-c++

7
推荐指数
0
解决办法
1758
查看次数

使用stringstream浮动的字符串

我在网上找到了这个代码作为模板,用于执行字符串到float/int/double转换.它只在这里,所以我有一些东西可以参考这个问题....

我想有一个用户输入一个数字作为一个字符串,将其转换为float,测试它的成功和辍学,如果进入了"Q"或打印"无效的输入",如果它是不是"Q'uit性格和返回更多输入.

转换失败测试的语法是什么?它会是ss.fail()吗?

// using stringstream constructors.
#include <iostream>
#include <sstream>
using namespace std;

int main () {

  int val;
  stringstream ss (stringstream::in | stringstream::out);

  ss << "120 42 377 6 5 2000";

  /* Would I insert an 

     if(ss.fail())
       { 
        // Deal with conversion error }
       }

    in here?! */


  for (int n=0; n<6; n++)
  {
    ss >> val;
    cout << val*2 << endl;
  }

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

c++ stringstream

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

如何获取stringstream将uint8_t视为数字而不是字符?

我有这个代码,并想知道是否有可能让stringstream将uint8_t视为一个数字而不是一个字符?

uint8_t s;
std::stringstream sstream( "255" );
sstream >> s;
std::cout << s << " equals 50/'2' not 255 " << std::endl;
Run Code Online (Sandbox Code Playgroud)

s应该是255而不是50 /'2'

stringstream

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

为什么我必须在这里清除std :: stringstream?

我写了一个简短的测试程序,看看我是否可以反复使用stringstream附加到字符串.

在第一个版本中,我得到了Output1,我真的不明白为什么s1保持为空.我发现我必须做ss.clear()然后在Output2中获得预期的结果.任何人都可以解释为什么没有明确的它不起作用?我本以为,如果我反复输入数字并将它们取回一个字符串,我应该总是得到数字.我不确定该数字是否被附加,但这不是这个例子的重点.

这里:http://www.cplusplus.com/reference/sstream/stringstream/它说我可以使用任何操作,没有限制或要求重置字符串,我可以看到.我也不明白为什么之后我得到没有ss.clear()的输出.

此外,我有点惊讶s0后来保持不变.因此,如果字符串已经包含内容,则流不会覆盖或重置字符串?

我正在使用gcc 3.4.4和cygwin.

int main()
{
    std::string s0;
    std::string s1;
    int n = 1;
    std::stringstream ss;
    ss << n;
    ss >> s0;
    cout << "S0:" << s0 << endl;
    ss.clear();     <-- If I remove this, then s1 stays empty.
    n = 2;
    ss << n;
    ss >> s1;
    cout << "S1:" << s1 << endl;
    ss << n;
    ss >> s0;
    cout << "S0_2:" << s0 << endl;    <-- Why is s0 still …
Run Code Online (Sandbox Code Playgroud)

c++ std stringstream

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

std :: stringstream的默认`fill character`是什么?

是实现定义还是标准建议流的默认填充字符?

示例代码:

#include <iostream>
#include <iomanip>
#include <sstream>

int main ()
{
    std::stringstream stream;
    stream << std::setw( 10 ) << 25 << std::endl;

    std::cout << stream.str() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

clang++ --stdlib=libstdc++

$ clang++ --stdlib=libstdc++ test.cpp
$ ./a.out | hexdump
0000000 20 20 20 20 20 20 20 20 32 35 0a 0a
000000c
$
Run Code Online (Sandbox Code Playgroud)

clang++ --stdlib=libc++

$ clang++ --stdlib=libc++ test.cpp
$ ./a.out | hexdump
0000000 ff ff ff ff ff ff ff ff 32 35 0a 0a
000000c …
Run Code Online (Sandbox Code Playgroud)

c++ stringstream setw clang++ libc++

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

获取字符串流中的最后一个字符而不复制其整个缓冲区

如果我使用此代码:

template <typename Streamable>
/* ... */
std::stringstream ss;
ss << function_yielding_a_Streamable();
auto last_char = ss.str().back();
Run Code Online (Sandbox Code Playgroud)

然后(我相信)ss需要创建一个缓冲区中的字符串副本,只为我获取最后一个字符,然后它将被销毁.我可以做一些更好的事情吗?也许使用这种seekp()方法?

c++ string stringstream seek

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

std :: string stream以二进制格式解析数字

我需要解析std::string包含二进制格式的数字,例如:

0b01101101
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用std :: hex格式说明符来解析十六进制格式的数字.

std::string number = "0xff";
number.erase(0, 2);
std::stringstream sstream(number);
sstream << std::hex;
int n;
sstream >> n;
Run Code Online (Sandbox Code Playgroud)

是否存在二进制格式的等价物?

c++ format binary stringstream c++11

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

标签 统计

stringstream ×10

c++ ×9

c++11 ×2

binary ×1

clang++ ×1

format ×1

libc++ ×1

seek ×1

setw ×1

std ×1

stl ×1

string ×1

stringbuffer ×1

unicode ×1

utf-8 ×1

visual-c++ ×1