Effective STL 阅读笔记: Item 3: Make copying cheap and correct for objects in containers
gentoo emerge unable to sync

Effective STL 阅读笔记: Item 4 ~ 5: Call empty instead of checking size() against zero.

tubo posted @ 2014年9月03日 00:10 in 未分类 , 617 阅读


1 Item 4: Call empty instead of checking size() against zero

简而言之,用 container.size() 来检查 container 是否为空,而不要用 /container.size() == 0/。

原因: empty is a constant-time operation for all standard containers, but for some list implementations, size takes linear time.

2 Item 5: Prefer range member functions to their single-element counterparts

如果 Container 提供了区间成员函数 (Range member functions),尽量用它来替代自己写的循环和 algrithm 中的 copy 。

A range member function is a member function that, like STL algorithms, uses two iterator parameters to specify a range of elements over which something should be done.

Range member function 的好处在于:

  • 代码量更少,更清晰、直观。
  • 有可能会减少申请内存的次数, 减少数据的移动次数。

Range member function 适用的几种情况:

  1. 容器的构造: 所有标准容器都提供该方法:
    container::container(InputIterator begin, // Beginning of range
                         InputIterator end);  // End of range
  2. 区间插入: 所有标准容器都提供该方法:
    void container::insert(iterator      position, // where to insert the range
                           InputIterator begin,    // start of range to insert
                           InputIterator end);     // end of range to insert

    由于关联容器 (Associative Containers) 有自己的方法来计算插入的位置,insert 方法的第一个 参数经常可以省略:

    void container::insert(lnputIterator begin, InputIterator end);
  3. 区间删除 (Range Eraure):


    • Sequence Container:
      iterator container::erase(iterator begin, iterator end);
    • Associative Container:
      void container::erase(iterator begin, iterator end);
  4. 区间赋值 (Range Assignment): 所有 Container 均提供:
    void container::assign(lnputIterator begin, InputIterator end);
meidir 说:
2023年12月31日 06:52

Thank you for sharing superb informations. Your site is very cool. I’m impressed by the details that you have on this website. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for extra articles. You, my friend, ROCK! I found just the info I already searched everywhere and just couldn’t come across. What a great web site. DESALINATION PLANT MANUFACTURER

meidir 说:
2024年1月02日 05:07

Over and over again I think about these issue. As a matter of fact it was not even yesterday that I last thought about it. To be honest, what is your thought though? Pool companies in Gatlinburg




very nice post, i definitely enjoy this amazing site, persist in it web design sevierville




Wohh just what I was looking for, thanks for putting up. lip balm




You are my inspiration I possess few web logs and sometimes run out from to post . Horseback riding Gatlinburg

meidir 说:
2024年1月08日 10:35

I’m keen on this post, enjoyed this one thank you posting . casa de cambio






I discovered your blog site on google and appearance a few of your early posts. Keep up the excellent operate. I simply extra the RSS feed to my MSN News Reader. Looking for forward to reading more on your part later on!… fisio

meidir 说:
2024年1月08日 22:33

Very interesting topic , regards for putting up. permis de construire architecte






The subsequent time I learn a blog, I hope that it doesnt disappoint me as much as this one. I mean, I do know it was my option to read, but I really thought youd have something attention-grabbing to say. All I hear is a bunch of whining about something that you could possibly repair should you werent too busy on the lookout for attention. bigfat cc

meidir 说:
2024年1月18日 05:26

When visiting blogs, i always look for a very nice content like yours “   PFAS CONTAMINATED WATER TREATMENT TECHNIQUES to remove per- and poly-fluoroalkyl compounds (PFAS)








Hi, Could I grab that snapshot and usage it on my own web log? danceable Arabic music

meidir 说:
2024年1月22日 02:35

Can I just say that of a relief to locate someone who actually knows what theyre preaching about online. You definitely learn how to bring a problem to light and work out it important. The best way to ought to ought to see this and appreciate this side from the story. I cant believe youre less well-liked as you certainly possess the gift. Free Downloads






Very great post. I simply stumbled upon your weblog and wanted to mention that I have really enjoyed surfing around your weblog posts. In any case I’ll be subscribing to your feed and I hope you write again soon! Rappelz

meidir 说:
2024年1月30日 08:11

Excellent blog here! after reading, i decide to buy a sleeping bag ASAP car tyres Birmingham






My brother recommended I would possibly like this web site. He was once entirely right. This submit truly made my day. You can not consider just how a lot time I had spent for this info! Thank you! coffee






Appreciation for the great blog post. I am glad I have taken the time to learn this. hotels






Great info – Gulvafslibning | Kurt Gulvmand I’m inquisitive to find out exactly what blog platform you’re using? We are experiencing several minor safety issues with my blog plus I would wish to locate something better and secure. Do you have some solutions! Oh yeah how about Egypt wonderful media stories Regards … Flash Factory car events

meidir 说:
2024年2月08日 23:14

Normally I do not learn article on blogs, however I wish to say that this write-up very compelled me to take a look at and do it! Your writing style has been amazed me. Thank you, very great post. Centro de Emprego




I’ve been exploring for a little bit for any high quality articles or blog posts on this kind of space . Exploring in Yahoo I ultimately stumbled upon this web site. Reading this info So i¡¦m glad to show that I have an incredibly excellent uncanny feeling I found out just what I needed. I most indisputably will make sure to don¡¦t omit this web site and provides it a look a continuing. masaje tantrico palma




Well done! I thank you your contribution to this matter. It has been useful. my blog: exercise to reduce tummy empresa de control de plagas en Madrid




Sometimes, blogging is a bit tiresome specially if you need to update more topics.”,** réparation ordinateur boulogne billancourt

meidir 说:
2024年2月12日 22:18

I always was interested in this subject and still am, appreciate it for putting up. locksmith






there are dining sets come in wide variety and prices, i often get the lower priced ones~ Local refrigerator repair

登录 *

loading captcha image...
or Ctrl+Enter