U E D R , A S I H C RSS

EffectiveSTL/Programming WithSTL


1. Item43. Prefer algorithm calls to hand-written loops.


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

2.1. 서론

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

2.2. 예제

  • 저걸 즉시 지킬수 있게 하는 예제를 보이겠다.

~cpp 
set<int> s;            // s에는 원소가 백만개 들어있다. 이 중에서 727이라는 원소를 찾고 싶다.
typedef set<int>::iterator SIIT;
...

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

...

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

2.3. 설명

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



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


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


5. Item47. Avoid producing write-only code.


6. Item48. Always #include the proper headers.


7. Item49. Learn to decipher STL_related compiler diagnostics.


8. Item50. Familiarize yourself with STL-releated websites.

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:10
Processing time 0.0132 sec