stl

Where can I look at the C++ standard

I want to use STL with the current program I'm working on and the vendor doesn't support what I feel is a reasonable STL, working is not my idea of reasonable. I have been unable to find a C++ Standard or an STL standard that is not just an API that leaves me wondering if my interpretation is correct or if the vendor interpretation is c...

Why does the C++ STL not provide any "tree" containers?

Why does the C++ STL not provide any "tree" containers, and what's the best thing to use instead? I want to store a hierarchy of objects as a tree, rather than use a tree as a performance enhancement... ...

How to fill a vector with non-trivial initial values?

Hi all, I know how to fill an std::vector with non-trivial initial values, e.g. sequence numbers: void IndexArray( unsigned int length, std::vector<unsigned int>& v ) { v.resize(length); for ( unsigned int i = 0; i < length; ++i ) { v[i] = i; } } But this is a for-loop. Is there an elegant way to do this with ...

Partial sort of std::list

I have a linked list that I want to sort part of, eg: std::sort(someIterator, otherIterator, predicate); std::sort requires random-access iterators so this approach doesn't work. There is a specialisation std::list::sort, but that can only sort the entire list. I don't think I have enough access to the list members to write something ...

C++ STL: Container Recreation or Reuse after clearing?

In programming we face various situations where we are required to make use of intermediate STL containers as the following example depicts: while(true) { set < int > tempSet; for (int i = 0; i < n; i ++) { if (m.size() == min && m.size() <= max) { tempSet.insert(i); } } //Some co...

What is a safe equivalent of non-void STL erase?

Suppose I have a hash_map and a code like // i is an iterator i = hash_map.erase(i) But GCC's STL doesn't return iterator in erase, but a void. Now is a code like hash_map.erase(i++) safe (i.e. does not invalidate the iterator or does any other unexpected or unpleasant things)? Please note this is a hash_map. ...

Finding "best matching key" for a given key in a sorted STL container

Problem I have timestamped data, which I need to search based on the timestamp in order to get the one existing timestamp which matches my input timestamp the closest. Preferably this should be solved with the STL. boost::* or stl::tr1::* (from VS9 with Featurepack) are also possible. Example of timestamped data: struct STimestampedDat...

Polymorphic functors in std::for_each

I'm trying to use stl algorithm for_each without proliferating templates throughout my code. std::for_each wants to instantiate MyFunctor class by value, but it can't since its abstract. I've created a functor adapter class which passes a pointer around and then derefernces it when appropriate. My Question: Does the STL or Boost alrea...

Where can I look up the definition of size_type for vectors in the C++ STL?

It seems safe to cast the result of my vector's size() function to an unsigned int. How can I tell for sure, though? My documentation isn't clear about how size_type is defined. ...

Is list::size() really O(n)?

Recently, I noticed some people mentioning that std::list::size() has a linear complexity. According to some sources, this is in fact implementation dependent as the standard doesn't say what the complexity has to be. The comment in this blog entry says: Actually, it depends on which STL you are using. Microsoft Visual Studio V6 ...

Correct way to use custom functor with std::generate_n() algorithm?

The following code compiles correctly under VC++ 8 on XPSP3, but running it causes a runtime error. My header looks like: #include <stdexcept> #include <iterator> #include <list> template<typename T> class test_generator { public: typedef T result_type; //constructor test_generator() { std::generate_n( std:...

how-to initialize 'const std::vector<T>' like a c array

Is there an elegant way to create and initialize a const std::vector<const T> like const T a[] = { ... } to a fixed (and small) number of values? I need to call a function frequently which expects a vector<T>, but these values will never change in my case. In principle I thought of something like namespace { const std::vector<const T...

Violation reading location in std::map operator[]

I encountered a problem when running some old code that was handed down to me. It works 99% of the time, but once in a while, I notice it throwing a "Violation reading location" exception. I have a variable number of threads potentially executing this code throughout the lifetime of the process. The low occurrence frequency may be indica...

using STL to find all elements in a vector

I have a collection of elements that I need to operate over, calling member functions on the collection: std::vector<MyType> v; ... // vector is populated For calling functions with no arguments it's pretty straight-forward: std::for_each(v.begin(), v.end(), std::mem_fun(&MyType::myfunc)); A similar thing can be done if there's one...

Simplest, safest way of holding a bunch of const char* in a set?

I want to hold a bunch of const char pointers into an std::set container [1]. std::set template requires a comparator functor, and the standard C++ library offers std::less, but its implementation is based on comparing the two keys directly, which is not standard for pointers. I know I can define my own functor and implement the operat...

Is there a standard way to do findfirst, findnext with gcc on linux using stl?

I can't seem to find the _findfirst / findfirst, _findnext / findnext API on gcc for Linux, and would actually rather use the Standard Template Library (STL) for that if it is included there. Does anyone know what API there is available for listing files in a directory under Linux for C++ (gcc)? ...

How do I sort a std::vector by the values of a different std::vector?

I have several std::vector, all of the same length. I want to sort one of these vectors, and apply the same transformation to all of the other vectors. Is there a neat way of doing this? (preferably using the STL or Boost)? Some of the vectors hold ints and some of them std::strings. Pseudo code: std::vector<int> Index = { 3, 1, 2 ...

Relative performance of std::vector vs. std::list vs. std::slist?

For a simple linked list in which random access to list elements is not a requirement, are there any significant advantages (performance or otherwise) to using std::list instead of std::vector? If backwards traversal is required, would it be more efficient to use std::slist and reverse() the list prior to iterating over its elements? ...

STL sorted set where the conditions of order may change

I have a C++ STL set with a custom ordering defined. The idea was that when items get added to the set, they're naturally ordered as I want them. However, what I've just realised is that the ordering predicate can change as time goes by. Presumably, the items in the set will then no longer be in order. So two questions really: Is i...

How do I create a generic std::vector destructor?

Having a vector containing pointers to objects then using the clear function doesn't call the destructors for the objects in the vector. I made a function to do this manually but I don't know how to make this a generic function for any kind of objects that might be in the vector. void buttonVectorCleanup(vector<Button *> dVector){ B...