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 (!...
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...
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!
...
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...
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?
...
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...
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.
...
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...
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...
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...
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?
...
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?
...
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 ...
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...
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).
...
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,...
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...
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...
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...
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 ?
...