stl

stl ordering - strict weak ordering

Why does STL work with a comparison function that is strict weak ordering? Why can't it be partial ordering? ...

Has the STL changed much?

I'm wanting to become conversant in the use of the Standard Template Library. If I come across a general reference or beginner's guide published around 1995-97, can I rely on the information in it? How much has STL changed in the last dozen years? ...

Pre-allocate space for C++ STL queue

I'm writing a radix sort algorithm using queues and I would like to have a STL queue allocate space before I start adding things to the queue so that I can avoid constant dynamic resizing operations. Even though this doesn't exist, I want something with the effect of... queue<int> qs(N); for(int i=0;i<N;++i) qs.push(rand()); in su...

How to determine whether output iterator has been modified

I have a template function that takes the following form: template < class ITER1, class ITER2 > bool example(ITER1 Input1, ITER1 Input2, ITER2 Output) { ITER2 OrigOutput(Output); // ... std::copy(Input1, Input2, Output); return (OrigOutput != Output); } And I'm calling example() like this: std::vector < int > Input...

Any built-in function to test if 4 is in [1,2,3,4] (vector)

In Ruby I can do: [1,2,3,4].include?(4) #=>True In Haskell I can do : 4 `elem` [1,2,3,4] #=> True What should I do in C++? ...

Can an STL map iterator go out of bounds through incrementing?

For associative containers, can the ++ operator send an iterator past the end of a collection? Example: map<UINT32, UINT32> new_map; new_map[0] = 0; new_map[1] = 1; map<UINT32, UINT32> new_iter = new_map.begin(); ++new_iter; ++new_iter; ++new_iter; ++new_iter; ++new_iter; ++new_iter; ++new_iter; At the end of this, does new_iter ==...

what's the correct way of writing this code?

typedef boost::shared_ptr<config_value_c> config_value_ptr; typedef std::vector<config_value_ptr> config_value_vec; config_value_vec config; typeof (config.iterator ()) it = config.iterator (); I want to extract an iterator to an array of boost pointers to class config_value_c. I know I can specify the iterator as std::vector<config...

Using STL to bind multiple function arguments

In the past I've used the bind1st and bind2nd functions in order to do straight forward operations on STL containers. I now have a container of MyBase class pointers that are for simplicities sake the following: class X { public: std::string getName() const; }; I want to call the following static function using for_each and bin...

How to get the address of the std::vector buffer start most elegantly?

I want to use std::vector for dynamically allocating memory. The scenario is: int neededLength = computeLength(); // some logic here // this will allocate the buffer std::vector<TCHAR> buffer( neededLength ); // call a function that accepts TCHAR* and the number of elements callFunction( &(buffer[0]), buffer.size() ); The code ...

Own Object as Data in stl Multimap

I'm writing an application, where I want to store strings as keys ans a custom Object as value multimap<string, owncreatedobject> mymap; Compiling does well, but I get a "Segmentation fault" when using the funtion insert during runtime. mymap.insert(string,myobject); --> Segmentation Error A already added a copyconstructor an assig...

How do I find the largest int in a std::set<int> ?

I have a std::set, what's the proper way to find the largest int in this set ? ...

documentation for STL

I have spent the last several years fighting tooth and nail to avoid working with C++ so I'm probably one of a very small number of people who likes systems programming and template meta programming but has absolutely no experience when it comes to the STL and very little C++ template experience. Does anyone know of a good document for...

Memory allocation and deallocation across dll boundaries

I understand that memory allocations made in one dll then subsequently free'd in another can cause all sort of problems, especially regarding the CRT. These sorts of problems are especially problematic when it comes to exporting STL containers. We've experienced these sorts of problems before (when writing custom Adobe plugins that lin...

Why STL containers are preferred over MFC containers?

Previously I used to use MFC collection classes such CArray and CMap. After a while I switched to STL containers and have been using them for a while. Although I find STL much better, I am unable to pin point the exact reasons for it. Some of the reasoning such as : It requires MFC: does not hold because other parts of my program uses ...

problems using STL std::transform from cygwin g++

I am running g++(gcc version 3.4.4) on cygwin. I can't get this small snippet of code to compile. I included the appropriate headers. int main(){ std::string temp("asgfsgfafgwwffw"); std::transform(temp.begin(), temp.end(), temp.begin(), std::toupper); std::cout <<...

Error while inserting pointer to a vector

Hi I have the following CPP code snippet and the associated error message: Code snippet struct node{ char charVal; bool childNode; struct node *leftChild; struct node *rightChild; }; vector<std::pair<int,struct node*> > nodeCountList; struct node *nodePtr = new struct node; nodeCountList.pus...

C++ Vectors Problem

well i have most probably an extremly stupid problem but could not figure it out and I m about to lose my sanity hope someone can help vector<CvMat*> sample; for(int x = 0; x < 29; x += 2) { for(int b = 0; b < 22; b += 2) { cvmSet(g, 0, b, cvmGet(NormalVector, 0, x + b)); cvmSet(g, 0, b + 1, cvmGet(NormalVector, 0, ...

C++ Vector Pointers to Objects

I'm using a vector of pointers to objects. These objects are derived from a base class, and are being dynamically allocated and stored. For example, I have something like: vector<Enemy*> Enemies; and I'll be deriving from the Enemy class and then dynamically allocating memory for the derived class, like this: enemies.push_back(new M...

Is the C++ STL std::set thread-safe?

I've a question about the thread safety of std::set. As far as I know I can iterate over a set and add/erase members and that doesn't invalidate the iterators. But consider following scenario: thread 'A' iterates over a set of shared_ptr<Type> thread 'B' occasionally adds items to this set. I've experienced segfaults as the program...

Should the "this" pointer and smart pointers be mixed?

How should I avoid using the "this" pointer in conjunction with smart pointers? Are there any design patterns/general suggestions on working around this? I'm assuming combining the two is a no-no since either: you're passing around a native pointer to a smart pointer-managed object which defeats the point of using the smart pointers ...