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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no 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!postnews1.google.com!not-for-mail From: kevin.cline@gmail.com (Kevin Cline) Newsgroups: comp.lang.ada Subject: Re: Ada Popularity: Comparison of Ada/Charles with C++ STL (and Perl) Date: 26 Sep 2004 00:08:20 -0700 Organization: http://groups.google.com Message-ID: References: <11b4d.3849$d5.30042@newsb.telia.net> NNTP-Posting-Host: 24.219.97.214 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1096182501 1044 127.0.0.1 (26 Sep 2004 07:08:21 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 26 Sep 2004 07:08:21 +0000 (UTC) Xref: g2news1.google.com comp.lang.ada:4194 Date: 2004-09-26T00:08:20-07:00 List-Id: Matthew Heaney wrote in message news:... > 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; Most C++ programmers would not. Modern practice is to avoid naked pointers when possible, which for me is almost all the time. Pointers are needed only for low -level programming, like building a new type of container. The rest of the time you can use auto_ptr or some other smart pointer type. Anyway, the arrow and dot operators have the same precedence, so I don't see why you would prefer ++ps->i to ++s.i. I find ++ps->i to be more confusing, since a pointer can be incremented, so it would be possible to read (++ps)->i. A reference can not be incremented, so there is only one reasonable interpretation of ++s.i.