stl

delete a specific entry in the map,but the iterator must point to the next element after the deletion.

Duplicate: What happens if you call erase on a map element while iterating from begin to end How to filter items from a stdmap I have a map map1<string,vector<string>> i have a iterator for this map "itr". i want to delete the entry from this map which is pointed by "itr". i can use the function map1.erase(itr); after this lin...

What do you think is making this C++ code slow? (It loops through an ADODB recordset, converts COM types to strings, and fills an ostringstream)

This loop is slower than I would expect, and I'm not sure where yet. See anything? I'm reading an Accces DB, using client-side cursors. When I have 127,000 rows with 20 columns, this loop takes about 10 seconds. The 20 columns are string, int, and date types. All the types get converted to ANSI strings before they are put into th...

Chaining of ordering predicates (e.g. for std::sort)

You can pass a function pointer, function object (or boost lambda) to std::sort to define a strict weak ordering of the elements of the container you want sorted. However, sometimes (enough that I've hit this several times), you want to be able to chain "primitive" comparisons. A trivial example would be if you were sorting a collectio...

Inspecting STL containers in XCode

From googling around it looks like XCode (3.1 in my case) should be at least trying to give me a sane debug view of STL containers - or at least vectors. However, whenever I go to look at a vector in the debugger I just see M_impl, with M_start and M_finish members (and a couple of others) - but nothing in-between! (it's a debug build, ...

acceptable fix for majority of signed/unsigned warnings?

I myself am convinced that in a project I'm working on signed integers are the best choice in the majority of cases, even though the value contained within can never be negative. (Simpler reverse for loops, less chance for bugs, etc., in particular for integers which can only hold values between 0 and, say, 20, anyway.) The majority of ...

How to expose std::vector<int> as a Python list using SWIG?

I'm trying to expose this function to Python using SWIG: std::vector<int> get_match_stats(); And I want SWIG to generate wrapping code for Python so I can see it as a list of integers. Adding this to the .i file: %include "typemaps.i" %include "std_vector.i" namespace std { %template(IntVector) vector<int>; } I'm running SWIG ...

How do I sort a vector of pairs based on the second element of the pair?

If I have a vector of pairs std::vector<std::pair<int, int> > vec; is there and easy way to sort the list in increasing order based on the second element of the pair? I know I can write a little function object that will do the work, but is there a way to use existing parts of the STL and std::less to do the work directly? EDIT: I ...

Best way to merge multiple STL containers, removing duplicate elements?

I have two STL containers that I want to merge, removing any elements that appear more than once. For example: typedef std::list<int> container; container c1; container c2; c1.push_back(1); c1.push_back(2); c1.push_back(3); c2.push_back(2); c2.push_back(3); c2.push_back(4); container c3 = unique_merge(c1, c2); // c3 now contains the...

Does myVector.erase(myPtr) delete the object pointed by myPtr ?

If I have the following code Foo *f = new Foo(); vector<Foo*> vect; vect.push_back(f); // do stuff vect.erase(f); Did I create a memory leak ? I guess so, but the word erase gives the feeling that it is deleting it. Writing this, I am wondering if it is not a mistake to put a pointer in a STL vector. What do you think ? ...

How do you organise your STL headers?

I am working on a large project that uses the STL and have a question about your preferred way to organise your STL #includes. Do you prefer to #include each header in the source file it is used. For example, if both foo.cpp and bar.cpp require std::string, then both will #include <string>. Do you prefer to have a single header file t...

STL Migration issues (VS 2003 -> 2005)

I have just converted a project from Visual Studio 2003 to 2005 and although most of it 'converted' fine, I have a series of STL errors from the following line: void SomeFn( std::vector<CSomeObject*>::iterator it, std::vector<CSomeObject*>::iterator itBegin = NULL, std::vector<CSomeObject*>::iterator itEnd = NULL ); The Visual Studio ...

c++ STL set difference

Does the C++ STL set data structure have a set difference operator? ...

STL::Map - Walk through list or use find?

Say I have a method that needs to pull 8 values from a map with 100 elements in it. Which do you think would be preferable: Walk in a for loop from begin to end once, pulling the elements out by switching on the key? Or using find 8 times to get those values? ...

Generic vector of vectors in C++

Is there a good way in C++ to implement (or fake) a type for a generic vector of vectors? Ignore the issue of when a vector of vectors is a good idea (unless there's something equivalent which is always better). Assume that it does accurately model the problem, and that a matrix does not accurately model the problem. Assume also that te...

How do you insert with a reverse_iterator

I want to insert something into a STL list in C++, but I only have a reverse iterator. What is the usual way to accomplish this? This works: (of course it does) std::list<int> l; std::list<int>::iterator forward = l.begin(); l.insert(forward, 5); This doesn't work: (what should I do instead?) std::list<int> l; std::list<int>::revers...

Vector iterator not dereferencable

Hi, I have an abstract base class called Shape from which both Circle and Rectangle are derived, but when I execute the following code in VS 2005 I get the error Debug assertion failed. At the same time I have not overloaded == operator in any class Expression:Vector iterator not dereferencable, what is the reason for this. vector<S...

Store 2D points for quick retrieval of those inside a rectangle

I have a large number of 2D points and I want to quickly get those that lie in a certain rectangle. Let's say a '.' is any point and 'X' is a point I want to find inside a rectangle which has 'T' as TopLeft and 'B' as BottomRight points: . . . . . . . T-----+ . . | X X | . . +-----B . . . . . . . I have tried a std::set with a sort fu...

Cleaning up an STL list/vector of pointers

What is the shortest chunk of C++ you can come up with to safely clean up a vector or list of pointers? (assuming you have to call delete on the pointers?) list<Foo*> foo_list; I'd rather not use Boost or wrap my pointers with smart pointers. ...

Forward declare an STL container?

Is it possible to forward declare an STL container in a header file? For example, take the following code: #include <vector> class Foo { private: std::vector<int> container_; ... }; I want to be able to do something like this: namespace std { template <typename T> class vector; } class Foo { private: std::vector<in...

STL Vector comparison issue

With a vector defined as std::vector<std::string>, Wondering why the following is valid: if ( vecMetaData[0] != "Some string" ) { ... But not this: switch ( vecMetaData[1] ) { ... Visual studio complains : error C2450: switch expression of type 'std::basic_string<_Elem,_Traits,_Ax>' is illegal 1> with 1> [ 1...