Can multimaps be implemented with structures as key? If yes then how? - c++

For example two structures like point and square as given below if it is possible to do so how may i insert new elements into it?
typedef struct _point{
int x;
int y;
}Point;
typedef struct _square{
float belowLeftX;
float belowLeftY;
}Square;
multimap <Square, Point > dictionary;

Yes, a structure as key is not different compared to a class as key. You have two options to get your code working.
Option 1:
Supply ordering to the Square type.
typedef struct _square {
float belowLeftX;
float belowLeftY;
bool operator<(struct _square const& rhs) const
{
if (belowLeftX < rhs.belowLeftX) return true;
if (rhs.belowLeftX < belowLeftX) return false;
return belowLeftY < rhs.belowLeftY;
}
Option 2:
Supply ordering of the Square type to the dictionary.
auto comparator = [](Square const& lhs, Square const& rhs)
{
if (lhs.belowLeftX < rhs.belowLeftX) return true;
if (rhs.belowLeftX < lhs.belowLeftX) return false;
return lhs.belowLeftY < rhs.belowLeftY;
};
std::multimap <Square, Point, decltype(comparator)> dictionary(comparator);

Related

Sorting a list by a struct member in c++

I have a list filled with this struct:
struct singlePaymentStruct
{
std::string payer;
int payment;
double amount;
std::time_t timeRec;
singlePaymentStruct() {
payer="Empty";
payment=0;
amount=0;
timeRec = time(0);
}
};
I want to be able to sort this list by any of the fields. How exactly do I do this?
I didn't quite understand how sort method works with something more complex than just a list of records...
Solution found:
singlePaymentList.sort( []( const singlePaymentStruct &a, const singlePaymentStruct &b)
{return a.payer > b.payer;}
);
1.overloading operator<
you can do this by overloading the < operator
struct Foo{
int bar;
bool operator<(Foo &x){
return bar < x.bar;
}
};
2.using lambda expressions
(what is lambda expression?)
Foo array[10];
std::sort(array,array + 10,[](Foo const &l, Foo const &r) {
return l.bar < r.bar; });
3.using custom compare functions
If the possible fields to be used for sorting are known prior, it may be easier to read to implement custom compare functions specifically for the sorting.
struct Foo {
int bar;
SpecialType daa; // Assume daa.IsLessThan() available.
static bool lessBar(const Foo& l, const Foo& r) {
return l.bar < r.bar;
}
static bool lessDaa(const Foo& l, const Foo& r) {
return l.daa.IsLessThan(r.daa);
}
};
Foo array1[10]; // To be sorted by Foo::bar
Foo array2[10]; // To be sorted by Foo::daa
std::sort(array1, array1+10, Foo::lessBar);
std::sort(array2, array2+10, Foo::lessDaa);
std::sort accepts a third optional parameter that is a comparator function. This function should behave as < between elements (i.e. return true when the first is "less than" the second.
For example to sort an std::vector of your structures on increasing payment value what you can do is:
std::sort(data.begin(), data.end(),
[](const singlePaymentStruct& a, const singlePaymentStruct& b) {
return a.payment < b.payment;
});
let the array be struct singlePaymentStruct a[N]
sort(a,a+N,cmp);
bool cmp(struct singlePaymentStruct x, struct singlePaymentStruct y)
{
return x.field < y.field ; //or anything you want to do and return boolean
}
How it works under the hood?
Simply put basically it uses some sorting algoritm like quicksort or mergesort.
Why do we specify comparator functor ?
Well we need that comparator functor to decide the ordering of elements.
The basic thing is in any sorting algortihm the basic operation is comparison..and if we can specify that we are basically controlling the sorting operation.
Hope now you get the pieces together. That's why cmp() takes two values which it will compare and based on which order them.

Using multiple keys with map/unordered_map (multidimensional)

Consier I have some buckets (vector<float>). I need to access this buckets based on some indexes. Example:
int indexX, indexY, indexZ, indexW;
So when a new point arrives I need to put the point in the correct buckets. Now I am doing something like this:
// X Y Z W => Bucket
unordered_map<int, unordered_map<int, unordered_map<int, unordered_map<int, vector<float>>>>> indexedTable;
// New point arrives and I put it in the right bucket:
indexedTable[indexX][indexY][indexZ][indexW].push_back(myValue);
But I find this very ugly, and sadly it's also very slow. For example accessing it for 1700 points, it takes 0.56 secs, that's too slow.
Are there any better/faster alternatives, without using Boost ?
Note that this data structure in my needs is comparable to a Sparse Matrix (multidimensional), because very few "buckets" will be filled with something.
Instead of using this 4-deep monstrosity, you could just use an unordered_map with a struct containing the 4 indices as key, and vector<float> as the value type. Provide an equality comparator and hash function for the struct and you're in business.
struct indices
{
int indexX, indexY, indexZ, indexW;
bool operator==(indices const& other) const
{
return std::tie(indexX, indexY, indexZ, indexW) ==
std::tie(other.indexX, other.indexY, other.indexZ, other.indexW);
}
};
struct indices_hash
{
std::size_t operator()(indices const& i) const
{
std::size_t seed = 0;
boost::hash_combine(seed, i.indexX);
boost::hash_combine(seed, i.indexY);
boost::hash_combine(seed, i.indexZ);
boost::hash_combine(seed, i.indexW);
return seed;
}
};
std::unordered_map<indices, std::vector<float>, indices_hash> m;
Since you don't want to use Boost, either come up with your own hash_combine alternative or copy the implementation from here.
Live example
As long as you don't need to access all of the points within a dimension (ie, all buckets where y==2), then you should consider using a single structure to represent your point, rather than nesting maps.
Something along these lines:
struct Point_t
{
int x;
int y;
int z;
int w;
Point_t (int _x, int _y, int _z, int _w) : x (_x), y (_y), z (_z), w (_w) {}
};
// Make sure the points are sortable
struct CmpPoint_t
{
bool operator() (const Point_t& lhs, const Point_t& rhs)
{
return (lhs.x < rhs.x &&
lhs.y < rhs.y &&
lhs.z < rhs.z &&
lhs.w < rhs.w);
}
};
typedef std::vector<float> Bucket_t;
typedef std::map<Point_t, Bucket_t, CmpPoint_t> BucketMap_t;
Applying your example use case:
BucketMap_t indexedTable;
indexedTable[Point_t(indexX, indexY, indexZ, indexW)].push_back (myValue);

Invalid operator< using std::equal_to

Without going into much details why I´m doing what I´m doing let me describe the issue.
Im using a std::set for storing unique objects of a struct called VertexTypePos3Normal.
The struct is defined as following:
struct VertexTypePos3Normal {
// ctor, dtor ..
friend bool operator==(const VertexTypePos3Normal& v1, const VertexTypePos3Normal& v2);
friend bool operator<(const VertexTypePos3Normal& v1, const VertexTypePos3Normal& v2);
glm::vec3 pos;
glm::vec3 normal;
};
bool operator<(const VertexTypePos3Normal& v1, const VertexTypePos3Normal& v2) {
return (v1.pos.x < v2.pos.x) && (v1.pos.y < v2.pos.y) && (v1.pos.z < v2.pos.z) && (v1.normal.x < v2.normal.x) && (v1.normal.y < v2.normal.y) && (v1.normal.z < v2.normal.z);
}
// operator == ommited
Per default std::set uses std::less as comparison function.
So I first declared my set as std::set<VertexTypePos3Normal> set;
The elements inserted into the set are stored in a std::vector that is not containing unique values (looping over the vector).
Using std::less called my operator< but the result was not correct as the set contained mostly only 1 value although the vector contained about 15 different ones.
Here is the method inserting into the set:
void createUniqueVertices(const std::vector<const VertexTypePos3Normal>& verticesIn,
std::vector<const VertexTypePos3Normal>& verticesOut,
std::vector<unsigned short>& indicesOut)
{
//std::map<VertexTypePos3Normal, int, std::equal_to<VertexTypePos3Normal> > map;
std::set<const VertexTypePos3Normal, std::equal_to<const VertexTypePos3Normal> > set;
int indexCounter = 0;
for (auto c_it = verticesIn.cbegin(); c_it != verticesIn.cend(); ++c_it) {
//bool newlyAdded = map.insert(std::pair<VertexTypePos3Normal, int>(*c_it, indexCounter)).second;
bool newlyAdded = set.insert(*c_it).second;
//if (newlyAdded) {
//verticesOut.push_back(*c_it);
//map.insert(std::pair<VertexTypePos3Normal, int>(*c_it, indexCounter));
//++indexCounter;
//}
//indicesOut.push_back(map[*c_it]);
}
}
So I was about to try out std::equal_to instead of std::less and wrote operator==.
Now the weird stuff started:
Although I´m not calling std::less anymore and therefore also not operator<, there is an assertion error in STL (using VC compiler) _DEBUG_ERROR2("invalid operator<", _File, _Line);
So actually i got two questions:
1.) Why is my operator < not working with std::less as it is supposed to.
2.) How can operator< trigger an assertion when it is not even called.
EDIT: Thanks for all information. Looks like I totally missunderstood strict weak ordering. Using std::tie taking care of it solved my problem. Here is the updated code:
void createUniqueVertices(const std::vector<const VertexTypePos3Normal>& verticesIn,
std::vector<const VertexTypePos3Normal>& verticesOut,
std::vector<unsigned short>& indicesOut)
{
std::map<VertexTypePos3Normal, int> map;
int indexCounter = 0;
for (auto c_it = verticesIn.cbegin(); c_it != verticesIn.cend(); ++c_it) {
bool newlyAdded = map.insert(std::pair<VertexTypePos3Normal, int>(*c_it, indexCounter)).second;
if (newlyAdded) {
verticesOut.push_back(*c_it);
//map.insert(std::pair<VertexTypePos3Normal, int>(*c_it, indexCounter));
++indexCounter;
}
indicesOut.push_back(map[*c_it]);
}
}
Im using a map in the final version as the set is obsolete.
Here is my new operator<
bool operator<(const VertexTypePos3Normal& v1, const VertexTypePos3Normal& v2) {
return (std::tie(v1.pos.x, v1.pos.y, v1.pos.z, v1.normal.x, v1.normal.y, v1.normal.z) < std::tie(v2.pos.x, v2.pos.y, v2.pos.z, v2.normal.x, v2.normal.y, v2.normal.z));
}
Ordered associative containers require a strict weak ordering relation. Among the required properties is antisymmetry, that is, cmp(x,y) implies !cmp(y,x). Your definition of operator< does not satisfy this property.
Also, equality (or equivalence) may be defined as !(cmp(x,y)||cmp(y,x)), and often this is used instead of x==y. That is, operator< may be called even if you don't use it explicitly.
Your operator < is plain wrong.
You might want:
bool operator<(const VertexTypePos3Normal& v1, const VertexTypePos3Normal& v2) {
if(v1.pos.x < v2.pos.x) return true;
else if(v1.pos.x == v2.pos.x) {
if(v1.pos.y < v2.pos.y) return true;
else if(v1.pos.y == v2.pos.y) {
if(v1.pos.z < v2.pos.z) return true;
else if(v1.pos.z < v2.pos.z) {
if(v1.normal.x < v2.normal.x) return true;
else if(v1.normal.x == v2.normal.x) {
if(v1.normal.y < v2.normal.y) return true;
else if(v1.normal.y < v2.normal.y) {
if(v1.normal.z < v2.normal.z) return true;
}
}
}
}
}
return false;
}
Note: That should be split into two less function calls for glm::vec3 (having bool less(const glm::vec3&, const glm::vec3&);)

