Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7501

Re: STL map insert Options

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: STL map insert Options
Date Wed, 06 Jul 2011 10:28:05 +1200
Lines 64
Message-ID <97hhh0Fo68U2@mid.individual.net> (permalink)
References <MPG.287d2c69abea9b739896c0@news.eternal-september.org>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net l1+iPo8Zi1h4NZlDwpXfGAXgzNKuBecgyyj5UUpVh+6ZjULaLj
Cancel-Lock sha1:8TEqDsyNqg9/10UG3/9L0X2Z6H4=
User-Agent Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4
In-Reply-To <MPG.287d2c69abea9b739896c0@news.eternal-september.org>
Xref x330-a1.tempe.blueboxinc.net comp.lang.c++:7501

Show key headers only | View raw


On 07/ 6/11 09:13 AM, Mike Copeland wrote:
>     I am curious about the benefits of several STL map usages and
> interfaces.  Consider the following  declarations:
>
> class Entrant
> {
> public:
>      int          baseBib;
>      char         baseEvent;
>      unsigned int specTime;
> } ;
>
> int main()
> {
>      map<int, Entrant>  myEntrant;
>      map<int, Entrant>::iterator it;
>      Entrant a1;
>
>      a1.baseBib = 2066, a1.baseEvent = 'a', a1.specTime = 0;
>      myEntrant.insert(pair<int, Entrant>(a1.baseBib, a1));
> [etc.]
>
> ....and:
>
> typedef struct ChipRec
> {
>      int          bibNum;
>      char         cEvent;
>      unsigned int finTime;
> } TData;
>      TData tWork;
>
> typedef map<int, ChipRec>  BCI;
> 	BCI bci;
> 	map<int, ChipRec>::iterator bciIter;
> int main()
> {
>      tWork.bibNum = 2066, tWork.cEvent = 'a', tWork.finTime = 0;
>      bci.insert(BCI::value_type(tWork.bibNum, tWork));
> [etc.]
>
>     In both declarations I'm doing the same work, but I can't see any
> advantage to either (class or typedef struct declarations)...other than
> minor coding differences.  Am I missing something that _is_ a reason to
> use one technique versus another?
>     Also, I see some references to "make_pair", but I don't understand
> what that is nor why/how I might use that construct in my applications.
> Could someone explain the 3 constructs "pair", "make_pair" and
> "value_type"?  TIA

The value_type for a std::map is a std::pair<key,value>.

std::make_pair is a function template short cut for creating a 
std::pair.  The function template allows automatic template argument 
deduction.  So

myEntrant.insert(std::pair<int, Entrant>(a1.baseBib, a1));

is equivalent to

myEntrant.insert(std::make_pair(a1.baseBib, a1));

-- 
Ian Collins

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

STL map insert Options mrc2323@cox.net (Mike Copeland) - 2011-07-05 14:13 -0700
  Re: STL map insert Options Ian Collins <ian-news@hotmail.com> - 2011-07-06 10:28 +1200
  Re: STL map insert Options Paavo Helde <myfirstname@osa.pri.ee> - 2011-07-06 01:14 -0500
  Re: STL map insert Options Stephen Howe <stephen.howe@kantarmedia.com> - 2011-07-12 13:29 -0700
    Re: STL map insert Options Ian Collins <ian-news@hotmail.com> - 2011-07-13 09:01 +1200
      Re: STL map insert Options Stephen Howe <stephen.howe@kantarmedia.com> - 2011-07-13 07:31 -0700
        Re: STL map insert Options Leigh Johnston <leigh@i42.co.uk> - 2011-07-13 15:59 +0100
        Re: STL map insert Options Ian Collins <ian-news@hotmail.com> - 2011-07-14 07:57 +1200
          Re: STL map insert Options Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo> - 2011-07-14 00:07 -0400
            Re: STL map insert Options Ian Collins <ian-news@hotmail.com> - 2011-07-14 16:50 +1200
  Re: STL map insert Options Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-07-19 15:20 +0000

csiph-web