Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

EffectiveSTL/ProgrammingWithSTL

From ZeroWiki
Revision as of 05:23, 7 February 2021 by imported>Unknown
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Item43. Prefer algorithm calls to hand-written loops.


Item44. Prefer member functions to algorithms with the same names.

서론

  • 제목 그대로.. 같은 이름의 알고리즘을 쓰는 대신에, 그 컨테이너 객체의 같은 이름의 메소드를 쓰는게 낫다.
  • 이유는, 더 효율적이기 때문에.
  • 또 다른 이유는, equivalnce와 equality의 차이에서 오는 성공 여부래는데.. 이 챕터는 아직 안봤다.

예제

  • 저걸 즉시 지킬수 있게 하는 예제를 보이겠다.
set<int> s;            // s에는 원소가 백만개 들어있다. 이 중에서 727이라는 원소를 찾고 싶다.
typedef set<int>::iterator SIIT;
...

// 메소드 호출 버젼
SIIT i = s.find(727);

...

// 알고리즘 호출 버젼
SIIT j = find(s.begin(), s.end(), 727)

설명

  • set의 find메소드는 로그시간내에 수행된다. 하지만 find 알고리즘은 선형시간내에 수행된다. 즉
    • find메소드의 average case는 20, worst case는 40.
    • find알고리즘의 average case는 5십만, worst case는 백만.
    • 이 결과를 보면 당연히 저 위의것을 지키기 될 것이다.



Item45. Distinguish among count, find, binary_search, lower_bound, upper_bound, and equal_range.


Item46. Consider function objects instead of functions as algorithm parameters.


Item47. Avoid producing write-only code.


Item48. Always #include the proper headers.


Item49. Learn to decipher STL_related compiler diagnostics.


Item50. Familiarize yourself with STL-releated websites.


EffectiveSTL