任何人都可以告诉我如何清除stringstream的内容..?我尝试了以下但它没有工作.
stringstream ss;
ss<<"bala"<<"murugan";
cout<<ss.str(); //Output balamurugan
ss.str().clear();
ss<<" hi";
cout<<ss.str();
// output is balamurugan hi
Run Code Online (Sandbox Code Playgroud)
我所要求的输出单独"嗨".请告诉我
仅作为示例而非实际代码:
stringstream ss;
ss << " world!";
string hello("Hello");
// insert hello to beginning of ss ??
Run Code Online (Sandbox Code Playgroud)
感谢所有答复,我也找到了下面的代码,该代码可以正常工作:
ostringstream& insert( ostringstream& oss, const string& s )
{
streamsize pos = oss.tellp();
oss.str( s + oss.str() );
oss.seekp( pos + s.length() );
return oss;
}
Run Code Online (Sandbox Code Playgroud) 出于好奇心的简单问题.
类上的多个方法需要使用字符串流,或者特别是ostringstream.
1)将一个stringstream变量作为类成员,然后在使用它之前清除它,即msg.str("")
2)每次需要使用时,在每个方法中本地创建一个新的stringstream变量.
在效率和整洁方面,哪种方法最好?
我的预感是选项1,但不确定初始构造是否与每次调用str()相结合会更糟?
PS我读过初始化..哪一个效率更高?而这是更快/更有效?,我的下一步将是让我研究分析和编写一个小测试应用程序,但我觉得要求可能更快:-)
我有一个stringstream,我需要将第一部分输出,然后将剩余部分放入一个单独的字符串中.例如,我有字符串"This is a car",我需要最终得到2个字符串:a = "This"和b = "is a car".
当我使用stringstream来获取第一部分时<<,我使用.str()转换为字符串,这当然给了我整个事情" This is a car".我怎么能让它发挥我想要的?
我一直在使用以下代码来清除std::stringstream:
m.swap(std::stringstream());
Run Code Online (Sandbox Code Playgroud)
代码可能来自这个SO线程.最近我在Visual Studio 2013中编译了我的代码,我收到了以下警告:
warning C4239: nonstandard extension used : 'argument' : conversion from 'std::basic_stringstream<char,std::char_traits<char>,std::allocator<char>>' to 'std::basic_stringstream<char,std::char_traits<char>,std::allocator<char>> &'
1> A non-const reference may only be bound to an lvalue
Run Code Online (Sandbox Code Playgroud)
我没有在早期版本的Visual Studio中使用/ W4收到任何警告.我记得在早期版本中我无法可靠地清除字符串流,这就是我最初使用该代码的原因.
因此,通过这种方式清除字符串流是否可移植,如果没有,你可以解释为什么不呢?还有一种可移植的方式来清除字符串流吗?谢谢
编辑:这是请求的代码.使用它来编译它/W4以查看警告.
int _tmain(int argc, _TCHAR* argv[])
{
std::stringstream m;
m.swap(std::stringstream());
return 0;
}
Run Code Online (Sandbox Code Playgroud) 如果他们能告诉我为什么streamstream<<(char)忽略0x05,我会给任何人买啤酒.这个节目正在制作中000102030406070809E280081150121314
我认为预期的输出更像是:
00010203040506070809E2800811050505050505050550121314
IBM i(aka AS/400)C++编译器,在V7R1上运行.
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <stdint.h>
using namespace std;
typedef int8_t byte;
int main(int argc, char* argv[])
{
stringstream sstr2;
sstr2 << char(0x00);
sstr2 << char(0x01);
sstr2 << char(0x02);
sstr2 << char(0x03);
sstr2 << char(0x04);
sstr2 << char(0x05);
sstr2 << char(0x06);
sstr2 << char(0x07);
sstr2 << char(0x08);
sstr2 << char(0x09);
sstr2 << char(0xe2);
sstr2 << char(0x80);
sstr2 << char(0x08);
sstr2 << char(0x11);
sstr2 << char(0x05);
sstr2 …Run Code Online (Sandbox Code Playgroud) 我知道stringstream可以更新stringstream::str(),但是如果我在stringstream之后输入其他内容,则无法按预期工作.以下代码段演示了这种现象:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
stringstream ss; //ostringstream gives the same output
ss << "Original string ";
ss.str("Updated string ");
ss << "sth else";
cout << ss.str() << endl;
}
Run Code Online (Sandbox Code Playgroud)
我希望得到输出
Updated string sth else
Run Code Online (Sandbox Code Playgroud)
但它实际上是输出
sth elsestring
Run Code Online (Sandbox Code Playgroud)
似乎相反,将新输入的字符串附加到当前字符串的末尾(在我的情况下Updated string),它会尝试从头开始覆盖它.我的代码出了什么问题?
#include <sstream>
#include <iostream>
#include <string>
class A : public std::stringstream {
public:
A() {}
~A() { std::cout << str().c_str() << std::endl; }
};
int main() {
A() << "Foo" << std::string(" ABC");
}
Run Code Online (Sandbox Code Playgroud)
我期待打印的程序:
Foo ABC
Run Code Online (Sandbox Code Playgroud)
代替
0x401bad ABC
Run Code Online (Sandbox Code Playgroud)
为什么要打印0x401bad ABC?
g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Run Code Online (Sandbox Code Playgroud) 我有这样的字符串:'123plus43times7'
其中数字后跟字典中的单词.
我知道我可以使用>>运算符提取int/numbers :
StringStream >> number
Run Code Online (Sandbox Code Playgroud)
我可以得到这个号码.但是,Stream仍然有数字.如果数字长度未知或者我应该找出数字的长度,然后使用str.substr()创建新的字符串流,如何删除该数字?使用C++ STL String和SStream执行此任何其他更好的方法将非常感激.
#include <sstream>
#include <iostream>
int main() {
double val;
std::stringstream ss("6.93758e-310");
ss >> val;
std::cout << "fail: " << ss.fail() << std::endl
}
Run Code Online (Sandbox Code Playgroud)
......我得到不同的行为:
ss.fail()未设置,而可能需要注意的是,在这两种情况下,errno都设置为ERANGE.
另外,在本地我得到了与clang和gcc相同的行为,除非我明确地使用libc ++和clang(-stdlib=libc++)而不是glibc.
我不确定正确的行为是什么,但我觉得它应该是一致的.
c++ ×10
stringstream ×10
string ×4
double ×1
inputstream ×1
parsing ×1
performance ×1
stream ×1