Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #7501
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: STL map insert Options |
| Date | 2011-07-06 10:28 +1200 |
| Message-ID | <97hhh0Fo68U2@mid.individual.net> (permalink) |
| References | <MPG.287d2c69abea9b739896c0@news.eternal-september.org> |
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
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