stl

Call c++ member function with each element in a list?

I have a list of Thing and a Controller that I want to notify() with each of the things. The code below works: #include <algorithm> #include <iostream> #include <tr1/functional> #include <list> using namespace std; class Thing { public: int x; }; class Controller { public: void notify(Thing& t) { cerr << t.x << endl; } }; class N...

why ostringstream could not work well in multithreading environment

Maybe something is weird. When I use STL ostringstream class in my multithreading environment I found that the execution time of each thread increased linearly as the thread number increased. I don't know why this happened. I try to check the ostringstream source code but could not find any synchronization code. Are there some synchroniz...

Alternative to template declaration of typedef

I'm trying to accomplish namespace NTL { typedef std::valarray vector; } through standard C++. I know it's not allowed, but I need a quick and easy way (without reimplementing all functions, operators, overloads, etc.) to get a template typedef. I am now doing a template class Vector which has a valarray as data member, but that ...

Using a settable tolerance in the comparison object for an STL set

I want a tool to remove duplicate nodes ("nodes" in the finite element sense, simply a point in space with certain coordinates). Basically, I want to be able to take a collection of nodes, and reduce it by eliminating extra nodes that are within a certain tolerance in distance of other nodes. I figure that an STL set of nodes sorted by...

Lists of member pointer functions

Say I have a class: class A { public: void doSomething(); } Where doSomething does something that explicitly relies on the internal state of the instance of A. Now, I have a situation where I have a bunch of things of type A laying around, but I only want to call doSomething from a strict subset of them, so I want to stick them on ...

Returning a c++ std::vector without a copy?

Is it possible to return a standard container from a function without making a copy? Example code: std::vector<A> MyFunc(); ... std::vector<A> b = MyFunc(); As far as I understand, this copies the return value into a new vector b. Does making the function return references or something like that allow avoiding the copy? ...

Pointer to c++ string

Hi, in the following code, I am using a pointer to a c++ string in the change() function. Is there anyway to use the string class' operators when working with a pointer to a string? For example, at() works for the [] operator, but is there any way to use the [] operator? #include <string> #include <iostream> using namespace std; void...

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; } ...

Is it meaningful to optimize i++ as ++i to avoid the temporary variable?

Someone told me that I can write for (iterator it = somecontainer.begin(); it != somecontainer.end(); ++it) instead of for (iterator it = somecontainer.begin(); it != somecontainer.end(); it++) ...since the latter one has the cost of an extra unused temporary variable. Is this optimization useful for modern compiler? Do I need to...

Which sorted STL container to use for fast insert and find with a special key?

Hi, I have some data with a key associated with each data item. The key is made of two parts: let's call them color and id. I want to iterate the container by color to speed up rendering and I also want to find items in the container by id alone. I tried using std::map for this with a key class MyKey { public: int color; int id; ...

Error with T::iterator, where template parameter T might be vector<int> or list<int>

I'm trying to write a function to print a representation of common STL containers (vector, list, etc..). I gave the function a template parameter T which, for example, might represent vector. I'm having problems getting an iterator of type T. vector<int> v(10, 0); repr< vector<int> >(v); ... template <typename T> void repr(const T & ...

What's generally the size limit to switch from a vector to a deque?

I recent wrote this post: http://stackoverflow.com/questions/3737138/how-best-to-store-very-large-2d-list-of-floats-in-c-error-handling Some suggested that I implemented my 2D list-like structure of floats as a vector, others said a deque. From what I gather vector requires continuous memory, but is hence more efficient. Obviously thi...

STL, list, iterator

Hello everybody! Exscusme buy my English ))) I have this problem: I must iterate items in STL list with posible editing items: remove, add, edit. This is my approach: void MoveOnNewPlace(Tree& parent, Tree& child) { Tree *temp=&parent; while(temp->GetParent()!=temp && temp->GetItem().GetChar()=='(') temp=temp->GetPar...

Visual C++'s implementation of std::map

How is std::map implemented in Visual C++? I know that some tree data structures just flag nodes as deleted when they are removed, instead of removing them right away. I need to make sure that my elements are never compared to elements which are no longer in the map. EDIT: I know that the implementation is probably correct wrt. the co...

std::vector iterator invalidation

There have been a few questions regarding this issue before; my understanding is that calling std::vector::erase will only invalidate iterators which are at a position after the erased element. However, after erasing an element, is the iterator at that position still valid (provided, of course, that it doesn't point to end() after the e...

is it possible to design a better collections library than stl for c++?

the title says it all... now answer! ...

How to erase the last n elements in the C++ map?

Is there a nice and simple way to find nth element in C++ std::map? Particularly I'm looking for an algorithm to erase the last k elements from the map. That would make sense to retrieve the iterator to the nth element and call std::map::erase. The requirement is that complexity doesn't suffer - that should be possible to erase the eleme...

STLish lower_bound function for Radix/Patricia Trie

Lately I've been studying Patricia tries, and working with a really good C++ implementation which can be used as an STL Sorted Associative Container. Patricia tries differ from normal binary trees because leaf nodes have back pointers which point back to internal nodes. Nonetheless, it's possible to traverse a Patricia trie in alphabet...

Mistake in calling std::stable_sort ?

struct SimGenRequest { int wakeup_mfm_; double value_; bool operator < ( const SimGenRequest & r2 ) const { return ( wakeup_mfm_ < r2.wakeup_mfm_ ) ; } }; Use : std::stable_sort ( all_requests_.begin ( ), all_requests_.end ( ) ); Works ( compiles ). But struct SimGenRequest { int wakeup_mfm_; doubl...

how to define a vector with a functor

hey, i implemented the following functor: struct CompareCatId : public std::binary_function<Vehicle*, Vehicle*, bool> { bool operator()(Vehicle* x, Vehicle* y) const { if(x->GetVehicleType() > y->GetVehicleType()) return true; else if (x->GetVehicleType() == y->GetVehicleType() && x->GetLicenseNumb...