stl

c++ - should I use the algorithm or hand-code it in this case?

Ok, someone tell me which would be better. I need to |= the elements of one vector with another. That is, I want to void orTogether(vector<char>& v1, const vector<char>& v2) { typedef vector<char>::iterator iter; for (iter i = v1.begin(), iter j = v2.begin() ; i != v1.end(); ++i, ++j) *i |= *j; } I can't use for_each d...

Call member function on each element in a container.

Hello, This question is a matter of style, since you can always write a for loop or something similar; however, is there a less obtrusive STL or BOOST equivalent to writing: for (container<type>::iterator iter = cointainer.begin(); iter != cointainer.end(); iter++) iter->func(); ? Something like (imagined) this: call_for...

How can i estimate memory usage of stl::map?

For example, I have a std::map with known sizeof(A) and sizefo(B), while map has N entries inside. How would you estimate its memory usage? I'd say it's something like (sizeof(A) + sizeof(B)) * N * factor But what is the factor? Different formula maybe? Update: Maybe it's easier to ask for upper bound? ...

accumulate the sum of elements in map, using value

Say I have a struct SMyStruct { int MULT; int VAL; }; std::map<std::string, SMyStuct*> _idToMyStructMap; Now I want to calculate total of all SMyStuct, where total is defined as MULT1 *VAL1 + MULT2 *VAL2 for each elements in the idToMyStructMap. Seems like accumulate function is a natural choice. Please suggest. thanks ...

Using map containing set as a value.

Basically I have, typedef map<std::string, set<double> > MAP_STRING_TO_SET; What is the best way to update (add or remove value) the set with a new value without causing the set to be copied? The only viable solution I see is to use map<std::string, set<double>* > -- something I don't want to do. Thanks ...

fast way to parse a configuration

Say you have char *= "name:454"; What is the best way to parse name and the number, thus std:string id would equal to "name"; double d would equal to 454; STL please, no boost. ...

Iterator access performance for STL map vs. vector?

What is the performance difference between using an iterator to loop through an STL map, versus a vector? I'd like to use the map key for insertion, deletion, and some accesses, but I also need to do regular accesses to every element in the map. ...

How much of STL is too much?

I am using a lot of STL code with std::for_each, bind, and so on, but I noticed that sometimes STL usage is not good idea. For example if you have a std::vector and want to do one action on each item of the vector, your first idea is to use this: std::for_each(vec.begin(), vec.end(), Foo()) and it is elegant and ok, for a while. Bu...

Most useful or amazing STL short liners

I'm looking for practical and educational samples of C++ / STL code fitting in few lines. My actual favorites are: Empty a vector freeing its reserved memory: vector <...>().swap (v) (swap with a temporary) Copy a map to a vector: map<T1, T2> myMap; vector< pair<T1, T2> > myVec(myMap.begin(), myMap.end()); // or myVec.assign(myMap....

Help storing an intrusive_ptr of a template class in a std::map

I have a small template class of type Locker contained within a boost::intrusive_ptr that I want to store inside a std::map: template <typename T> bool LockerManager<T>:: AddData(const std::string& id, T* pData) { boost::intrusive_ptr<Locker<T> > lPtr(Locker<T>(pData)); // Line 359 - compiles mMap.insert(make_pair(id, lPtr)); /...

Mapping between stl C++ and C# containers

Can someone point out a good mapping between the usual C++ STL containers such as vector, list, map, set, multimap... and the C# generic containers? I'm used to the former ones and somehow I've accustomed myself to express algorithms in terms of those containers. I'm having some hard time finding the C# equivalent to those. Thank you! ...

Using local classes with STL algorithms

I have always wondered why you cannot use locally defined classes as predicates to STL algorithms. In the question: Approaching STL algorithms, lambda, local classes and other approaches, BubbaT mentions says that 'Since the C++ standard forbids local types to be used as arguments' Example code: int main() { int array[] = { 1, 2, ...

Convert iterator to pointer?

I have a std::vector with n elements. Now I need to pass a pointer to a vector that has the last n-1 elements to a function. For example, my vector<int> foo contains (5,2,6,87,251). A function takes vector<int>* and I want to pass it a pointer to (2,6,87,251). Can I just (safely) take the iterator ++foo.begin(), convert it to a pointer...

size() Vs empty() in vector - why empty() is preferred?

While debugging something, I saw the STL vector::empty() implementation: bool empty() const {return (size() == 0); } I believe, whenever we are probing the emptiness of vector it is always recommended to use empty over size(). But seeing that implementation, I am wondering, what is the benefit of doing so? Instead, there is a fun...

What is the meaning of numeric_limits<double>::digits10

What is the precise meaning of numeric_limits::digits10? Some other related questions in stackoverflow made me think it is the maximum precision of a double, but The following prototype starts working (sucess is true) when precision is greater that 17 ( == 2+numeric_limits::digits10) With STLPort, readDouble==infinity at the end; with...

Structure for hierarchal Component storage

I've been batting this problem around in my head for a few days now and haven't come to any satisfactory conclusions so I figured I would ask the SO crew for their opinion. For a game that I'm working on I'm using a Component Object Model as described here and here. It's actually going fairly well but my current storage solution is turni...

Is STL vector a better version of realloc ?

In C++, I believe, a better way of dealing with reallocation is to use a STL vectors, as it guarantees the contiguous storage locations. I have couple question to understand the difference: Is there any scenario in which I need to prefer realloc over vector ? Is there anything else ( apart from vector ) which is equivalent to realloc...

Are const_iterators faster ?

Our coding guidelines say prefer const_iterator, because they are little faster compared to normal iterator. It seems like compiler optimizes the code when you use the const _iterator. Is it really correct ? If yes, what really happens internally to make const_iterator takes the edge?. EDIT: I wrote small test to check const_iterator ...

Concatenating C++ iterator ranges into a const vector member variable at construction time

I have a class X, which I provide a snippet of here: class X { public: template <typename Iter> X(Iter begin, Iter end) : mVec(begin, end) {} private: vector<Y> const mVec; }; I now want to add a new concatenating constructor to this class, something like: template <typename Iter1, typename Iter2> X(Iter1 begin1, Ite...

Why doesn't auto_ptr construction work using = syntax

I ran into a compiler error that didn't make much sense to me: #include <memory> using namespace std; auto_ptr<Table> table = db->query("select * from t"); error: conversion from 'Table*' to non-scalar type 'std::auto_ptr< Table>' requested However, the following line does work: auto_ptr<Table> table(db->query("select * from t")); ...