hi friends;
i am trying to build a class using multimap, the idea is something like this:
I have say N no of particles and each particle has fix position in 3d space
something like this:
Coordinate: Particle level
(1,2,3) ----------------> 1
(1,2,3) ----------------> 7
(1,0,0) ----------------> 2
(0,0,0) ----------------> 0
..... and so on
I tried this with 4d array like Pos[x][y][z], but i dont like the approach and thought maybe using multimap from stl will be more effivient in this kind of mapping. Please put your inputs in this regard.
i'll now show the class i have devolopped so far
// [B]Container class that define (i,j,k) triplet[/B]
#include<iostream>
#include<map>
#include<set>
#include<iomanip>
typedef class container store;
typedef std::pair<const store, int> Pair;
typedef std::multimap< store, int > Box;
// 1st class container definition
class container
{
private:
int _i;
int _j;
int _k;
public:
container():_i(0),_j(0),_k(0){} //constructor
container(const int&i, const int&j,
const int&k):_i(i),_j(j),_k(k){} //constructor
container(const container& t) //copy constructor
{ _i=t._i;
_j=t._j;
_k=t._k;}
bool operator>(const container& t)const // need to understand
{ return _k>t._k||_j>t._j||_i>t._i;}
bool operator<(const container& t)const
{return _k<t._k||_j<t._j||_i<t._i;} // need to understand
bool operator()(const container& __x, const container& __y) const
{ return __x < __y; }
container & operator=(const container & t) //Assignment Operator
{
if (this == &t) // object assigned to itself
return *this; // all done
_i=t._i;
_j=t._j;
_k=t._k;
return *this;
}
friend std::ostream & operator<<(std::ostream & os, const container & t)
{
os << "(" << t._i << "," << t._j<<","<<t._k<<")";
return os;
}
~container(){}
};
Next i write the mapping class using particle and data triplet defined in container class
class _Map
{
private:
Box xx;
public:
_Map(){} //default constructor
void int_insert(const store &HOLD, const int &the_number)
{xx.insert(Pair(HOLD,the_number));} //HOLD is the key index where the number is inserted
int WholeEraseByKey(const store &HOLD) //it also returns the number of erased elements
{
Box::size_type n;
n = xx.erase(HOLD);
return n;
}
void ShowMap()
{
Box::iterator pos;
std::cout.setf(std::ios::left, std::ios::adjustfield);
for (pos=xx.begin(); pos!=xx.end(); ++pos) {
std::cout << pos->first << " ->"
<< pos->second << std::endl;
}
}
~_Map(){}
};
This code so far works fine. But i need to put some more functionality where i am getting confused.
1. remove a particular particle.
say i have: (0,0,0) ----> 7
(0,0,0) -----> 8
(0,0,0) ------> 10
(1,1,1) --------> 1
and i need to erase the value 10 only. I am only able to erase the whole map with key value (0,0,0).
2. Secondly is it possible by looking at value, to retrive the information about is coordinate without doing a full search.
Please comment.
thanks