stl-containers

Why is is it not possible to pass a const set<Derived*> as const set<Base*> to a function?

Before this is marked as duplicate, I'm aware of this question, but in my case we are talking about const containers. I have 2 classes: class Base { }; class Derived : public Base { }; And a function: void register_objects(const std::set<Base*> &objects) {} I would like to invoke this function as: std::set<Derived*> objs; registe...

Assigning vector::iterator to char array post VS 2003

I am trying to get some C++ code originally written in Microsoft Visual Studio (VS) 2003 to compile under VS 2008 and I am having trouble finding an efficient solution to assigning a vector::iterator to the beginning of a char array. I know that iterators went from being a defined as a simple pointer type (T*) to a class type between VS...

Checking for list membership using the STL and a unary function adapted functor

I've attempted to write a brief utility functor that takes two std::pair items and tests for their equality, but disregarding the ordering of the elements. Additionally (and this is where I run into trouble) I've written a function to take a container of those std::pair items and test for membership of a given pair argument in a the cont...

is c++ STL algorithms and containers same across platforms and performance?

After learning good amount of c++, i'm now into STL containers and algorithms template library, my major concerns are, 1) Is this library same across different platforms like MS, linux n other os? 2) will quality or efficiency of program c++ module decrease with more use of STL containers and algorithms, i think i can't customize it to...

Copy vector of values to vector of pairs in one line

I have the following types: struct X { int x; X( int val ) : x(val) {} }; struct X2 { int x2; X2() : x2() {} }; typedef std::pair<X, X2> pair_t; typedef std::vector<pair_t> pairs_vec_t; typedef std::vector<X> X_vec_t; I need to initialize instance of pairs_vec_t with values from X_vec_t. I use the following code and it ...

What is the preferred STL collection when that's all you need?

I just need a "bag of things". It doesn't need to be a set, a map or even have any particular order. I just need to be able to add things and iterate over it, nothing more. I don't expect it to be very large but it can't get really bad perf if it does. What container should I use? ...

C++ Problem with destructor called when removing element from STL container

Say I have 2 containers storing pointers to the same objects... std::list<Foo*> fooList; std::vector<Foo*> fooVec; Lets say I remove an object from one of these containers via one if its methods, for example... std::vector<Foo*>::iterator itr = std::find( fooVec.begin(), fooVec.end(), pToObj ); fooVec.erase( itr ); CppReference ...

[STL] Enjoying Both Worlds: vector With insert/erase efficiency of list

I need a container that gives me a fast indexer and can also be very efficiency in arbitrary insertion and deletion operations (meaning at any position of the container). I remember reading about such container that uses buckets but I can't seem to find it or retrace the steps that lead me to it (I will start using the bookmarks, prom...

Comparing data bytewise in a effective way (with c++)

Question: Is there a more effective way to compare data bytewise than using the comparison operator of the c++ list container? I have to compare [large? 10kByte < size < 500kByte] amounts of data bytewise, to verify the integrity of external storage devices. Therefore I read files bytewise and store the values in a list of unsigned cha...

const and STL containers

The following std::vector code is giving errors int main() { std::vector<const double> VectDouble; VectDouble.push_back(2.34); VectDouble.push_back(2.33); VectDouble.push_back(2.32); for(std::vector<const double> VectDouble::iterator i=VectDouble.begin();i!=VectDouble.end();++i) std::cout<<*i; } ...

Removing something from a STL container without deconstructing it.

Ok, I'm using C++ STL containers (currently vector<customType*>). Now I need to remove elements from the container, but using erase deconstructs the object, which is bad, since I'm taking it off one, and putting it onto a variable doing some processing then onto another one. At the moment my code is quite nasty, and I'm just putting NUL...