Hi ,
I am trying to create a Map that will have vector as its key and an integer as value.
Each vector will be a member of a particular structure. I am new to using maps.
When i tried the code below: I am getting duplicate entries in the map.
If any one can help on this that would be great, Thanks in Advance
Code :
#include <iostream>
#include <string.h>
#include <vector>
#include <map>
#include <utility>
using namespace std;
typedef struct A
{
int x ;
int y ;
int value;
}Literal;
struct vectComp
{
bool operator() (const vector<Literal> v1,const vector<Literal> v2)
{
int i , j;
bool lit_equal = false;
cout<<"V1.size and v2.size "<<v1.size() <<", "<<v2.size()<<endl;
if(v1.size()!= v2.size())
{
cout<<"TRUE"<<endl;
return true;
}
else
{
for(i=0;i<v1.size();i++)
{
lit_equal = false;
for(j=0;j<v2.size();j++)
{
if((v1[i].x== v2[j].x) && (v1[i].y== v2[j].y) &&(v1[i].value== v2[j].value))
{
/* cout<< " v1 X and v2 X "<<v1[i].x << ", "<<v2[j].x<<endl;
cout<< " v1 y and v2 y "<<v1[i].y << ", "<<v2[j].y<<endl;
cout<< " v1 value and v2 value"<<v1[i].value << ", "<<v2[j].value<<endl;
cout<<"v[i] and v[j] MATCHES TRUE"<<endl;*/
lit_equal = true;
break;
}
}
if(lit_equal == false)
{
break;
}
}
if(lit_equal == false)
{
cout<<"TRUE"<<endl;
return true;
}
else
{
cout<<"FALSE"<<endl;
return false;
}
}
}
};
map< vector< Literal >,int,vectComp >mymap;
map< vector< Literal >,int >::iterator it;
map< vector< Literal >,int >::key_compare mycomp;
pair< map< vector< Literal >,int >::iterator,bool >ret;
int main()
{
int i , j;
for(i=0;i<4;i++)
{
vector< Literal > v;
for(j=0;j<(1+i);j++)
{
Literal sL;
sL.x= j+i;
sL.y = (j+1+i);
sL.value = (j+2+i);
v.push_back(sL);
}
//mymap.insert(pair< vector< Literal >,int >(v,i));
mymap.insert(std::make_pair(v,i+1));
cout<<"-------------------------------------------"<<endl;
}
for(i=0;i<3;i++)
{
vector< Literal > v1;
for(j=0;j<(1+i);j++)
{
Literal sL1;
sL1.x= j+i;
sL1.y = (j+1+i);
sL1.value = (j+2+i);
v1.push_back(sL1);
}
//mymap.insert(pair< vector< Literal >,int >(v,i));
mymap.insert(std::make_pair(v1,i+1));
cout<<"-------------------------------------------"<<endl;
}
cout<<"mymap.size() is " <<(int)mymap.size()<<endl;
for(it=mymap.begin(); it!= mymap.end();it++)
{
cout<<"Key : "<<endl;
for(i=0;i<(*it).first.size();i++)
{
cout<< (*it).first[i].x<<(*it).first[i].y<<(*it).first[i].value<<"\t";
}
cout <<" Value : "<<(*it).second<<endl;
cout<< "Vector Size : "<<(*it).first.size()<<endl;
}
return 0;
}