ostringstream的字符串构造函数的目的是什么? [英] What is the purpose of ostringstream's string constructor?

查看:190
本文介绍了ostringstream的字符串构造函数的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MSVC 2005上,我有以下代码。

On MSVC 2005, I have the following code.

std::ostringstream stream("initial string ");
stream << 5;
std::cout << stream.str();

我期望的是:

initial string 5

我得到的是:

5nitial string

使用字符串初始化流,我希望流将其位置移动到初始字符串的末尾。显然,STL不同意我的意见(不是第一次)。

Initializing the stream with a string, I would expect the stream to move its position to the end of the initial string. Obviously, STL doesn't agree with me (not the first time).

这种行为的目的是什么?这个用例对什么有用吗?此外,是否有一种方法可以将流位置推进到初始字符串的末尾?

What's the purpose of such behavior? Is this use case useful for anything? Also, is there a way to advance the stream position to the end of the initial string?

推荐答案

使用 ostringstream 并提供初始值就像打开一个现有文件进行写入。你有一些数据,除非你另外指定,你的初始位置将在数据的开始。您可以在数据中查找,或者指定 ios :: ate ,将写入指针初始定位到现有数据的末尾,也可以指定 ios :: app 始终将写入指针定位到现有数据的结尾。

Using an ostringstream and providing an initial value is just like opening an existing file for writing. You have some data there, and unless you specify otherwise, your initial position will be at the beginning of the data. You can seek in the data, or you specify ios::ate to initially position the write pointer to the end of the existing data, or you can specify ios::app to always position the write pointer to the end of the existing data.

这篇关于ostringstream的字符串构造函数的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