我想清除并重用ostringstream(和底层缓冲区),以便我的应用程序不必执行尽可能多的分配.如何将对象重置为初始状态?
stringstream parser;
parser << 5;
short top = 0;
parser >> top;
parser.str(""); //HERE I'M RESETTING parser
parser << 6; //DOESN'T PUT 6 INTO parser
short bottom = 0;
parser >> bottom;
Run Code Online (Sandbox Code Playgroud)
为什么不起作用?
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以获得所需的输出?
如何将字符串流的状态"重置"为创建它时的状态?
int firstValue = 1;
int secondValue = 2;
std::wstringstream ss;
ss << "Hello: " << firstValue;
std::wstring firstText(ss.str());
//print the value of firstText here
//How do I "reset" the stringstream here?
//I would like it behave as if I had created
// stringstream ss2 and used it below.
ss << "Bye: " << secondValue;
std::wstring secondText(ss.str());
//print the value of secondText here
Run Code Online (Sandbox Code Playgroud) 其中一种可能性是:
somestringstream.str("");
Run Code Online (Sandbox Code Playgroud)
但它最优化吗?有没有办法保留stringstream内部缓冲区,以便跟随运算符<<()调用不需要再次保留内存?
在http://channel9.msdn.com/Events/GoingNative/2013/Writing-Quick-Code-in-Cpp-Quickly的 50:40时,Andrei Alexandrescu开玩笑说如何效率/慢速istream.
我过去遇到过一个问题,ostream很慢,而且fwrite明显更快(在主循环运行一次时减少了很多秒),但我从来不明白为什么也没看过它.
什么使得C++中的istream和ostream变慢?或者至少比其他东西(如fread/fget,fwrite)慢,这同样满足了需求.
这些线程不回答我:
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) 我只是想知道clear()和str("")之间的区别是什么;
例如:
stringstream ss("Stack Overflow");
ss.clear();
ss.str("");
Run Code Online (Sandbox Code Playgroud)
我想知道潜在的技术差异.
std::ostream没有成员函数close()。我不应该允许关闭什么类型的流?
例如,也许我想关闭std::cout以防止进一步写入。
std::cout.close(); // \xe2\x80\x98std::ostream\xe2\x80\x99 has no member named \xe2\x80\x98close\xe2\x80\x99\nRun Code Online (Sandbox Code Playgroud)\n\n如果我使用 C 库,我可以stdout这样结束:
fclose(stdout); // no problem\nRun Code Online (Sandbox Code Playgroud)\n\nclose()那么排除该成员背后的想法是什么std::ostream?
有关的:
\n\n\n任何人都可以告诉我如何清除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)
我所要求的输出单独"嗨".请告诉我
我一直在使用以下代码来清除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) C++14
一般来说,大学的工作人员建议我们使用Boost来解析文件,但我已经安装了它,但没有成功地用它实现任何东西。
所以我必须逐行解析 CSV 文件,其中每行有 2 列,当然用逗号分隔。这两列中的每一列都是一个数字。我必须取这两个数字的整数值并使用它们最后构造我的分形对象。
第一个问题是:该文件可能如下所示:
1,1
<HERE WE HAVE A NEWLINE>
<HERE WE HAVE A NEWLINE>
Run Code Online (Sandbox Code Playgroud)
这种格式的文件没问题。但我的解决方案输出“无效输入”,其中正确的解决方案应该只打印一次相应的分形 - 1,1。
第二个问题是:该文件可能如下所示:
1,1
<HERE WE HAVE A NEWLINE>
1,1
Run Code Online (Sandbox Code Playgroud)
这应该是一个无效的输入,但我的解决方案将其视为正确的输入 - 并且只是跳过中间的换行符。
也许你可以指导我如何解决这些问题,这对我来说真的很有帮助,因为我从早到晚都在努力进行这项练习。
这是我当前的解析器:
#include <iostream>
#include "Fractal.h"
#include <fstream>
#include <stack>
#include <sstream>
const char *usgErr = "Usage: FractalDrawer <file path>\n";
const char *invalidErr = "Invalid input\n";
const char *VALIDEXT = "csv";
const char EXTDOT = '.';
const char COMMA = ',';
const char MINTYPE = …Run Code Online (Sandbox Code Playgroud) 我试图用g ++(GCC)4.8.2 20131212(Red Hat 4.8.2-7)编译以下代码:
#include <sstream>
using namespace std;
int main(int argc, char ** argv)
{
auto x = 1;
stringstream s1, s2;
s1.swap(s2);
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
g++ -g -std=c++0x -c main.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:8:5: error: ‘std::stringstream’ has no member named ‘swap’
s1.swap(s2);
^
make: *** [main.o] Error 1
Run Code Online (Sandbox Code Playgroud)
根据这个参考,它应该工作.使用不同的-std标志(gnu ++ 11,c ++ 0x等)没有帮助.我错过了什么?