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,243dc2fb696a49cd X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!stamper.news.atl.earthlink.net!newsread3.news.atl.earthlink.net.POSTED!14bb18d8!not-for-mail Sender: mheaney@MHEANEYX200 Newsgroups: comp.lang.ada Subject: Re: Ada Popularity: Comparison of Ada/Charles with C++ STL (and Perl) References: <11b4d.3849$d5.30042@newsb.telia.net> From: Matthew Heaney Message-ID: User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Fri, 24 Sep 2004 02:47:04 GMT NNTP-Posting-Host: 64.185.133.124 X-Complaints-To: abuse@earthlink.net X-Trace: newsread3.news.atl.earthlink.net 1095994024 64.185.133.124 (Thu, 23 Sep 2004 19:47:04 PDT) NNTP-Posting-Date: Thu, 23 Sep 2004 19:47:04 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: g2news1.google.com comp.lang.ada:4078 Date: 2004-09-24T02:47:04+00:00 List-Id: kevin.cline@gmail.com (Kevin Cline) writes: > Georg Bauhaus wrote: > > Is the intention to first store a 0 with word, and then > > to increment the value stored at word? > > My understanding is that Insert will insert a new key/value pair into > the map if no entry exists for the given key, and outputs the iterator > I pointing to the key/value pair for the given key. Yes. This behavior is identical to the C++ statements: typedef std::map wcmap_t; wcmap_t wcmap; typedef wcmap_t::iterator iter_t; typedef std::pair status_t; const status_t status = wcmap.insert(std::make_pair(word, 0)); const iter_t i = status.first; ++i->second; > > Although this will require that you know how to separate the queries > > and commands in this abbreviation. If you are not used to how ++ and > > [] work in the context of maps, will this still be easy to > > understand? > > The only question is whether the [] operator will create an entry if > none exists. Thirty seconds with the reference will confirm that it > does, and I would expect a professional programmer to be able to > remember that. Yes, the index operator creates an element with its default value, if the requested key isn't already in the map. Note, however, that the index operator isn't completely benign. In fact, the index operator for map gets its own entry in Effective STL. One thing programmers sometimes do is: some_map[some_key] = x; The issue here is that the element associated with some_key gets constructed using its default ctor, and then the object is immediately assigned the value of x. But it's more efficient to use the copy ctor, but to do that you need to use the insert member function as above. The other issue with the index operator is that if you use a (string) literal as the key, then it's possible to misspell the value, and create a new key accidently. That being said, for this particular problem, the index operator is very convenient, and entirely appropriate. > > AFAIK, [] does more than one thing behind the scenes. > > ++ is known to be difficult to understand in that it > > is not clear what ++ is referring to. > > Now are you claiming that C++ is hard to read because one has to learn > the operator precedence rules? The rule in C and C++ is that postfix operators have a higher precedence than prefix operators. This is the only rule that makes sense, for example: struct S { int i; }; S s = { 0 }; ++s.i; In this case, the operator is the "dot" operator (which isn't user-definable). Actually, I would have used a pointer instead: S* ps = &s; ++ps->i; The arrow operator is postfix, so it has higher precedence than ++ (which is prefix), so the expression above increments i, not ps. In the statement: ++wcmap[word]; the index operator is postfix, so that happens first. It either returns the element whose key is "word", or constructs a new element using the default ctor for the type, which in this case (type int) initializes the element to the value 0. That last bit might be confusing, since you don't normally think of built-in types as having a constructor. However, type int does have a ctor, so for example: int i; //garbage int j = int::int(); //0 The existence of a default ctor for type int is why the index operator works safely. -Matt