ostringstream

How do I perform string formatting to a static buffer in C++?

牧云@^-^@ 提交于 2021-01-28 07:59:41
问题 I am working in a section of code with very high performance requirements. I need to perform some formatted string operations, but I am trying to avoid memory allocations, even internal library ones. In the past, I would have done something similar to the following (assuming C++11): constexpr int BUFFER_SIZE = 200; char buffer[BUFFER_SIZE]; int index = 0; index += snprintf(&buffer[index], BUFFER_SIZE-index, "Part A: %d\n", intA); index += snprintf(&buffer[index], BUFFER_SIZE-index, "Part B:

How would std::ostringstream convert to bool?

陌路散爱 提交于 2019-12-31 02:35:11
问题 I stumbled across this code. std::ostringstream str; /// (some usage) assert( ! str ); What does ostringstream signify when used in a bool context? Is this possibly an incorrect usage that happens to compile and run? 回答1: It tells you if the stream is currently valid. This is something that all streams can do. A file stream, for example, can be invalid if the file was not opened properly. As a side note, this functionality (testing a stream as a bool) is achieved by overloading explicit

C++ format macro / inline ostringstream

你离开我真会死。 提交于 2019-12-17 08:53:33
问题 I'm trying to write a macro that would allow me to do something like: FORMAT(a << "b" << c << d) , and the result would be a string -- the same as creating an ostringstream, inserting a...d , and returning .str() . Something like: string f(){ ostringstream o; o << a << "b" << c << d; return o.str() } Essentially, FORMAT(a << "b" << c << d) == f() . First, I tried: 1: #define FORMAT(items) \ ((std::ostringstream&)(std::ostringstream() << items)).str() If the very first item is a C string (

Convert double to string quickly observing given precision

a 夏天 提交于 2019-12-13 01:12:26
问题 I have a C++ program using SDL . During rendering, I need to draw some graphics components. I sometimes need to convert double variables, rounded to one decimal, to std::string . For this, I'm currently using a ostringstream object and it works fine. std::ostringstream ss; ss << std::fixed << std::setprecision(1) << x; However, I'm wondering if this way of converting variables is a good idea with regards to performance. I've tried to round the double variables with std::to_string(std::round(x

Rounding off floats with ostringstream

我与影子孤独终老i 提交于 2019-12-12 10:37:09
问题 I have an issue regarding conversion from float to c++ string using ostringstream. Here is my line: void doSomething(float t) { ostringstream stream; stream << t; cout << stream.str(); } when t has value -0.89999 it is round off to -0.9, but when it's value is 0.0999 or lesser than this say 1.754e-7, it just prints without round off. what can be the solution for this. 回答1: You need to set the precision for ostringstream using precision e.g stream.precision(3); stream<<fixed; // for fixed

Why is my std::string obtained via stream being overwritten?

孤街浪徒 提交于 2019-12-11 13:17:19
问题 Assume I have a function like so: std::string get_shader(std::string path) { std::string fullpath = "./resources/shaders/" + path; std::ifstream vertexShaderFile(fullpath); std::ostringstream vertexBuffer; vertexBuffer << vertexShaderFile.rdbuf(); return vertexBuffer.str(); } And then some code like this: GLuint vertex_shader; GLuint fragment_shader; GLuint program; const GLchar * vertex_shader_source = get_shader("triangle_vertex.vs").c_str(); // At this point vertex_shader_source is correct

How do I use the ostringstream properly in c++?

独自空忆成欢 提交于 2019-12-09 14:21:30
问题 I am attempting to return some information when my toString() method is called, which include an integer and some floats. I learned about ostringstream works great but when the class that contains this method is called over and over again, the information gets stacked onto my previous output. Here is my code ostringstream int_buffer, float_buffer, float_buffer2; is introduced at the beginning of my class, then string toString() { int_buffer << on_hand; float_buffer << price; float_buffer2 <<

Inline ostringstream macro reloaded

感情迁移 提交于 2019-12-08 04:52:58
问题 Referring to C++ format macro / inline ostringstream The question there was for a macro that allows inline concatenation of objects to create a string, iostream-style. The answer was: #define SSTR( x ) dynamic_cast< std::ostringstream & >( \ ( std::ostringstream().seekp( 0, std::ios_base::cur ) << x ) \ ).str() Usage (for example): throw std::runtime_error( SSTR( "FooBar error: Value " << x << " exceeds " << y ) ); That works beautifully - with GCC. It compiles and runs under Visual C++ 2005,

How do I use the ostringstream properly in c++?

那年仲夏 提交于 2019-12-03 23:30:17
I am attempting to return some information when my toString() method is called, which include an integer and some floats. I learned about ostringstream works great but when the class that contains this method is called over and over again, the information gets stacked onto my previous output. Here is my code ostringstream int_buffer, float_buffer, float_buffer2; is introduced at the beginning of my class, then string toString() { int_buffer << on_hand; float_buffer << price; float_buffer2 << generated_revenue; string stron_hand = int_buffer.str(); string strprice = float_buffer.str(); string

Problem with ostringstream and copy constructor [duplicate]

佐手、 提交于 2019-12-02 13:15:02
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: Why copying stringstream is not allowed? how copy from one stringstream object to another in C++? Compiling class T fails with Visual C++ and GCC producing iostreams template errors. Here is the code: #include <sstream> class T { static T copy; std::ostringstream log; T() {} T(const T& t) {log = t.log;} ~T() {copy = *this;} }; T T::copy; Changing log data member type to string makes it compile and run OK. Is