stl

How to maintain 2 lists of object

Hi, My c++ program needs to maintain 2 list of objects. list<A*> inuse; list<A*> free; So objects A can even in 'inuse' list or in 'free' list, but not both. http://www.cplusplus.com/reference/stl/list/ I am think of using 'list' as the data structure for my lists. My questions are why i can't randomly access an elmenet in the l...

How to shrink-to-fit an std::vector in a memory-efficient way?

I would like to 'shrink-to-fit' an std::vector, to reduce its capacity to its exact size, so that additional memory is freed. The standard trick seems to be the one described here: template< typename T, class Allocator > void shrink_capacity(std::vector<T,Allocator>& v) { std::vector<T,Allocator>(v.begin(),v.end()).swap(v); } The w...

Is it a good idea to index an STL map with a pair?

Hi Everyone, I'm just wondering if it is a good idea to make a data structure like std::map< std::pair<int,int>,std::string > Just wondering how the pairs would be ordered internally... :S Thanks! ...

Indices instead of pointers in STL containers?

Due to specific requirements [*], I need a singly-linked list implementation that uses integer indices instead of pointers to link nodes. The indices are always interpreted with respect to a vector containing the list nodes. I thought I might achieve this by defining my own allocator, but looking into the gcc's implementation of , they...

design patterns used in STL(standard template library)

I am learning STL and design patterns . i wanted to know is there any document or link that explains how design patterns are implemented in STL i did the google but not able to get much data ...

STL map--> sort by value?

Hi I wonder how can I implement the STL map sorting by value. For example, I have a map m map<int, int>; m[1] = 10; m[2] = 5; m[4] = 6; m[6] = 1; and then.. I'd like to sort that with the m's value. So, if I print the map, I'd like to get the result like m[6] = 1 m[2] = 5 m[4] = 6 m[1] = 10 this. How can I sort like this way? Is...

[C++] How should I compare pairs of pointers (for sort predicate)

I have a STL container full of billions of the following objects pair<SomeClass*, SomeClass*> I need some function of the following form /*returns items sorted biggest first */ bool sortPredicate (pair<SomeClass*, SomeClass*>two, pair<SomeClass*, SomeClass*> one) { return ???; } Is there some trick I can use to very quickly com...

std::list iterator: get next element

I'm trying to build a string using data elements stored in a std::list, where I want commas placed only between the elements (ie, if elements are {A,B,C,D} in list, result string should be "A,B,C,D". This code does not work: typedef std::list< shared_ptr<EventDataItem> > DataItemList; // ... std::string Compose(DataItemList& dilList) {...

Trouble passing a template function as an argument to another function in C++

Source of the problem -Accelerated C++, problem 8-5 I've written a small program that examines lines of string input, and tallies the number of times a word appears on a given line. The following code accomplishes this: #include <map> #include <iostream> #include <string> #include <vector> #include <list> #include <cctype> #include <i...

Easier debugging stl array

In MSVC++ I have a vector. Whenever you go out of bounds of the vector (in debug mode, launched as "Start Debugging"), when you step out of bounds of the vector the program halts with a dialog box: Microsoft Visual C++ Debug Library ==== Debug Assertion Failed! Expression: Vector subscript out of range Abort | Retry | Ignore So w...

std::basic_string full specialization (g++ conflict)

I am trying to define a full specialization of std::basic_string< char, char_traits<char>, allocator<char> > which is typedef'd (in g++) by the <string> header. The problem is, if I include <string> first, g++ sees the typedef as an instantiation of basic_string and gives me errors. If I do my specialization first then I have no issues...

Difference among STLPort and SGI STL

Recently, I was buzzed by the following problem STL std::string class causes crashes and memory corruption on multi-processor machines while using VC6. I plan to use an alternative STL libraries instead of the one provided by VC6. I came across 2 libraries : STLPort and SGI STL I was wondering what is the difference between the 2. Whi...

STL container to pop() by priority?

I'm writing a thread-pool for Qt as QRunnable doesn't handle event loops in new threads. Not too familiar with STL, what would be the best way to pop() something by priority? Priority should probably be a property of MyRunnable imo, but I can always give that info to an STL container when adding the runnable to the queue. ...

limit stl's vector max_size

Hi How can I limit STL vector's max_size? Eventually by specialization. An example would be welcome. ...

Is there a sorted_vector class, which supports insert() etc.?

Often, it is more efficient to use a sorted std::vector instead of a std::set. Does anyone know a library class sorted_vector, which basically has a similar interface to std::set, but inserts elements into the sorted vector (so that there are no duplicates), uses binary search to find elements, etc.? I know it's not hard to write, but p...

Is there any way to limit the size of an STL Map?

I want to implement some sort of lookup table in C++ that will act as a cache. It is meant to emulate a piece of hardware I'm simulating. The keys are non-integer, so I'm guessing a hash is in order. I have no intention of inventing the wheel so I intend to use std::map for this (though suggestions for alternatives are welcome). The q...

How to load/save C++ class instance (using STL containers) to disk

I have a C++ class representing a hierarchically organised data tree which is very large (~Gb, basically as large as I can get away with in memory). It uses an STL list to store information at each node plus iterators to other nodes. Each node has only one parent, but 0-10 children. Abstracted, it looks something like: struct node { pub...

Virtual methods as Comp function to sort

Hello everyone! I'm new to C++ and i'm trying to use std::sort function to sort a vector of Solutions. The code is something like this (solution list is a *vector): void SolutionSet::sort(Comparator &comparator) { std::sort(solutionsList_->begin(), solutionsList_->end(), &comparator::compare); } The comparator param is a Comparat...

Composite pattern in C++ problem

Hello! I have to work with an application in C++ similar to a phone book: the class Agenda with an STL list of Contacts.Regarding the contacts hierarchy,there is a base-class named Contact(an abstract one),and the derived classes Friend and Acquaintance(the types of contact). These classes have,for instance, a virtual method called...

Adapting Map Iterators Using STL/Boost/Lambdas

Consider the following non-working code: typedef map<int, unsigned> mymap; mymap m; for( int i = 1; i < 5; ++i ) m[i] = i; // 'remove' all elements from map where .second < 3 remove_if(m.begin(), m.end(), bind2nd(less<int>(), 3)); I'm trying to remove elements from this map where .second < 3. This obviously isn't written correctl...