How to reuse an ostringstream?
I'd like to clear out and reuse an ostringstream (and the underlying buffer) so that my app doesn't have to do as many allocations. How do I reset the object to its initial state? ...
I'd like to clear out and reuse an ostringstream (and the underlying buffer) so that my app doesn't have to do as many allocations. How do I reset the object to its initial state? ...
I have next code: #include <iostream> #include <algorithm> #include <map> #include <iterator> //namespace std //{ std::ostream& operator << ( std::ostream& out, const std::pair< size_t, size_t >& rhs ) { out << rhs.first << ", " << rhs.second; return out; } //} int main() { std::map < size_t, size_t > some_map; ...
Some background information, for a homework assignment I had to write a polish notation calculator using binary trees, for this to work I had to parse command line input so that it would properly build the binary tree and then go over it to give a valid answer to the mathematical expression that was entered. For the parsing I used a std...
I have a container of pointers which I want to iterate over, calling a member function which has a parameter that is a reference. How do I do this with STL? My current solution is to use boost::bind, and boost::ref for the parameter. // Given: // void Renderable::render(Graphics& g) // // There is a reference, g, in scope with the call...
I've discovered that std::string's are very slow compared to old-fashioned null-terminated strings, so much slow that they significantly slow down my overall program by a factor of 2. I expected STL to be slower, I didn't realise it was going to be this much slower. I'm using Visual Studio 2008, release mode. It shows assignment of a...
Given (in C++) char * byte_sequence; size_t byte_sequence_length; char * buffer; size_t N; Assuming byte_sequence and byte_sequence_length are initialized to some arbitrary length sequence of bytes (and its length), and buffer is initialized to point to N * byte_sequence_length bytes, what would be the easiest way to replicate the byt...
I cannot find this question on stockoverflow. But I am wondering how people use STL (No fancy boost)... just an ol' fashion STL. Tricks/tips/mostly used cases acquired over many, many years... and perhaps gotchas... Let's share it... One tip per answer...with code example -- Edit is it such a bad question as it resulting in downvotes...
I want to fill these containers pretty quickly with some data for testing. What are the best and quick ways to do that? It shouldn't be too convoluted, and thus and inhumanly short, but also NOT to verbose Edit Guys I thought you can do something with memset knowning that vector has an underlining array? Also, what about map? ...
in c++, <stdexcept> has a base class for 'domain errors', std::domain_error. i don't understand under what circumstances i should throw a domain error in my code. all of the other exception base classes are pretty self explanatory. i'm pretty sure that std::domain_error has nothing to do with internet domain names, per se, so please e...
I prefer two ways: void copyVecFast(const vec<int>& original) { vector<int> newVec; newVec.reserve(original.size()); copy(original.begin(),original.end(),back_inserter(newVec)); } void copyVecFast(vec<int>& original) { vector<int> newVec; newVec.swap(original); } How do you do it? ...
If for instance you have a std::vector<MyClass>, where MyClass has a public method: bool isTiredOfLife(), how do you remove the elements that return true? ...
The situation is: there is a file with 14 294 508 unsigned integers and 13 994 397 floating-point numbers (need to read doubles). Total file size is ~250 MB. Using std::istream takes ~30sec. Reading the data from file to memory (just copying bytes, without formatted input) is much faster. Is there any way to improve reading speed withou...
I've been comparing a STL implementation of a popular XmlRpc library with an implementation that mostly avoids STL. The STL implementation is much slower - I got 47s down to 4.5s. I've diagnosed some of the reasons: it's partly due to std::string being mis-used (e.g. the author should have used "const std::string&" wherever possible - ...
I have a priority_queue of some object: typedef priority_queue<Object> Queue; Queue queue; From time to time, the priority of one of the objects may change - I need to be able to update the priority of that object in the queue in an efficient way. Currently I am using this method which works but seems inefficient: Queue newQueue; whi...
First I will give a specific case, and the I would like to see if it can be applied to a general problem. Say I have map. And I want to get all the keys meeting a certain criteria. For example all keys that contain "COL". My naive implementation will be template<typename T> void Filter (map<string, T> & m, std:set<string> & result, ...
I'm looking for a string indexof function from the std namespace that returns an integer of a matching string similar to the java function of the same name. Something like: std::string word = "bob"; int matchIndex = getAString().indexOf( word ); where getAString() is defined like this: std::string getAString() { ... } ...
I want to supply a number, and then receive a set of random numbers. However, I want those numbers to be the same regardless of which computer I run it on (assuming I supply the same seed). Basically my question is: in C++, if I make use of rand(), but supply srand() with a user-defined seed rather than the current time, will I be able...
Is this guaranteed to be always true: std::numeric_limits<int>::max() == INT_MAX What does C++ standard say about it? I could not find any reference in the standard that would explicitly state this, but I keep reading that those should be equivalent. What about C99 types that are not in C++98 standard for compilers that implement bot...
This should be trivial but I can't seem to find it (unless no such class exists!) What's the STL class (or set of classes) for smart pointers? UPDATE Thanks for the responses, I must say I'm surprised there's no standard implementation. I ended up using this one: http://www.gamedev.net/reference/articles/article1060.asp ...
So I'm wondering about both setting and manipulating bit fields. I've already found http://stackoverflow.com/questions/264552/cc-code-to-treat-a-character-array-as-a-bitstream which is similar to my question I guess but it doesn't doesn't give me a nice stl approach I am thinking has to exist. I was thinking of bitsets from the stl ...