stl

What is std::pair?

What is std::pair for, why would I use it, and what benefits does boost::compressed_pair bring? ...

What's the best hashing algorithm to use on a stl string when using hash_map?

I've found the standard hashing function on VS2005 is painfully slow when trying to achieve high performance look ups. What are some good examples of fast and efficient hashing algorithms that should void most collisions? ...

Why does std::stack use std::deque by default?

Since the only operations required for a container to be used in a stack are: back() push_back() pop_back() Why is the default container for it a deque instead of a vector? Don't deque reallocations give a buffer of elements before front() so that push_front() is an efficient operation? Aren't these elements wasted since they will n...

What is the best way to inspect STL containers in Visual Studio debugging?

If I have a std::vector or std::map variable, and I want to see the contents, it's a big pain to see the nth element while debugging. Is there a plugin, or some trick to making it easier to watch STL container variables while debugging (VS2003/2005/2008)? ...

Why use c strings in c++?

Is there any good reason to use C-strings in C++ nowadays? My textbook uses them in examples at some points, and I really feel like it would be easier just to use a std::string. ...

How to retrieve all keys (or values) from a std::map?

This is one of the possible ways I come out: struct RetrieveKey { template <typename T> typename T::first_type operator()(T keyValuePair) const { return keyValuePair.first; } }; map<int, int> m; vector<int> keys; // Retrieve all keys transform(m.begin(), m.end(), back_inserter(keys), RetrieveKey()); // Dump al...

Why is it wrong to use std::auto_ptr<> with STL containers?

Why is it wrong to use std::auto_ptr<> with STL containers? ...

how get a vector<Derived*> into a function that expects a vector<Base*> as argument

Hi, Consider these classes, class Base { ... }; class Derived : public Base { ... }; this function void BaseFoo( std::vector<Base*>vec ) { ... } And finally my vector std::vector<Derived*>derived; I want to pass derived to function BaseFoo, but the compiler doesn't let me. How do I solve this, without copying the whole vector...

Pointers and containers

We all know that RAW pointers need to be wrapped in some form of smart pointer to get Exception safe memory management. But when it comes to containers of pointers the issue becomes more thorny. The std containers insist on the contained object being copyable so this rules out the use of std::auto_ptr, though you can still use boost::sh...

What is the best way to slurp a file into a std::string in c++?

How to slurp a file into a std::string, i.e., read the whole file at once? Text or binary mode should be specified by the caller. The solution should be standard-compliant, portable and efficient. It should not needlessly copy the string's data, and it should avoid reallocations of memory while reading the string. One way to do this wou...

What is the STL implementation with the lowest memory footprint?

I am working on a very large scale computing library that is using STL heavily. The library is being built using MSVC2003 and it is using its STL implementation. I am looking for an alternative STL implementation that would help the library lower its memory requirements and increase its performance. It is not possible to switch to a new...

Has anyone already done the work to make STLPort build with VS2008 and/or an x64 build with VS2005?

At present it seems that VS2008 still isn't supported either in the 5.1.5 release or in the STLPort CVS repository. If someone has already done this work then it would be useful to share, if possible :) Likewise it would be useful to know about the changes required for a VS2005 or 2008 x64 build. ...

Generic cache of objects

Hi, Does anyone know any implementation of a templated cache of objects? You use a key to find object (the same as in std::map<>) You specify a maximum number of objects that can be in the cache at the same time There are facilities to create an object not found in the cache There are facilities to know when an object is discarded fr...

existance map in c++

I want something like an std::map, but I only want to see if the item exists or not, I don't actually need a key AND value. What should I use? ...

Returning an 'any kind of input iterator' instead of a vector::iterator or a list::iterator

Suppose I want to implement in C++ a data-structure to store oriented graphs. Arcs will be stored in Nodes thanks to STL containers. I'd like users to be able to iterate over the arcs of a node, in an STL-like way. The issue I have is that I don't want to expose in the Node class (that will actually be an abstract base class) which STL...

Why use iterators instead of array indices?

Take the following two lines of code: for (int i = 0; i < some_vector.size(); i++) { //do stuff } And this: for (some_iterator = some_vector.begin(); some_iterator != some_vector.end(); some_iterator++) { //do stuff } I'm told that the second way is preferred. Why exactly is this? ...

c++ exception : throwing std::string

Hello, I would like to throw an exception when my C++ methods encounter something weird and can't recover. Is it OK to throw a std::string pointer? Here's what I was looking forward to doing: void Foo::Bar(){ if(!QueryPerformanceTimer(&m_baz)){ throw new std::String("it's the end of the world!"); } } void Foo:Caller(){ try{...

Should one prefer STL algorithms over hand-rolled loops?

I seem to be seeing more 'for' loops over iterators in questions & answers here than I do for_each(), transform(), and the like. Scott Meyers suggests that stl algorithms are preferred, or at least he did in 2001. Of course, using them often means moving the loop body into a function or function object. Some may feel this is an unacce...

std::map iteration - order differences between Debug and Release builds

Here's a common code pattern I have to work with: class foo { public: void InitMap(); void InvokeMethodsInMap(); static void abcMethod(); static void defMethod(); private: typedef std::map<const char*, pMethod> TMyMap; TMyMap m_MyMap; } void foo::InitMap() { m_MyMap["abc"] = &foo::abcMethod; m_MyMap["def...

Setting all values in a std::map

I'd like to set all the values in a std::map to the same value. Is there a method for this or do I have to iterate through the map and set them one by one? ...