stl

How should smart pointers get down casted?

Do smart pointers handle down casting, and if not what is a safe way of working around this limitation? An example of what I'm trying to do is having two STL vectors (for example) containing smart pointers. The first contains smart pointers to a base class while the second contains smart pointers to a derived class. The smart pointers...

Sorting a std::map by value before output & destroy

I am aware that map is not prepared to be sorted, its heavily optimized for fast and random key access., and actually doesn't support std::sort. My current problem is that I have a full map<std::string,int> which I'm not going to use anymore, I just need to extract 10 pairs in value(int) order and destroy it. The best thing if it ...

Extract min implemetation for heap in c++

I need to implement extract min for heap(in c++ if possible), could not get this method from STL heap. ...

compile against libc++ statically

I wrote some custom c++ code and it works fine in ubuntu, but when I upload it to my server (which uses centos 5) its fails and says library is out of date. I googled all around and centos cannot use the latest libraries. How can I compile against the stl so that it is included in the binary and it doesn't matter that centos uses an ol...

Effect of memory usage in the complexity of an algorithm

I am reading Nicolai Josuttis book on C++STL algorithms. For many algorithms such as stable_sort(), he mentions that the complexity of the algorithm n * log(n) if enough memory is available, otherwise it is n * log(n) * log(n). My question is how does the memory usage affects the complexity ? And how does STL detect such a situation? ...

Linux IDE with proper support for STL debugging

I am looking for a Linux IDE with support for STL debugging. the problem is that with Eclipse CDT, if I inspect the vector after the push_back: int main() { vector<string> v; v.push_back("blah"); return 0; } I get something hostile like {<std::_Vector_base<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, st...

Differences in Microsofts C++ STL for Windows CE?

Hi, anyone know of a complete list of the differences in Microsofts implementation of STL for Windows CE, compared to the full STL for desktop? I am using WinCE 6.0, with VS 2005. I am a bit suprised that they seem to have removed so many things; for GCC it is almost the same. Thanks! ...

simple C++ hash_set example

I am new to C++ and STL. I am stuck with the following simple example of a hash set storing custom data structures: #include <iostream> #include <ext/hash_set> using namespace std; using namespace __gnu_cxx; struct trip { int trip_id; int delta_n; int delta_secs; trip(int trip_id, int delta_n, int delta_secs){ th...

Using find_if on std::vector<std::string> with bind2nd and string::compare

This may seem to be an academic question, but still I would be very interested in the answer: I have a vector of strings s in which I would like to find a given string findme. This can be done using something like find(s.begin(), s.end(), findme); My question is: There must be a way doing the same using find_if and the compare method...

Sorting a vector of custom objects

How does one go about sorting a vector containing custom (i.e. user defined) objects. Probably, standard STL algorithm sort along with a predicate (a function or a function object) which would operate on one of the fields (as a key for sorting) in the custom object should be used. Am I on the right track? ...

Can I use an stl map if I plan to use arbitrary class objects as the key?

I'm new to STL. The thing stumping me about using a map to store arbitrary objects: std::map<MyClassObj, MyDataObject> MyMap; is how I find objects. How would MyMap.find (MyClassObjInstance) work for instance? Do I need to implement my own iterator and provide some standard functions which would include some equivalence function? A...

map of vectors in STL?

I want to have a map of vectors, (but I don't want to use pointer for the internal vector), is it possible? // define my map of vector map<int, vector<MyClass> > map; // insert an empty vector for key 10. # Compile Error map.insert(pair<int, vector<MyClass> >(10, vector<MyClass>)); I know that if I have used pointer for vector, as fo...

Parrallel reads from STL containers

It is safe to read a STL container from multiple parallel threads. However, the performance is terrible. Why? I create a small object that stores some data in a multiset. This makes the constructors fairly expensive ( about 5 usecs on my machine. ) I store hundreds of thousands of the small objects in a large multiset. Processing t...

Get a pointer to structure in a map C++

Ok so I have struct like this typedef struct { float x; float y; char name[]; } pTip; And another struc typdef struct { float xx; float yy; pTip *tip; }finalTip; I create and populate a map<string, pTip> maps That works fine. I am now trying to generate vector of finalTips I do: map<string, pTip>::const_iterator it...

STL: Stores references or values?

Hi, I've always been a bit confused about how STL containers (vector, list, map...) store values. Do they store references to the values I pass in, or do they copy/copy construct +store the values themselves? For example, int i; vector<int> vec; vec.push_back(i); // does &(vec[0]) == &i; and class abc; abc inst; vector<abc> vec; ve...

Persistant references in STL Containers

When using C++ STL containers, under what conditions must reference values be accessed? For example are any references invalidated after the next function call to the container? { std::vector<int> vector; vector.push_back (1); vector.push_back (2); vector.push_back (3); vector[0] = 10; //modifies 0'th element int& ref = vector[0...

How to effectively delete C++ objects stored in multiple containers? auto_ptr?

I have an application which creates objects of a certain kind (let's say, of "Foo" class) during execution, to track some statistics, and insert them into one or both of two STL maps, say: map<Foo*, int> map1; map<Foo*, int> map2; I was wondering what is the best way to delete the Foo objects. At the moment my solution is to iterate o...

std::vector and c-style arrays

I am experimenting with OpenCL to increase the speed of our software. We work with maps a lot and, to simplify, represent a map as a std::vector< std::vector >. The OpenCL API takes raw c-style pointers as arguments, for example int* in the case above. My questions: Are there implementation guarantees in the stl that vector is, intern...

std c++ container element destruction and insertion behaviour

I have made the following little Program: (basically a class that couts if it gets created, copied or destroyed and a main that does some of that) class Foo { public: Foo(string name): _name(name) { cout << "Instance " << _name << " of Foo created!" << std::endl; }; Foo(const Foo& other): _name(other._name) { cout << "Instance ...

How can I use std::remove on a container with std::tr1::weak_ptr?

If I had a STL container, say a list of pointers I could remove them like in the exmple below. With a container of weak_ptrs this does not work, because they cannot be compared, as they need to be locked first. What can I do? void MyClass::RemoveItem(std::tr1::weak_ptr<Item> const & pItem) { mylist.remove(pItem); } ...