std::map with user defined data

  • Thread starter mergaite_lietuvaite
  • Start date
M

mergaite_lietuvaite

Hi,

I try to use map. My code is very easy, but there are some errors....
(by compiling). Plaese, could you say, what I do wrong..

struct less_array
{
bool operator()(const FArray p1, const FArray p2) const
{
return p1 < p2 ;
}
};

int main(){

....

map<const FArray, vector<FArray>, less_array> my_map;

const vector<FArray>& vert1 = .....getVert1(); // a vector of.
vector<FArray>::const_iterator vert = vert1.begin();

if(my_map.find(vert) == my_map.end()) // line 172 in code
cout<<"map is empty"<<endl;

my_map[vert]= new FArray(); // line 194 in code

.....
}

Compiler gets error-message because of my_map-find and my_map[vert].
Compiler message is:
....
FDualMarchingCubesAlgorithm.cc:176: error: no matching function for
call to `
std::map<const FArray, std::vector<FArray, std::allocator<FArray> >,
FDualMarchingCubesAlgorithm::less_array,
std::allocator<std::pair<const FArray,
std::vector<FArray, std::allocator<FArray> > > > >::find(
__gnu_cxx::__normal_iterator<const FArray*, std::vector<FArray,
std::allocator<FArray> > >&)'
/usr/include/g++/bits/stl_map.h:468: error: candidates are: typename
std::_Rb_tree<_Key, std::pair<const _Key, _Tp>,
std::_Select1st<std::pair<const _Key, _Tp> >, _Compare,
_Alloc>::iterator
std::map<_Key, _Tp, _Compare, _Alloc>::find(const _Key&) [with _Key
= const
FArray, _Tp = std::vector<FArray, std::allocator<FArray> >, _Compare
=
FDualMarchingCubesAlgorithm::eqpos, _Alloc =
std::allocator<std::pair<const
FArray, std::vector<FArray, std::allocator<FArray> > > >]
.....

DualMarchingCubesAlgorithm.cc:194: error: no match for 'operator[]' in
'
my_map[vert]'
......

thank you on advance!

Joana
 
R

Rolf Magnus

Hi,

I try to use map. My code is very easy, but there are some errors....
(by compiling). Plaese, could you say, what I do wrong..

struct less_array
{
bool operator()(const FArray p1, const FArray p2) const
{
return p1 < p2 ;
}
};

Not an error, but you should probably pass the arguments by reference to
ensure they are not unnecessarily copied. But then, you don't need the
whole struct, since the above is the default behavior for maps.
int main(){

...

map<const FArray, vector<FArray>, less_array> my_map;

const vector<FArray>& vert1 = .....getVert1(); // a vector of.
vector<FArray>::const_iterator vert = vert1.begin();

if(my_map.find(vert) == my_map.end()) // line 172 in code

vert is a const_iterator. Your map's key type is not such an iterator type,
so you can't use it as argument type for find(). You probably wanted:

if(my_map.find(*vert) == my_map.end())
cout<<"map is empty"<<endl;

my_map[vert]= new FArray(); // line 194 in code
 
E

Earl Purple

map<const FArray, vector<FArray>, less_array> my_map;

vector<FArray>::const_iterator vert = vert1.begin();

my_map[vert]= new FArray(); // line 194 in code

....
}

To answer just the question asked, it is invalid because vert is an
iterator, not an FArray. Also the RHS will not convert from FArray * to
vector< FArrray >

my_map[ *vert ] = vector< FArray > ( 1 );

will work albeit that I'm a bit questionable about your design.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top