stl

How to insert an object to a map structure by a certain field?

Hello, i have an object - Employee, and i want to know how to insert this object to a map structure sorted by char* lastName field. Thanx. My map need to contain pointers to Employee objects not the objects themselves. the key is the last name of the employee, the map need to be sorted by the employees last name, should i use multimap? ...

Fast hash for std::set<int> of two values?

I am using a std::set<int> for the key of a std map (std::unordered_map<std::Set<int>,float>). I need a hash for this set. The set will always be only two integers, whose values could be up to 2 million. Any ideas on a good and fast hash for such a key as performance is critical? ...

C++/STL Graphviz and Boost - Seg Fault Problem

I am using the dot language to create a graph using Boost. My Graphs are huge and I am trying to delete graphs that are no longer used. Under boost/graph/graphviz.hpp I added a function to remove a vertex: virtual void do_remove_vertex( const node_t& node) { bgl_vertex_t v = bgl_nodes[node]; clear_vertex(v,graph_); ...

Possible to elegantly convert std:vector to cliext::vector or cli::array<T>?

How's that for a catchy title? I need to convert back and forth from a CLR compliant type, like an array, and a std::vector type. Are there any adapter methods out there, or should I just keep copying it in and out each time I call one of my native methods? There are some interesting methods for converting between the cliext STL var...

c+ stl sorted vector inplace union

I'd like an efficient method for doing the inplace union of a sorted vector with another sorted vector. By inplace, I mean that the algorithm shouldn't create a whole new vector or other storage to store the union, even temporarily. Instead, the first vector should simple grow by exactly the number of new elements. Something like: vo...

std::random_shuffle, compliler error: no matching function for call [closed]

Hi! I cannot compile following code by g++ 4.3.2: #include <stdlib.h> #include <algorithm> struct Generator { ptrdiff_t operator() (ptrdiff_t max) { return rand() % max; } }; // ... Generator generator; std::vector<size_t> indices; // fill vector std::random_shuffle(indices.begin(), indices.end(), generator); // error here! ...

storing mem_fun in a standard container

Is there a way to create a vector< mem_fun_t< ReturnType, MyClass > > ? The error i'm seeing is: error C2512: 'std::mem_fun1_t<_Result,_Ty,_Arg>' : no appropriate default constructor available ...

Ensure multiset is reordered when objects change

I have a multiset with a custom predicate function, e.g multiset<MyClass *,MyCompFunc> where MyCompFunc looks at an attribute on the MyClass objects. During the progress of the application, the objects might change in a way that should cause them to be reordered. What's the correct way to get the multiset to become reordered when this h...

Can a std::map value object contain a reference to the corresponding key?

Essentially, what I'd like is for the value object to maintain a reference to the corresponding key object, because there's some useful information in there, which would be nice to access via the value object. What I'm attempting to do may just not make sense, but consider the following: class key { // ... Various members ... f...

Merge STL map's

How can I merge two STL maps into one? They both have the same key value types (map). If there is overlap of the keys I would like to give preference to one of the maps. ...

std::sort and std::unique problem with a struct

The following code: #include <vector> #include <algorithm> struct myStructDim { int nId; int dwHeight; int dwWidth; }; void main() { ::std::vector<myStructDim> m_vec_dim; ::std::sort(m_vec_dim.begin(), m_vec_dim.end()); m_vec_dim.erase( ::std::unique(m_vec_dim.begin(), m_vec_dim.end())...

Cleaning up bidirectional iterator code

I've modified James' flattening iterator to act as a bidirectional iterator if possible, but I don't think my changes are very elegant (particularly relying on a bool to see if the inner iterator has been set). However, I can't seem to come up with a nicer solution. Does anyone have any ideas? #include <algorithm> #include <iostream> ...

Downloadable STL documentation that covers TR1?

For my offline coding needs, I've been using the downloadable STL documentation from SGI. Unfortunately it was last edited in 1999, and doesn't cover any of the TR1. It's easy enough to find online references for TR1, but can anybody recommend an easily downloadable TR1 reference for working offline? In HTML? I've downloaded Boost's ...

Problems with C++ containers

I have a std::list in a C++ program, which contains objects of a Class A. Lets say I have 10 objects in it. I have a reference to the 6th object stored, in another data structure say ref_6. Lets say I need to remove the 8th element from my list. To do this, I would use pop_front 8 times and store 8 objects in a vector and use push_front...

Big->little (little->big) endian conversion of std::vector of structs

How can I perform endian conversion on vector of structs? For example: struct TestStruct { int nSomeNumber; char sSomeString[512]; }; std::vector<TestStruct> vTestVector; I know how to swap int values, but how to swap a whole vector of custom structs? ...

Prettier syntax for "pointer to last element", std::vector?

I'm wondering if there is prettier syntax for this to get a normal pointer (not an iterator) to the last element in a C++ vector std::vector<int> vec; int* ptrToLastOne = &(*(vec.end() - 1)) ; // the other way I could see was int* ptrToLastOne2 = &vec[ vec.size()-1 ] ; But these are both not very nice looking! ...

C++ STL containers, 'under the bonnet'?

Hi, what would be the best way to learn what actually happens with the STL containers? I presume just programming wouldnt be enough. Are there any books specifically aimed at this? ...

To write a fuction that prints each element of a STL vector despite the type

I tried with this piece of code but it didn't work template <class T> void display(vector<T> vec) { vector<T>::iterator MyIter; for(MyIter=vec.begin();MyIter!=vec.end();MyIter++) cout<<*MyIter<<" "; cout<<endl; } I mean if there's a way of avoid making a function for each type T (int, char, string). I'm using only builtin ...

[edit] recursive template?

Rephrased Question I found that my original question wasn't clear enough and the repliers misunderstood my problem. So let me try to clarify: Let's say I have two classes: struct C { void(*m_func)(C*); }; struct D { std::function<void(D*)> m_func; }; Now I want to make a generic version of the two, so I do something like this: temp...

Customizing STL iterator class

which concept in c++ teaches you to extend and write your own iterator class? I know a little about writing templates. ...