stl

Forward-declaring library names

Hi all, An excerpt from "Exceptional C++": "In the old days, you could just replace "#include " with "class ostream;" in this situation, because ostream used to be a class and it wasn't in namespace std. Alas, no more. Writing "class ostream;" is illegal for two reasons: ostream is now in namespace std, and programmers aren't allowed ...

Post-Initialize stringstream inside map?

How can I post-initialize an stringstream inside a map? Is it even possible or do I have to create a stringstream*? std::map<std::string, std::stringstream> mapTopics; if(mapTopics.end() == mapTopics.find(Topic)) { mapTopics[Topic] = std::stringstream(""); // Post Initialize <--- } std::map<std::string, std::stringstream>::iterat...

Which STL container for ordered data with key-based access?

Let's say I have a collection of Person objects, each of which looks like this: class Person { string Name; string UniqueID; } Now, the objects must be stored in a container which allows me to order them so that I can given item X easily locate item X+1 and X-1. However, I also need fast access based on the UniqueID, as the coll...

Question about using string::swap() with temporaries

The following segment demonstrates my issue: (compilation error on GCC) stringstream ss; string s; ss << "Hello"; // This fails: // s.swap(ss.str()); // This works: ss.str().swap(s); My error: constSwap.cc:14: error: no matching function for call to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::swap(std::...

Using bitset in place of using hand written bit manipulation code?

Is there any performance loss/gain using bitset in place where hand written? How to build the following using a bitset at runtime make all the bits between 2 and 5 as zero i.e., 11110011. ...

C++ defining two multimaps causes program to crash

This is utterly mystifying me. I have, in my class declaration, two lines: std::multimap<int, int> commands; std::multimap<std::string, std::string> config; The code compiles without issue, but when I run it, I get the following error: *** glibc detected *** ./antares: free(): invalid pointer: 0xb5ac1b64 *** Seems simple enough, ex...

How does the STL map::find function work without the equality operator?

Under the hood, an STL map is a red-black tree, and it uses the < operator of its keys or a user-provided comparison to figure out the location for element insertion. map::find() returns the element that matches the supplied key (if any matches are present) How can it do this without using an equality operator? Let's say my map has the...

C++ STL in VS2008: std::ostringstream throws std::bad_alloc after heavy assign/clear usage

Hi all, I have come across a situation (on Win32) where the std::ostringstream object continues to consume process memory, even when it is ostensibly cleared out after a series of append-type operations. Please take a look at this C++ fragment: int main(void) { std::ostringstream cOutputLogStream; // Random long string std...

What's the difference between set<pair> and map in C++?

There are two ways in which I can easily make a key,value attribution in C++ STL: maps and sets of pairs. For instance, I might have map<key_class,value_class> or set<pair<key_class,value_class> > In terms of algorithm complexity and coding style, what are the differences between these usages? ...

C++: speed of std::stack::pop() method

I'm writing a lighter version of some containers from STL for myself. (I know that STL was written by professional programmers and I am too stupid or too ambitious if think that I can write it better than they did. When I wrote my list (only with method I need), it worked few times faster. So, I thought it's a good idea. But, anyway.) ...

Light weight container around const char* & length without copying the data

I have a underlying API that passes a const char* and a length: foo(const char* data, const uint32_t len); I'd like to wrap this data/length in a light weight container that can be iterated and has the ability to be randomly accessed but not make a copy (e.g. like a vector). What is the best way to achieve this? The const char* data i...

How can I make an object construct itself at a particular location in memory?

Possible Duplicate: Create new C++ object at specific memory address? I am writing what is essentially an object pool allocator, which will allocate a single class. I am allocating just enough memory to fit the objects that I need, and I am passing out pointers to spaces inside. Now my question is this: Once I have gotten a ...

method declared in struct in C++ (STL)

I'm trying to understand the syntax used in STL for a class. Our teacher pointed us to this website (http://www.sgi.com/tech/stl/Map.html) where I copied the code below: struct ltstr { bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) < 0; } }; int main() { map<const char*, int, ltstr> months; ...

Watch content of stl vector via gdb

Hi there! I've got a stl vector of custom class objects defined in a global namespace (yeah, evil, I know). Now I would like to set a watchpoint in gdb to monitor possible changes in a (public) member variable for a certain element of this vector. Something like: watch custom_namespace::vec[123].aVariable If I do this, then gdb (Vers...

Behavior of STL remove() function - only rearrange container elements?

I've read here on StackOveflow and other sources that the behavior of the remove function is simply re-ordering the original container so that the elements that are TO BE REMOVED are moved to the end of the container and ARE NOT deleted. They remain part of the container and the remove() function simply returns an iterator that delimits...

Iterate forward and then reverse over STL container

I have an STL container and I need to perform an action on each element in the container. But if the action fails on any element, I want to reverse the action on any elements that have already been changed. For example, if I had an STL vector with pointers to a number bankAccount classes and wanted to increase each one by $50. But i...

c++: set<customClasS* how to overload operator<(const customClass&*...)?!

Good Evening (depending on where u are right now). I am a little confused with the stl stuff for sorted sets... I want to store pointers of a custom class in my set and I want them to be sorted by my own criterion and not just the pointer size. Anyone has an idea how to do this? Since it is impossible to do it like operator<(const foo ...

Storing map's first element to a vector in the most generic form. Best solution

My goal is to store all the keys of a map (first item) to a vector and I'm doing the following. template < class vecDet> class storeInto { public: storeInto(vecDet& source) : VectorInfo(source) { } ~storeInto(); template <class pairdet> void operator()(pairdet& pairinfo) { VectorInfo.push_back(pairinfo.first); } p...

wchar not working in VC++ 2005

wstring ws(L"Press 'q' to end."); wcout << ws; error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::wstring' (or there is no acceptable conversion) This is in a VC++ 2005 Win32 console app, created with default settings... which I think means UNICODE is on? I only just found out cout do...

scoped_ptr for structure with substituted free method

I have a structure typedef struct myStruct_st { int a; }myStruct; It can be created using myStruct * myStruct_new() { printf("Allocate\n"); return new myStruct; } And deleted using static void myStruct_free(myStruct * ptr) { printf("Deallocate\n"); delete ptr; } I want the memory allocated for the structure freed au...