ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Algorithm (find , find_if)
    C++ 2014. 2. 22. 22:00
    1. 알고리즘은 STL 제공하는 Container 무관하게 상호연동이 가능한 Generic Algorithm 제공한다.
    2. map이나 set 같이 자체적으로 알고리즘을 제공하는 경우 성능면에서 자체 알고리즘을 사용하는 것이 좋다.

       

     

    #include <iostream>

    #include <vector>

    #include <algorithm>

     

    using namespace std;

     

    bool passornot(int score) { return score >= 80; }

     

    void main()

    {

        cout << "Main " << endl;

        int num ;

        vector<int> myNum;

     

        while (true) {

            cout << "Enter a numnber (exit for -1) " && cin >> num;

         if (num==-1) break;

            myNum.push_back(num);

        }

    /*  find algorithm

        while (true) {

            cout << "Enter a numnber find (exit for -1) " && cin >> num;

         if (num==-1) break;

            auto it = find( myNum.begin(), myNum.end(), num);

            if (it==myNum.end())

                cout << *it << " not found"<< endl;

            else

                cout << *it << " found"<< endl;

        }

    */

    // find_if 첫번째 true return 될때까지 찾고 리턴을 한다.

    // find_all 지원하지 않는다.

    double sum = accumulate( myNum.begin(), myNum.end(), 0);

    cout << sum << "  is accumulated "<< endl;

    //    auto it = find_if( myNum.begin(), myNum.end(), passornot);

        //lamda

        auto it = find_if( myNum.begin(), myNum.end(), [] (int i) { return i >= 80;} );

     

        if (it==myNum.end())

            cout << *it << " no acore to pass"<< endl;

        else

            cout << *it << " found "<< endl;

     

    }

    'C++' 카테고리의 다른 글

    string class operator overloding  (0) 2014.02.26
    Character array literal  (0) 2014.02.25
    Bitset 의 예  (0) 2014.02.22
    MultiMap  (0) 2014.02.22
    STL map 의 정리  (0) 2014.02.20
Designed by Tistory.