This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: problems using blitz::TinyVector as key for map



Hi,


Thanks for the quick reply.

On Fri, 7 Oct 2005, Peter Doerfler wrote:

What does blitz::all() do? Looks like operator<() is defined for TinyVector.
In that case you shouldn't need to make your own less since std::less just
calls operator<().

The < operator operates elementwise, and returns a 'vector expression' of bools. The all operator takes the 'vector expression' of bools, and returns true if all the bools in the expression are true.


It turns out the problem is that the comparison operator as defined is not a 'strict weak ordering'. I looked at this after I sent out the message last night, and realized it could be the problem, but didn't see anything wrong with it at the time.

int main()
{
   map<blitz::TinyVector<int, 3>, int, blitz::TinyVectorless<int, 3> >
dict;
   dict[blitz::TinyVector<int, 3>(50, 50, 49)] = 0;
   dict[blitz::TinyVector<int, 3>(60, 60, 60)] = 1;
   blitz::TinyVector<int, 3> x(2, 2, 1000);
   blitz::TinyVector<int, 3> y(2, 2, 2);
   cout << (dict.find(x))->first << endl;
   cout << (dict.find(y))->first << endl;
   return 0;
}

You are dereferencing the end of the map because neither x nor y are contained in dict. AFAIK dereferencing end() is undefined and could do anything. Dig into stl_tree.h to figure out why you might be getting the results you see -- in case you care ;)

Right. My mistake.


Anyway, you need to check whether your key was actually found, somewhere along
the lines of

typedef map<blitz::TinyVector<int, 3>, int, blitz::TinyVectorless<int, 3> >
dict_type;
typedef dict_type::iterator dict_iterator;
dict_iterator it = dict.find(x);
cout << (it!=dict.end() ? (*it).first : "not found") << endl;

Not tested since I'm not using blitz.

HTH,
Peter


PS: You might want to pick up a good book about using the STL like "Effective STL" if you'll be using it more.

I actually have the C++ Standard Library book by Josuttis. The problem is just that I am stupid. Do you like the "Effective STL" book?


Faheem.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]