stl

bad_alloc error when using std::string

Hi all.. I'm currently working on a project that depends on me providing a path to a file (eg. "C:\Path.pth"). Now, I had everything working yesterday by calling my std::string with: std::string path("C:\\Path.pth"); -- But now it doesn't work!? It throws a bad_alloc. Seems like the '\' character is the problem. I even tried using \x5...

Iterators.. why use them?

In the STL library some containers have iterators and it is commonly held that they are a superior way of iterating through these containers rather than simple for loops e.g. for ( int i=0; i < vecVector.size(); i++ ) { .. } Can anyone tell me why and in what cases I should use iterators and in what cases the code snippet above plea...

How to filter items from a std::map?

I have roughly the following code. Could this be made nicer or more efficient? Perhaps using std::remove_if? Can you remove items from the map while traversing it? Can we avoid using the temporary map? typedef std::map<Action, What> Actions; static Actions _actions; bool expired(const Actions::value_type &action) { return <something>...

What are the Complexity guarantees of the standard containers?

Apparently ;-) the standard containers provide some form of guarantees. What type of guarantees and what exactly are the differences between the different types of container? Working from the SGI Page I have come up with this: Container Types: ================ Container: Forward Container Reverse Container Rando...

Position in Vector using STL

Hi, im trying to locate the position of the minimum value in a vector, using STL find algorithm (and the min_element algorithm), but instead of returning the postion, its just giving me the value. E.g, if the minimum value is it, is position will be returned as 8 etc. What am I doing wrong here? int value=*min_element(v2.begin(),v2.end(...

Comparison Functor Types vs. operator<

In the Google C++ Style Guide, the section on Operator Overloading recommends against overloading any operators ("except in rare, special circumstances"). Specifically, it recommends: In particular, do not overload operator== or operator< just so that your class can be used as a key in an STL container; instead, you should cr...

if(str1==str2) versus if(str1.length()==str2.length() && str1==str2)

I've seen second one in another's code and I suppose this length comparison have been done to increase code productivity. It was used in a parser for a script language with a specific dictionary: words are 4 to 24 letters long with the average of 7-8 lettets, alphabet includes 26 latin letters plus "@","$" and "_". Length comparison we...

Do I need to protect read access to an STL container in a multithreading environment?

I have one std::list<> container and these threads: One writer thread which adds elements indefinitely. One reader/writer thread which reads and removes elements while available. Several reader threads which access the SIZE of the container (by using the size() method) There is a normal mutex which protects the access to the list fro...

How do you iterate backwards through an STL list ?

I'm writing some cross-platform code between Windows and Mac. If list::end() "returns an iterator that addresses the location succeeding the last element in a list" and can be checked when traversing a list forward, what is the best way to traverse backwards? This code workson the Mac but not on Windows (can't decrement beyond first e...

Is Iterator initialization inside for loop considered bad style, and why?

Typically you will find STL code like this: for (SomeClass::SomeContainer::iterator Iter = m_SomeMemberContainerVar.begin(); Iter != m_SomeMemberContainerVar.end(); ++Iter) { } But we actually have the recommendation to write it like this: SomeClass::SomeContainer::iterator Iter = m_SomeMemberContainerVar.begin(); SomeClass::SomeCont...

What are some convincing arguments to upgrade from Visual Studio 6?

I have a client who is still using Visual Studio 6 for building production systems. They write multi-threaded systems that use STL and run on mutli-processor machines. Occasionally when they change the spec of or increase the load on one of their server machines they get 'weird' difficult to reproduce errors... I know that there are ...

How is tr1::reference_wrapper useful?

Hi, recently I've been reading through Scott Meyers's excellent Effective C++ book. In one of the last tips he covered some of the features from TR1 - I knew many of them via Boost. However, there was one that I definitely did NOT recognize: tr1::reference_wrapper. How and when would I use tr1::reference_wrapper? ...

What is a good use case for tr1::result_of?

I hear that tr1::result_of gets used frequently inside of Boost... I'm wondering if there are any good (simple) use cases for tr1::result_of I can use at home. ...

string c_str() vs. data()

Hi, I have read several places that the difference between c_str() and data() (in STL and other implementations) is that c_str is always null terminated while data is not. As far as I have seen in the actual implementation, they either do the same or data() calls c_str(). What am I missing here? Which one is more correct to use in whic...

What is the best implementation of STL for VS2005?

I'm currently using default implementation of STL for VS2005 and I'm not really satisfied with it. Perhaps there is something better? ...

Calling a static member function of a C++ STL container's value_type

Hi, I'm trying to get my head around why the following doesn't work. I have a std::vector and I want to call a static member function of it's contained value_type like so: std::vector<Vector> v; unsigned u = v.value_type::Dim(); where Vector is in fact a typedef for a templated type: template <typename T, unsigned U> class SVector; ...

Visual c++ "for each" portability

I only just recently discovered that Visual C++ 2008 (and perhaps earlier versions as well?) supports for each syntax on stl lists et al to facilitate iteration. For example: list<Object> myList; for each (Object o in myList) { o.foo(); } I was very happy to discover it, but I'm concerned about portability for the dreaded day when ...

C Analog To STL

Just because I'm curious--is there any C analog to the functionality of the STL in C++? I've seen mention of a GTK+ library called glib that a few people consider fills the bill but are there other libraries that would provide STL functionality in C? ...

how to concat two stl vectors?

how to concat two stl vectors? ...

Is there a convenient way to wrap std::pair as a new type?

Often times I find myself using std::pair to define logical groupings of two related quantities as function arguments/return values. Some examples: row/col, tag/value, etc. Often times I should really be rolling my own class instead of just using std::pair. It's pretty easy to see when things start breaking down - when the code becomes ...