stl

How to keep only duplicates efficiently?

Given an STL vector, output only the duplicates in sorted order, e.g., INPUT : { 4, 4, 1, 2, 3, 2, 3 } OUTPUT: { 2, 3, 4 } The algorithm is trivial, but the goal is to make it as efficient as std::unique(). My naive implementation modifies the container in-place: My naive implementation: void not_unique(vector<int>* pv) { if (!...

Trying to write a std::iterator : Compilation error

I am trying to write an std::iterator for the CArray<Type,ArgType> MFC class. This is what I have done till now: template <class Type, class ArgType> class CArrayIterator : public std::iterator<std::random_access_iterator_tag, ArgType> { public: CArrayIterator(CArray<Type,ArgType>& array_in, int index_in = 0) : m_pArray(&arr...

C++: Appending a vector to a vector

Assuming I have 2 STL vectors: vector<int> a; vector<int> b; Let's also say the both have around 30 elements. How do I add the vector b to the end of vector a? The dirty way would be iterating through b and adding each element via push_back, though I wouldn't like to do that! ...

C++ dictionary/map with added order

I want to have something similar to map but while iterating I want them to be in the same order as it is added. Example map.insert("one", 1); map.insert("two", 2); map.insert("three", 3); While iterating I want the items to be like "one", ""two", "three"..By default, map doesn't provide this added order. How to get the map elements t...

Array performance question

I am very familiar with STL vector (and other container) performance guarantees, however I can't seem to find anything concrete about plain arrays. Are pointer arithmetic and [] methods constant or linear time? ...

Sort a list of pointers.

Hello all, Once again I find myself failing at some really simple task in C++. Sometimes I wish I could de-learn all I know from OO in java, since my problems usually start by thinking like Java. Anyways, I have a std::list<BaseObject*> that I want to sort. Let's say that BaseObject is: class BaseObject { protected: int id; public...

regarding C++ stl container swap function

I recently learned that all stl containers have swap function: i.e. c1.swap(c2); will lead to object underlying c1 being assigned to c2 and vice versa. I asked my professor if same is true in case of c1 and c2 being references. he said same mechanism is followed. I wonder how it happens since c++ references cannot be reseted. ...

shared memory STL maps

Hello, I am writing an Apache module in C++. I need to store the common data that all childs need to read as a portion of shared memory. Structure is kind of map of vectors, so I want to use STL map and vectors for it. I have written a shared allocator and a shared manager for the purpose, they work fine for vectors but not for maps, bel...

Pointers into elements in a container

Say I have an object: struct Foo { int bar_; Foo(int bar) bar_(bar) {} }; and I have an STL container that contains Foos, perhaps a vector, and I take // Elsewhere... vector<Foo> vec; vec.push_back(Foo(4)); int *p = &(vec[0].bar_) This is a terrible idea, right? The reason is that vector is going to be storing its el...

std::binary_function - no match for call?

Hi, this is my code: #include <iostream> #include <functional> using namespace std; int main() { binary_function<double, double, double> operations[] = { plus<double>(), minus<double>(), multiplies<double>(), divides<double>() }; double a, b; int choice; cout << "Enter two numbers" << endl; cin >> a >> b; cout << "E...

Using a checked STL implementation, anything available for free?

Have you used a checked STL implementation? Did it find bugs you were not expecting? Is there one I can try on Linux for free? ...

Populate a vector with all multimap values with a given key

Given a multimap<A,B> M what's a neat way to create a vector<B> of all values in M with a specific key. e.g given a multimap how can I get a vector of all strings mapped to the value 123? An answer is easy, looping from lower->upper bound, but is there a neat loop-free method? ...

C++ STL vector iterator... but got runtime error

I'm studying STL and made win32 project.. But I got stuck in runtime error.. I tried to debug it but.. (partial code) vector<Vertex> currPoly=polygons.back(); vector<Vertex>::iterator it; for(it=currPoly.begin();it!=currPoly.end();++it){ vector<Vertex>::iterator p1; vector<Vertex>::iterator n1; vector<Vertex>::iterator ...

Is it a good idea to create an STL iterator which is noncopyable?

Most of the time, STL iterators are CopyConstructable, because several STL algorithms require this to improve performance, such as std::sort. However, I've been working on a pet project to wrap the FindXFile API (previously asked about), but the problem is it's impossible to implement a copyable iterator around this API. A find handle c...

C++ STL-conforming Allocators

What allocators are available out there for use with STL when dealing with small objects. I have already tried playing with pool allocators from Boost, but got no performance improvement (actually, in some cases there was considerable degradation). ...

C++ STL Map vs Vector speed

In the interpreter for my experimental programming language I have a symbol table. Each symbol consists of a name and a value (the value can be e.g.: of type string, int, function, etc.). At first I represented the table with a vector and iterated through the symbols checking if the given symbol name fitted. Then I though using a map,...

C++ min heap with user-defined type.

Hi, I am trying to implement a min heap in c++ for a struct type that I created. I created a vector of the type, but it crashed when I used make_heap on it, which is understandable because it doesn't know how to compare the items in the heap. How do I create a min-heap (that is, the top element is always the smallest one in the heap) fo...

Bad_alloc exception when using new for a struct c++

Hi, I am writing a query processor which allocates large amounts of memory and tries to find matching documents. Whenever I find a match, I create a structure to hold two variables describing the document and add it to a priority queue. Since there is no way of knowing how many times I will do this, I tried creating my structs dynamical...

should std::auto_ptr<>::operator = reset / deallocate its existing pointee ?

I read here about std::auto_ptr<>::operator= Notice however that the left-hand side object is not automatically deallocated when it already points to some object. You can explicitly do this by calling member function reset before assigning it a new value. However, when I read the source code for header file C:\Program Fil...

How to see the contents of std::map in Visual C++ .NET (Visual Studio 2003) while debugging?

I need to see the contents of a std::map variable while debugging. However, if i click on it in the Autos/Locals Tab, I see implementation specific stuff, instead of the keys and its contents which I want to look at. Is there a work-around i'm missing ? ...