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 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> References: 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: Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++:7501 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 myEntrant; > map::iterator it; > Entrant a1; > > a1.baseBib = 2066, a1.baseEvent = 'a', a1.specTime = 0; > myEntrant.insert(pair(a1.baseBib, a1)); > [etc.] > > ....and: > > typedef struct ChipRec > { > int bibNum; > char cEvent; > unsigned int finTime; > } TData; > TData tWork; > > typedef map BCI; > BCI bci; > map::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. 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(a1.baseBib, a1)); is equivalent to myEntrant.insert(std::make_pair(a1.baseBib, a1)); -- Ian Collins