Can I use vector as index in map structure in c++?

I attempted to do something like this but it does not compile:
class point
{
public:
int x;
int y;
};
int main()
{
vector<point> vp1;
vector<point> vp2;
vector<point> vp3;
map < vector<point>, int > m;
m[vp1] = 1;
m[vp2] = 2;
m[vp3] = 3;
map < vector<point>, int >::iterator it;
for (it=m.begin(); it!=m.end(); it++)
{
cout<<m[it->first]<<endl;
}
return 0;
}
You can use anything as the index type into a std::map as long as it supports an operator< (which could could define as a free-standing function -- doesn't have to be a member function, as long as you can write a < b for a and b being instances of your type of interest) with the usual semantics (antireflexive, transitive, ...). Or, you can pass a binary function with the same semantics to use in lieu of <, if that suits you better.
You can, but the type used as a key in a map needs to be comparable, either using operator<, or using a comparison function/functor you supply as the third template parameter for the map type.
Yes, you can. Vectors, like all containers, are comparable. The resulting map will sort the vectors in lexicographic order.
The problem is that point is not comparable. You have to define a sort order for points, and then this will in turn define lexicographic order over vector<point>.
class point
{
public:
int x;
int y;
};
bool operator<( point const &l, point const &r ) {
return l.x < r.x? true
: r.x < l.x? false
: l.y < r.y;
}
A simpler solution is to use std::pair instead of defining your own point.
typedef pair< int, int > point; // point::first = x, point::second = y
// pair is already comparable; order defined as in previous example
typedef vector<point> pointvec; // OK
You haven't defined a function to compare a vector<point> Maps make requiremets of keys checking of equivalence and comparison.
You have to declare the operator<. It would look like this (please keep in mind, that the three vectors in your sample code actually look the same):
bool operator<(const vector<point>& left, const vector<point>& right)
{
return left.size() < right.size();
}

Insert a pair of object into a map

I'm trying to insert some pair value into a map. May map is composed by an object and a vector of another object. i don't know why but the only way to make the code to compile is to declare the first object like a pointer. But in this way when I insert some object, only the first pair is put into the map.
My map is this:
map<prmEdge,vector<prmNode> > archi;
this is the code:
{
bool prmPlanner::insert_edge(int from,int to,int h) {
prmEdge e;
int f=from;
int t=to;
if(to<from){
f=to;
t=from;
}
e.setFrom(f);
e.setTo(t);
vector<prmNode> app;
prmNode par=nodes[e.getFrom()];
prmNode arr=nodes[e.getTo()];
app.push_back(par);
app.push_back(arr);
archi.insert(pair<prmEdge,vector<prmNode> >(e,app) );
return true;
}
}
In this way, I have an error in compilation in the class pair.h.
What could I do?? Thank you very much.
You need to supply a comparator for prmEdge. My guess is that it uses the default comparator for map, e.g. comparing the address of the key -- which is always the same because e is local.
Objects that serve as Keys in the map need to be ordered, so you either need to supply a operator for comparing edges, or a comparator function for map.
class EdgeComparator {
public:
bool operator( )( const prmEdge& emp1, const prmEdge& emp2) const {
// ... ?
}
};
map<prmEdge,vector<prmNode>, EdgeComparator > archi;
The really hard part is deciding how to compare the edges so a definitive order is defined. Assuming that you only have from and to You can try with:
class EdgeComparator {
public:
bool operator( )( const prmEdge& emp1, const prmEdge& emp2) const {
if ( emp1.from != emp2.from )
return ( emp1.from < emp2.from );
return ( emp1.to < emp2.to );
}
};
It will sort on primary key from and secondary to.
The class prmEdge needs to define a comparison function (default is operator<) to work with std::map. Although you don't post that code, I would expect that to be your problem (for the record, pointer have an operator< defined.
struct A {
int a;
bool operator<(A other)
{
return a < other.a;
}
};
struct B {
int b;
};
bool cmp(B lhs, B rhs)
{
return lhs.b < rhs.b;
}
std::map<A, int> map_a;
std::map<B, int, std::pointer_to_binary_function<B, B, bool> > map_b(std::ptr_fun(cmp));
Map elements are ordered by their keys. But the map needs to know how:
Either overload the < operator in the prmEdge class...
class prmEdge
{
//...
public:
bool operator<(const prmEdge& right) const
{
//...
}
};
...or specify a comparator for the map:
class Comparator
{
public:
bool operator()(const prmEdge& left, const prmEdge& right) const
{
// ...
}
};
map<prmEdge, vector<prmNode>, Comparator> archi;