From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,703c4f68db81387d X-Google-Thread: 109fba,703c4f68db81387d X-Google-Thread: 115aec,703c4f68db81387d X-Google-Thread: f43e6,703c4f68db81387d X-Google-Attributes: gid103376,gid109fba,gid115aec,gidf43e6,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news4.google.com!newshub.sdsu.edu!fr.ip.ndsoftware.net!proxad.net!134.158.69.22.MISMATCH!in2p3.fr!oleane.net!oleane!not-for-mail From: =?ISO-8859-1?Q?Falk_Tannh=E4user?= Newsgroups: comp.lang.ada,comp.lang.c++,comp.realtime,comp.software-eng Subject: Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada) Date: Mon, 14 Mar 2005 17:06:10 +0100 Organization: Canon Research Centre France Message-ID: References: <4229bad9$0$1019$afc38c87@news.optusnet.com.au> <1110032222.447846.167060@g14g2000cwa.googlegroups.com> <871xau9nlh.fsf@insalien.org> <3SjWd.103128$Vf.3969241@news000.worldonline.dk> <87r7iu85lf.fsf@insalien.org> <1110052142.832650@athnrd02> <42309456$1@news.broadpark.no> <4232ab3a$0$26547$9b4e6d93@newsread4.arcor-online.net> NNTP-Posting-Host: centre.crf.canon.fr Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Trace: s1.news.oleane.net 1110816328 12612 194.2.158.33 (14 Mar 2005 16:05:28 GMT) X-Complaints-To: abuse@oleane.net NNTP-Posting-Date: Mon, 14 Mar 2005 16:05:28 +0000 (UTC) User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041217 X-Accept-Language: de, fr, pl, en, en-us In-Reply-To: Xref: g2news1.google.com comp.lang.ada:9368 comp.lang.c++:45568 comp.realtime:1451 comp.software-eng:5017 Date: 2005-03-14T17:06:10+01:00 List-Id: Dr. Adrian Wrigley wrote: > I have probably missed a trick in the C++, but I couldn't get > std::map code to compile (except in the trivial cases): > > #include > > struct compoundindex { > int a, b, c; > }; > > int main() > { > std::map hash; > compoundindex fred = { 1, 2, 4 }; > > hash[fred] = 0.123; > } C++ associative containers (map, set, multimap, multiset) require a "strict weak ordering" for their key types. In the present case, it is enough to define the following 'operator<' to induce a total ordering: bool operator<(compoundindex const& x, compoundindex const& y) { if(x.a < y.a) return true; if(x.a == y.a) { if(x.b < y.b) return true; if(x.b == y.b) return x.c < y.c; } return false; } Alternatively, if there is no natural definition for a general-purpose operator< for the key type in question, it is possible to create a comparison functor and to instantiate the container with it as supplementary template parameter. In either case, the container implementation uses the order to store its elements in a balanced tree, so that element access becomes possible with logarithmic complexity regarding to the number of elements currently in the container. Hash-based associative containers (guaranteing constant-time access as long as there are no key collisions) are not (yet) provided by the standard library but are widely available through third party / extension libraries. Falk