ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 제네릭 알고리즘
    제네릭 프로그래밍 2009. 1. 1. 00:37
    컨테이너 요소들을 다룬다.
    알고리즘은 그들이 사용하는 반복자들에 대해 어떠한 조건이 필요한지를 확실히 이해해야 한다.


    template <typename IteratorType, typename elemType >
    IteratorType
    find( IteratorType first, IteratorType last,
    const elemType &value )
    {
    for ( ; first != last; ++first )
    if  ( value == *first )
    return first;

    return last;
    }


    template <typename InputIterator, typename OutputIterator,
    typename ElemType, typename Comp>
    OutputIterator
    filter( InputIterator first, InputIterator last,
    OutputIterator at, const ElemType &val, Comp pred )
    {
    while (( first =
    find_if( first, last,
    bind2nd( pred, val ))) != last )
    {
    *at++ = *first++;
    }

    return at;
    }


    #include <algorithm>
    #include <cctype>
    #include <string>

    using std::find_if;
    using std::string;

    using std::isspace;

    inline bool space(char c)
    {
            return isspace(c);
    }

    inline bool not_space(char c)
    {
            return !isspace(c);
    }

    template <class Out>
    void split(const string& str, Out os) {

        typedef string::const_iterator iter;

        iter i = str.begin();
        while (i != str.end()) {
            // ignore leading blanks
            i = find_if(i, str.end(), not_space);

            // find end of next word
            iter j = find_if(i, str.end(), space);

            // copy the characters in `[i,' `j)'
            if (i != str.end())
                *os++ = string(i, j);

            i = j;
        }
    }


    #include <iterator>
    #include <iostream>
    #include <string>
    #include "split.h"

    using std::cin;
    using std::cout;
    using std::getline;
    using std::ostream_iterator;
    using std::string;

    int main()
    {
        string s;
        while (getline(cin, s))
            split(s, ostream_iterator<string>(cout, "\n"));
        return 0;
    }


    #include <algorithm>
    #include <cctype>
    #include <string>
    #include <vector>

    #include "split.h"

    using std::find_if;
    using std::string;
    using std::vector;

    using std::isspace;

    // `true' if the argument is whitespace, `false' otherwise
    bool space(char c)
    {
        return isspace(c);
    }

    // `false' if the argument is whitespace, `true' otherwise
    bool not_space(char c)
    {
        return !isspace(c);
    }

    vector<string> split(const string& str)
    {
        typedef string::const_iterator iter;
        vector<string> ret;

        iter i = str.begin();
        while (i != str.end()) {

            // ignore leading blanks
            i = find_if(i, str.end(), not_space);

            // find end of next word
            iter j = find_if(i, str.end(), space);

            // copy the characters in `[i,' `j)'
            if (i != str.end())
                ret.push_back(string(i, j));
            i = j;
        }
        return ret;
    }


    #include <cctype>
    #include <iostream>
    #include <string>
    #include <vector>

    #include "split.h"

    using std::cin;
    using std::cout;
    using std::endl;
    using std::getline;
    using std::string;
    using std::vector;

    using std::isspace;

    int main()
    {
        string s;

        // read and split each line of input
        while (getline(cin, s)) {
            vector<string> v = split(s);

            // write each word in `v'
            for (vector<string>::size_type i = 0; i != v.size(); ++i)
                cout << v[i] << endl;
        }
        return 0;
    }


    #include <cctype>
    #include <string>
    #include <vector>

    #include "split.h"

    using std::vector;
    using std::string;

    #ifndef _MSC_VER
    using std::isspace;
    #endif

    vector<string> split(const string& s)
    {
        vector<string> ret;
        typedef string::size_type string_size;
        string_size i = 0;

        // invariant: we have processed characters `['original value of `i', `i)'
        while (i != s.size()) {
            // ignore leading blanks
            // invariant: characters in range `['original `i', current `i)' are all spaces
            while (i != s.size() && isspace(s[i]))
                ++i;

            // find end of next word
            string_size j = i;
            // invariant: none of the characters in range `['original `j', current `j)' is a space
            while (j != s.size() && !isspace(s[j]))
                ++j;

            // if we found some nonwhitespace characters
            if (i != j) {
                // copy from `s' starting at `i' and taking `j' `\-' `i' chars
                ret.push_back(s.substr(i, j - i));
                i = j;
            }

        }
        return ret;
    }


Designed by Tistory.