stl 簡單測試代碼


 
 //C++頭文件一覽
#include <assert.h>;    //設定插入點
#include <ctype.h>;     //字符處理
#include <errno.h>;     //定義錯誤碼
#include <float.h>;     //浮點數處理
#include <fstream.h>;    //文件輸入/輸出
#include <iomanip.h>;    //參數化輸入/輸出
#include <iostream.h>;   //數據流輸入/輸出
#include <limits.h>;    //定義各種數據類型最值常量
#include <locale.h>;    //定義本地化函數
#include <math.h>;     //定義數學函數
#include <stdio.h>;     //定義輸入/輸出函數
#include <stdlib.h>;    //定義雜項函數及內存分配函數
#include <string.h>;    //字符串處理
#include <strstrea.h>;   //基於數組的輸入/輸出
#include <time.h>;     //定義關於時間的函數
#include <wchar.h>;     //寬字符處理及輸入/輸出
#include <wctype.h>;    //寬字符分類

//標準 C++ (同上的不再註釋)
#include <algorithm>;    //STL 通用算法
#include <bitset>;     //STL 位集容器
#include <cctype>;
#include <cerrno>;
#include <clocale>;
#include <cmath>;
#include <complex>;     //複數類
#include <cstdio>;
#include <cstdlib>;
#include <cstring>;
#include <ctime>;
#include <deque>;      //STL 雙端隊列容器
#include <exception>;    //異常處理類
#include <fstream>;
#include <functional>;   //STL 定義運算函數(代替運算符)
#include <limits>;
#include <list>;      //STL 線性列表容器
#include <map>;       //STL 映射容器
#include <iomanip>;
#include <ios>;       //基本輸入/輸出支持
#include <iosfwd>;     //輸入/輸出系統使用的前置聲明
#include <iostream>;
#include <istream>;     //基本輸入流
#include <ostream>;     //基本輸出流
#include <queue>;      //STL 隊列容器
#include <set>;       //STL 集合容器
#include <sstream>;     //基於字符串的流
#include <stack>;      //STL 堆棧容器    
#include <stdexcept>;    //標準異常類
#include <streambuf>;    //底層輸入/輸出支持
#include <string>;     //字符串類
#include <utility>;     //STL 通用模板類
#include <vector>;     //STL 動態數組容器
#include <cwchar>;
#include <cwctype>;

using namespace std;

//////////////////////////////////////////////////////////////////////////
//C99 增加
#include <complex.h>;   //複數處理
#include <fenv.h>;    //浮點環境
#include <inttypes.h>;  //整數格式轉換
#include <stdbool.h>;   //布爾環境
#include <stdint.h>;   //整型環境
#include <tgmath.h>;   //通用類型數學宏

/*-----------------vector----------------------*/
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> coll;

    for(int i = 1; i <= 6; ++i)
        coll.push_back(i);

    for(int i = 0; i < coll.size(); ++i)
        cout << coll[i] << ' ';

    cout << endl;
}
/*-----------------deque----------------------*/
#include <iostream>
#include <deque>

using namespace std;

int main()
{
    deque<float> coll;

    for(int i = 1; i <= 6; ++i)
        coll.push_front(i*1.1);

    for(int i = 0; i < coll.size(); ++i)
        cout << coll[i] << ' ';

    cout << endl;
}
/*-----------------list----------------------*/
#include <iostream>
#include <list>

using namespace std;

int main()
{
    list<char> coll;

    for(char c='a'; c <= 'z'; ++c)
        coll.push_back(c);

    while( !coll.empty())
    {
        cout << coll.front();
        coll.pop_front();
    }
    
    cout << endl;
}
/*-----------------iterator----------------------*/
#include <iostream>
#include <list>

using namespace std;

int main()
{
    list<char> coll;

    for(char c='a'; c <= 'z'; ++c)
        coll.push_back(c);

    list<char>::const_iterator pos;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    
    cout << endl;
}
/*-----------------set----------------------*/
#include <iostream>
#include <set>

using namespace std;

int main()
{
    typedef std::set<int> IntSet;

    IntSet coll;

    coll.insert(3);
    coll.insert(1);
    coll.insert(5);
    coll.insert(4);
    coll.insert(1);
    coll.insert(6);
    coll.insert(2);

    IntSet::const_iterator pos;

    for(pos = coll.begin(); pos != coll.end(); ++pos)
        std::cout << *pos << ' ';

    cout << endl;
}
/*-----------------multimap----------------------*/
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main()
{
    typedef multimap<int, string> IntStringMMap;

    IntStringMMap coll;

    coll.insert(make_pair(5, "tagged"));
    coll.insert(make_pair(2, "a"));
    coll.insert(make_pair(1, "this"));
    coll.insert(make_pair(4, "of"));
    coll.insert(make_pair(6, "string"));
    coll.insert(make_pair(1, "is"));
    coll.insert(make_pair(3, "multimap"));

    IntStringMMap::iterator pos;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << pos->second << ' ';

    cout << endl;
}
/*-----------------map----------------------*/
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main()
{
    typedef map<string, float> StringFloatMap;
    StringFloatMap coll;

    coll["VAT"] = 0.15;
    coll["PI"] = 3.1415;
    coll["an arbitrary number"] = 4983.223;
    coll["Null"] = 0;

    StringFloatMap::iterator pos;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << "key: \"" << pos->first << "\" "
            << "value: " << pos->second << endl;
}
/*-----------------algorithm----------------------*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    vector<int> coll;
    vector<int>::iterator pos;

    coll.push_back(2);
    coll.push_back(5);
    coll.push_back(4);
    coll.push_back(1);
    coll.push_back(6);
    coll.push_back(3);

    pos = min_element(coll.begin(), coll.end());
    cout << "min: " << *pos << endl;
    pos = max_element(coll.begin(), coll.end());
    cout << "max: " << *pos << endl;

    sort(coll.begin(), coll.end());

    pos = find(coll.begin(), coll.end(), 3);

    reverse(pos, coll.end());

    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';

    cout << endl;
}
/*-----------------find----------------------*/
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

int main()
{
    list<int> coll;
    list<int>::iterator pos;

    for(int i = 20; i <= 40; ++i)
        coll.push_back(i);

    pos = find(coll.begin(), coll.end(), 3);

    reverse(pos, coll.end());

    list<int>::iterator pos25, pos35;
    pos25 = find(coll.begin(), coll.end(), 25);
    pos35 = find(coll.begin(), coll.end(), 35);


    cout << "max: " << *max_element(pos25, pos35) << endl;

    cout << "max: " << *max_element(pos25, ++pos35) << endl;
}
/*-----------------copy----------------------*/
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <algorithm>
using namespace std;

int main()
{
    list<int> coll1;
    vector<int> coll2;

    for(int i = 1; i <= 9; ++i)
        coll1.push_back(i);

    coll2.resize(coll1.size());
    copy(coll1.begin(), coll1.end(), coll2.begin());

    for(int i = 0; i < coll2.size(); ++i)
        cout << coll2[i] << ' ';

    cout << endl;

    deque<int> coll3(coll1.size());

    copy(coll1.begin(), coll1.end(), coll3.begin());

    for(int i = 0; i < coll3.size(); ++i)
        cout << coll3[i] << ' ';

    cout << endl;
}
/*-----------------copy3----------------------*/
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <algorithm>
using namespace std;

int main()
{
    list<int> coll1;

    for(int i = 1; i <= 9; ++i)
        coll1.push_back(i);

    vector<int> coll2;
    copy(coll1.begin(), coll1.end(), back_inserter(coll2));

    deque<int> coll3;
    copy(coll1.begin(), coll1.end(), front_inserter(coll3));

    set<int> coll4;
    copy(coll1.begin(), coll1.end(), inserter(coll4, coll4.begin()));

    while( !coll1.empty())
    {
        cout << coll1.front();
        coll1.pop_front();
    }
    cout << endl;

    for(int i = 0; i < coll2.size(); ++i)
        cout << coll2[i] << ' ';
    cout << endl;

    for(int i = 0; i < coll3.size(); ++i)
        cout << coll3[i] << ' ';
    cout << endl;

    set<int>::const_iterator pos;
    for(pos = coll4.begin(); pos != coll4.end(); ++pos)
        std::cout << *pos << ' ';
    cout << endl;
}
/*-----------------ioiter1----------------------*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;

int main()
{
    vector<string> coll;

    copy(istream_iterator<string>(cin),
         istream_iterator<string>(),
         back_inserter(coll));

    sort(coll.begin(), coll.end());

    unique_copy(coll.begin(), coll.end(),
                ostream_iterator<string>(cout, "\n"));

}
/*-----------------riter1----------------------*/
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
    vector<int> coll;

    for(int i = 1; i <= 9; ++i)
        coll.push_back(i);

    copy(coll.rbegin(), coll.rend(), ostream_iterator<int>(cout, " "));

    cout << endl;
}
/*-----------------remove----------------------*/
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
    list<int> coll;

    for(int i = 1; i <= 6; ++i)
    {
        coll.push_front(i);
        coll.push_back(i);

        cout << "pre: ";
        copy(coll.begin(), coll.end(),
             ostream_iterator<int> (cout, " "));
        cout << endl;

        remove(coll.begin(), coll.end(), 3);

        cout << "post: ";
        copy(coll.begin(), coll.end(),
             ostream_iterator<int> (cout, " "));
        cout << endl;
    }
}
/*-----------------remove2----------------------*/
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
    list<int> coll;

    for(int i = 1; i <= 6; ++i)
    {
        coll.push_front(i);
        coll.push_back(i);
    }
    
    copy(coll.begin(), coll.end(),
         ostream_iterator<int>(cout, " "));
    cout << endl;

    list<int>::iterator end = remove(coll.begin(), coll.end(), 3);

    copy(coll.begin(), coll.end(),
            ostream_iterator<int>(cout, " "));
    cout << endl;

    cout << "number of resulting elements: "
        << distance(end, coll.end()) << endl;

    coll.erase(end, coll.end());

    copy(coll.begin(), coll.end(),
            ostream_iterator<int>(cout, " "));
    cout << endl;
}
/*-----------------remove3----------------------*/
#include <iostream>
#include <set>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
    set<int> coll;

    for(int i = 1; i <= 9; ++i)
        coll.insert(i);

    copy(coll.begin(), coll.end(),
            ostream_iterator<int>(cout, " "));
    cout << endl;

    int num = coll.erase(3);

    cout << "number of removed element: " << num << endl;

    copy(coll.begin(), coll.end(),
            ostream_iterator<int>(cout, " "));
    cout << endl;

}
/*-----------------remove4----------------------*/
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
    list<int> coll;

    for(int i = 1; i <= 6; ++i)
    {
        coll.push_front(i);
        coll.push_back(i);
    }

    coll.erase(remove(coll.begin(), coll.end(), 3), coll.end());

    coll.remove(4);

    while( !coll.empty())
    {
        cout << coll.front();
        coll.pop_front();
    }
    
    cout << endl;
}
/*-----------------foreach----------------------*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void print(int elem)
{
    cout << elem << ' ';
}

int main()
{
    vector<int> coll;

    for(int i = 1; i <= 9; ++i)
        coll.push_back(i);

    for_each(coll.begin(), coll.end(), print);
    cout << endl;
}
/*-----------------transform1----------------------*/
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;


template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

int square(int value)
{
    return value*value;
}

int main()
{
    set<int> coll1;
    vector<int> coll2;

    for(int i = 1; i <= 9; ++i)
        coll1.insert(i);
    PRINT_ELEMENTS(coll1, "initialized: ");
    transform(coll1.begin(), coll1.end(),
            back_inserter(coll2),
            square);
    PRINT_ELEMENTS(coll2, "square:          ");
}
/*-----------------prime1----------------------*/
#include <iostream>
#include <list>
#include <algorithm>
#include <cstdlib>
using namespace std;

bool isPrime(int number)
{
    number = abs(number);

    if(number == 0 || number == 1)
        return true;

    int divisor;
    for(divisor = number/2; number%divisor != 0; --divisor);

    return divisor == 1;
}

int main()
{
    list<int> coll;

    for(int i = 24; i <= 30; ++i)
        coll.push_back(i);

    list<int>::iterator pos;
    pos = find_if(coll.begin(), coll.end(),
            isPrime);
    if(pos != coll.end())
        cout << *pos << " is first prime number found" << endl;
    else
        cout << "no prime number found" << endl;
}
/*-----------------sort1----------------------*/
#include <iostream>
#include <string>
#include <deque>
#include <algorithm>
using namespace std;

class Person
{
    public:
        string firstname() const;
        string lastname() const;
};

bool personSortCriterion(const Person& p1, const Person& p2)
{
    return p1.lastname() < p2.lastname() ||
        (!(p2.lastname()<p1.lastname()) &&
         p1.firstname() < p2.firstname());
}

int main()
{
    deque<Person> coll;

    sort(coll.begin(), coll.end(), personSortCriterion);
}
/*-----------------foreach2----------------------*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class PrintInt
{
    public:
        void operator() (int elem) const
        { cout << elem << ' '; }
};

int main()
{
    vector<int> coll;

    for(int i = 1; i <= 9; ++i)
        coll.push_back(i);

    for_each(coll.begin(), coll.end(), PrintInt());
    cout << endl;
}
/*-----------------fo1----------------------*/
#include <iostream>
#include <set>
#include <deque>
#include <algorithm>
using namespace std;


template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

int main()
{
    set< int, greater<int> > coll1;
    deque<int> coll2;
    for(int i = 1; i <= 9; ++i)
        coll1.insert(i);
    
    PRINT_ELEMENTS(coll1, "initialized: ");
    transform(coll1.begin(), coll1.end(),
            back_inserter(coll2),
            bind2nd(multiplies<int>(), 10));

    PRINT_ELEMENTS(coll2, "transformed: ");
    replace_if(coll2.begin(), coll2.end(),
            bind2nd(equal_to<int>(), 70), 42);

    PRINT_ELEMENTS(coll2, "replaced: ");

    coll2.erase(remove_if(coll2.begin(), coll2.end(),
                bind2nd(less<int>(), 50)), coll2.end());
    PRINT_ELEMENTS(coll2, "removed:    ");
}
/*-----------------vector1----------------------*/
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
    vector<string> sentence;

    sentence.reserve(5);

    sentence.push_back("Hello");
    sentence.push_back("how");
    sentence.push_back("are");
    sentence.push_back("you");
    sentence.push_back("?");

    copy(sentence.begin(), sentence.end(),
            ostream_iterator<string>(cout, " "));
    cout << endl;

    cout << " max_size(): " << sentence.max_size() << endl;
    cout << " size():     " << sentence.size()     << endl;
    cout << " capacity(): " << sentence.capacity() << endl;

    swap(sentence[1], sentence[3]);
    sentence.insert(find(sentence.begin(), sentence.end(), "?"),
            "always");

    sentence.back() = "!";
    copy(sentence.begin(), sentence.end(),
            ostream_iterator<string>(cout, " "));
    cout << endl;

    cout << " max_size(): " << sentence.max_size() << endl;
    cout << " size():     " << sentence.size()     << endl;
    cout << " capacity(): " << sentence.capacity() << endl;
}
/*-----------------deque1----------------------*/
#include <iostream>
#include <deque>
#include <string>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
    deque<string> coll;
    
    coll.assign(3, string("string"));
    coll.push_back("last string");
    coll.push_front("first string");
    
    copy(coll.begin(), coll.end(),
            ostream_iterator<string>(cout, "\n"));
    cout << endl;

    coll.pop_front();
    coll.pop_back();

    for(int i = 1; i < coll.size(); ++i)
        coll[i] = "another " + coll[i];

    coll.resize(4, "resized string");

    copy(coll.begin(), coll.end(),
            ostream_iterator<string>(cout, "\n"));
}
/*-----------------list1----------------------*/
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
using namespace std;

void printLists(const list<int> &l1, const list<int>& l2)
{
    cout << "list1: ";
    copy(l1.begin(), l1.end(),
            ostream_iterator<int>(cout, " "));
    cout << endl << "list2: ";
    copy(l2.begin(), l2.end(),
            ostream_iterator<int>(cout, " "));
    cout << endl << endl;
}

int main()
{
    list<int> list1, list2;
    
    for(int i = 0; i < 6; ++i)
    {
        list1.push_back(i);
        list2.push_front(i);
    }
    printLists(list1, list2);

    list2.splice(find(list2.begin(), list2.end(), 3), list1);
    printLists(list1, list2);
    
    list2.splice(list2.end(), list2, list2.begin());
    printLists(list1, list2);

    list2.sort();
    list1 = list2;
    list2.unique();
    printLists(list1, list2);

    list1.merge(list2);
    printLists(list1, list2);
}
/*-----------------set2----------------------*/
#include <iostream>
#include <set>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
    set<int> c;

    c.insert(1);
    c.insert(2);
    c.insert(4);
    c.insert(5);
    c.insert(6);

    cout << "lower_bound(3): " << *c.lower_bound(3) << endl;
    cout << "upper_bound(3): " << *c.upper_bound(3) << endl;
    cout << "equal_range(3): " << *c.equal_range(3).first << " "
                               << *c.equal_range(3).second << endl;
    cout << endl;
    cout << "lower_bound(5): " << *c.lower_bound(5) << endl;
    cout << "upper_bound(5): " << *c.upper_bound(5) << endl;
    cout << "equal_range(5): " << *c.equal_range(5).first << " "
                               << *c.equal_range(5).second << endl;

}
/*-----------------set1----------------------*/
#include <iostream>
#include <set>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
    typedef set<int, greater<int> > IntSet;

    IntSet coll1;

    coll1.insert(4);
    coll1.insert(3);
    coll1.insert(5);
    coll1.insert(1);
    coll1.insert(6);
    coll1.insert(2);
    coll1.insert(5);

    IntSet::iterator pos;
    for(pos = coll1.begin(); pos != coll1.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;

    pair<IntSet::iterator, bool> status = coll1.insert(4);
    if(status.second)
        cout << "4 inserted ad element "
             << distance(coll1.begin(), status.first) + 1
             << endl;
    else
        cout << "4 already exists" << endl;

    set<int> coll2(coll1.begin(), coll1.end());

    copy(coll2.begin(), coll2.end(),
            ostream_iterator<int>(cout, " "));
    cout << endl;

    coll2.erase(coll2.begin(), coll2.find(3));

    int num;
    num = coll2.erase(5);
    cout << num << " element(s) removed" << endl;

    copy(coll2.begin(), coll2.end(),
            ostream_iterator<int>(cout, " "));
    cout << endl;
}
/*-----------------mset1----------------------*/
#include <iostream>
#include <set>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
    typedef multiset<int, greater<int> > IntSet;

    IntSet coll1;

    coll1.insert(4);
    coll1.insert(3);
    coll1.insert(5);
    coll1.insert(1);
    coll1.insert(6);
    coll1.insert(2);
    coll1.insert(5);

    IntSet::iterator pos;

    for(pos = coll1.begin(); pos != coll1.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;

    IntSet::iterator ipos = coll1.insert(4);
    cout << "4 inserted as element "
         << distance(coll1.begin(), ipos) + 1
         << endl;

    multiset<int> coll2(coll1.begin(), coll1.end());
    copy(coll2.begin(), coll2.end(),
            ostream_iterator<int>(cout, " "));
    cout << endl;

    coll2.erase(coll2.begin(), coll2.find(3));

    int num;
    num = coll2.erase(5);
    cout << num << " element(s) removed" << endl;

    copy(coll2.begin(), coll2.end(),
            ostream_iterator<int>(cout, " "));
    cout << endl;
    
}
/*-----------------setcmp----------------------*/
#include <iostream>
#include <set>
#include <iterator>
#include <algorithm>
using namespace std;


template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
class RuntimeCmp
{
    public:
        enum cmp_mode{normal, reverse};
    private:
        cmp_mode mode;
    public:
        RuntimeCmp(cmp_mode m = normal) : mode(m){}
        bool operator ()(const T& t1, const T& t2) const
        {
            return mode == normal ? t1 < t2 : t2 < t1;
        }
        bool operator ==(const RuntimeCmp& rc)
        {
            return mode == rc.mode;
        }
};

typedef set<int, RuntimeCmp<int> > IntSet;

void fill(IntSet& set);

int main()
{
    IntSet coll1;
    fill(coll1);
    PRINT_ELEMENTS(coll1, "coll1: ");
    RuntimeCmp<int> reverse_order(RuntimeCmp<int>::reverse);
    
    IntSet coll2(reverse_order);
    fill(coll2);
    PRINT_ELEMENTS(coll2, "coll2: ");

    coll1 = coll2;
    coll1.insert(3);
    PRINT_ELEMENTS(coll1, "coll1: ");

    if(coll1.value_comp() == coll2.value_comp())
        cout << "coll1 and coll2 have same sorting criterion"
             << endl;
    else
        cout << "coll1 and coll2 have different sorting criterion"
             << endl;
}

void fill(IntSet& set)
{
    set.insert(4);
    set.insert(7);
    set.insert(5);
    set.insert(1);
    set.insert(6);
    set.insert(2);
    set.insert(5);
}
/*-----------------map1----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
    typedef map<string, float> StringFloatMap;

    StringFloatMap stocks;

    stocks["BASF"] = 369.50;
    stocks["VW"] = 413.50;
    stocks["Daimler"] = 819.00;
    stocks["BMW"] = 834.00;
    stocks["Siemens"] = 842.20;

    StringFloatMap::iterator pos;
    for(pos = stocks.begin(); pos != stocks.end(); ++pos)
        cout << "stock: " << pos->first << "\t"
             << "price: " << pos->second << endl;
    cout << endl;

    for(pos = stocks.begin(); pos != stocks.end(); ++pos)
        pos->second *= 2;

    for(pos = stocks.begin(); pos != stocks.end(); ++pos)
        cout << "stock: " << pos->first << "\t"
             << "price: " << pos->second << endl;
    cout << endl;

    stocks["Volkswagen"] = stocks["VW"];
    stocks.erase("VW");

    for(pos = stocks.begin(); pos != stocks.end(); ++pos)
        cout << "stock: " << pos->first << "\t"
             << "price: " << pos->second << endl;
}
/*-----------------mmap1----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <iomanip>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
    typedef multimap<string, string> StrStrMMap;

    StrStrMMap dict;

    dict.insert(make_pair("day", "Tag"));
    dict.insert(make_pair("strang", "fremd"));
    dict.insert(make_pair("car", "Auto"));
    dict.insert(make_pair("smart", "elegant"));
    dict.insert(make_pair("trait", "Merkmal"));
    dict.insert(make_pair("strange", "seltsam"));
    dict.insert(make_pair("smart", "raffiniert"));
    dict.insert(make_pair("smart", "klug"));
    dict.insert(make_pair("clever", "raffiniert"));
    
    StrStrMMap::iterator pos;
    cout.setf(ios::left, ios::adjustfield);
    cout << ' ' << setw(10) << "english "
         << "german " << endl;
    cout << setfill('-') << setw(20) << ""
         << setfill(' ') <<endl;
    for(pos = dict.begin(); pos != dict.end(); ++pos)
        cout << ' ' << setw(10) << pos->first.c_str()
             << pos->second << endl;
    cout << endl;

    string word("smart");
    cout << word << ": " << endl;

    for(pos = dict.lower_bound(word);
        pos != dict.upper_bound(word); ++pos)
        cout << " " << pos->second << endl;

    word = ("raffiniert");
    cout << word << ": " << endl;
    for(pos = dict.begin(); pos != dict.end(); ++pos)
        if(pos->second == word)
            cout << " " << pos->first << endl;
}
/*-----------------mapfind----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <iomanip>
#include <iterator>
#include <algorithm>
using namespace std;

template <class K, class V>
class value_equals
{
    private:
        V value;
    public:
        value_equals(const V& v): value(v){}
        bool operator ()(pair<const K, V> elem)
        { return elem.second == value; }
};

int main()
{
    typedef map<float, float> FloatFloatMap;
    FloatFloatMap coll;
    FloatFloatMap::iterator pos;

    coll[1] = 7;
    coll[2] = 4;
    coll[3] = 2;
    coll[4] = 3;
    coll[5] = 6;
    coll[6] = 1;
    coll[7] = 3;

    pos = coll.find(3.0);
    if(pos != coll.end())
        cout << pos->first << ": "
             << pos->second << endl;
    pos = find_if(coll.begin(), coll.end(),
            value_equals<float, float>(3.0));
    if(pos != coll.end())
        cout << pos->first << ": "
             << pos->second << endl;
}
/*-----------------mapcmp----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <iomanip>
#include <iterator>
#include <algorithm>
using namespace std;

class RuntimeStringCmp
{
    public:
        enum cmp_mode {normal, nocase};
    private:
        const cmp_mode mode;

        static bool nocase_compare(char c1, char c2)
        { return toupper(c1) < toupper(c2); }
    public:
        RuntimeStringCmp (cmp_mode m=normal) : mode(m) {}
        bool operator () (const string& s1, const string& s2) const
        {
            if(mode == normal)
                return s1 < s2;
            else
                return lexicographical_compare(s1.begin(), s1.end(),
                                               s2.begin(), s2.end(),
                                               nocase_compare);
        }
};

typedef map<string, string, RuntimeStringCmp> StringStringMap;

void fillAndPrint(StringStringMap& coll);

int main()
{
    StringStringMap coll1;
    fillAndPrint(coll1);

    RuntimeStringCmp ignorecase(RuntimeStringCmp::nocase);

    StringStringMap coll2(ignorecase);
    fillAndPrint(coll2);
}

void fillAndPrint(StringStringMap& coll)
{
    coll["Deutschland"] = "Germany";
    coll["deutsch"] = "German";
    coll["Haken"] = "snag";
    coll["arbeiten"] = "work";
    coll["Hund"] = "dog";
    coll["geben"] = "go";
    coll["Unternehmen"] = "enterprise";
    coll["unternehmen"] = "undertake";
    coll["gehen"] = "walk";
    coll["Bestatter"] = "undertaker";

    StringStringMap::iterator pos;
    cout.setf(ios::left, ios::adjustfield);
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << setw(15) << pos->first.c_str() << " "
             << pos->second << endl;
    cout << endl;
}
/*-----------------array1----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <iomanip>
#include <iterator>
#include <algorithm>
using namespace std;

int main()
{
    int coll[] = { 5, 6, 2, 4, 1, 3 };

    transform(coll, coll+6,
              coll,
              coll,
              multiplies<int>());
    sort(coll+1, coll+6);
    copy(coll, coll+6,
         ostream_iterator<int>(cout, " "));
    cout << endl;
}
/*-----------------carray----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template<class T, size_t thesize>
class carray
{
    private:
        T v[thesize];
    public:
        typedef T        value_type;
        typedef T*       iterator;
        typedef const T* const_iterator;
        typedef T&       reference;
        typedef const T& const_reference;
        typedef size_t   size_type;
        typedef ptrdiff_t difference_type;

        iterator begin() { return v; }
        const_iterator begin() const { return v; }
        iterator end() { return v+thesize; }
        const_iterator end() const { return v+thesize; }

        reference operator[] (size_t i) { return v[i]; }
        const_reference operator [] (size_t i) const { return v+thesize; }

        size_type size() const { return thesize; }
        size_type max_size() const { return thesize; }

        T* as_array() { return v; }
};

int main()
{
    carray<int, 10> a;

    for(unsigned i = 0; i < a.size(); ++i)
        a[i] = i+1;
    PRINT_ELEMENTS(a);
    
    reverse(a.begin(), a.end());
    PRINT_ELEMENTS(a);
    
    transform(a.begin(), a.end(),
              a.begin(),
              negate<int>());
    PRINT_ELEMENTS(a);
}
/*-----------------countptr----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <deque>
#include <list>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

template <class T>
class CountedPtr
{
    private:
        T* ptr;
        long* count;
    public:
        explicit CountedPtr(T* p = 0) : ptr(p), count(new long(1)){}
        
        CountedPtr(const CountedPtr<T>& p) throw()
            : ptr(p.ptr), count(p.count)
        { ++*count; }
        
        ~CountedPtr() throw()
        { dispose(); }

        CountedPtr<T>& operator = (const CountedPtr<T>& p) throw()
        {
            if(this != &p)
            {
                dispose();
                ptr = p.str;
                count = p.count;
                ++*count;
            }
            return *this;
        }

        T& operator*() const throw(){ return *ptr; }
        T* operator->() const throw() { return ptr; }

    private:
        void dispose()
        {
            if(--*count == 0)
            {
                delete count;
                delete ptr;
            }
        }
};

void printCountedPtr(CountedPtr<int> elem)
{
    cout << *elem << ' ';
}

int main()
{
    static int values[] = {3, 5, 9, 1, 6, 4};

    typedef CountedPtr<int> IntPtr;
    deque<IntPtr> coll1;
    list<IntPtr> coll2;

    for(int i=0; i < sizeof(values)/sizeof(values[0]); ++i)
    {
        IntPtr ptr(new int(values[i]));
        coll1.push_back(ptr);
        coll2.push_front(ptr);
    }

    for_each(coll1.begin(), coll1.end(),
             printCountedPtr);
    cout << endl;

    for_each(coll2.begin(), coll2.end(),
             printCountedPtr);
    cout << endl << endl;

    *coll1[2] *= *coll1[2];
    (**coll1.begin()) *= -1;
    (**coll2.begin()) = 0;

    for_each(coll1.begin(), coll1.end(),
             printCountedPtr);
    cout << endl;

    for_each(coll2.begin(), coll2.end(),
             printCountedPtr);
    cout << endl;
}
/*-----------------sortset----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

int main()
{
    set<string> coll((istream_iterator<string>(cin)),
            (istream_iterator<string>()));
    copy(coll.begin(), coll.end(),
            ostream_iterator<string>(cout, "\n"));
}
/*-----------------itercat----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

int main()
{
    vector<int> coll;
    
    for(int i = -3; i <= 9; i++)
        coll.push_back(i);

    cout << "number/distance: " << coll.end()-coll.begin() << endl;

    vector<int>::iterator pos;
    for(pos = coll.begin(); pos < coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;

    for(int i=0; i < coll.size(); ++i)
        cout << coll.begin()[i] << ' ';
    cout << endl;

    for(pos = coll.begin(); pos < coll.end()-1; pos += 2)
        cout << *pos << ' ';
    cout << endl;
}
/*-----------------advanced1----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

int main()
{
    list<int> coll;

    for(int i=1; i<=9; ++i)
        coll.push_back(i);

    list<int>::iterator pos = coll.begin();
    cout << *pos << endl;

    advance(pos, 3);

    cout << *pos << endl;

    advance(pos, -1);

    cout << *pos << endl;

}
/*-----------------distance----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

int main()
{
    list<int> coll;

    for(int i=-3; i <= 9; ++i)
        coll.push_back(i);

    list<int>::iterator pos;
    pos = find(coll.begin(), coll.end(), 5);

    if(pos != coll.end())
        cout << "difference between beginning and 5: "
             << distance(coll.begin(), pos) << endl;
    else
        cout << "5 not found" << endl;

}
/*-----------------swap----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

int main()
{
    list<int> coll;

    for(int i=1; i<=9; ++i)
        coll.push_back(i);

    PRINT_ELEMENTS(coll);

    iter_swap(coll.begin(), ++coll.begin());

    PRINT_ELEMENTS(coll);

    iter_swap(coll.begin(), --coll.end());

    PRINT_ELEMENTS(coll);
}
/*-----------------reviter----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

void print(int elem)
{
    cout << elem << ' ';
}

int main()
{
    list<int> coll;

    for(int i=1; i<=9; ++i)
        coll.push_back(i);

    for_each(coll.begin(), coll.end(), print);
    cout << endl;

    for_each(coll.rbegin(), coll.rend(), print);
    cout << endl;
}
/*-----------------reviter2----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

int main()
{
    vector<int> coll;

    for(int i=1; i<=9; ++i)
        coll.push_back(i);

    vector<int>::iterator pos;
    pos = find(coll.begin(), coll.end(), 5);

    cout << "pos: " << *pos << endl;

    vector<int>::reverse_iterator rpos(pos);

    cout << "rpos: " << *rpos << endl;
}
/*-----------------reviter3----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

void print(int elem)
{
    cout << elem << ' ';
}

int main()
{
    deque<int> coll;

    for(int i=1; i<=9; ++i)
        coll.push_back(i);

    deque<int>::iterator pos1;
    pos1 = find(coll.begin(), coll.end(), 2);

    deque<int>::iterator pos2;
    pos2 = find(coll.begin(), coll.end(), 7);

    for_each(pos1, pos2, print);
    cout << endl;

    deque<int>::reverse_iterator rpos1(pos1);
    deque<int>::reverse_iterator rpos2(pos2);

    for_each(rpos2, rpos1, print);
    cout << endl;
}
/*-----------------reviter4----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

int main()
{
    list<int> coll;

    for(int i=1; i<=9; ++i)
        coll.push_back(i);

    list<int>::iterator pos;
    pos = find(coll.begin(), coll.end(), 5);

    cout << "pos: " << *pos << endl;

    list<int>::reverse_iterator rpos(pos);

    cout << "rpos: " << *rpos << endl;

    list<int>::iterator rrpos;
    rrpos = rpos.base();

    cout << "rrpos: " << *rrpos << endl;
}
/*-----------------backins----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

int main()
{
    vector<int> coll;

    back_insert_iterator<vector<int> > iter(coll);

    *iter = 1;
    iter++;
    *iter = 2;
    iter++;
    *iter = 3;

    PRINT_ELEMENTS(coll);

    back_inserter(coll) = 44;
    back_inserter(coll) = 55;

    PRINT_ELEMENTS(coll);

    copy(coll.begin(), coll.end(), back_inserter(coll));

    PRINT_ELEMENTS(coll);
}
/*-----------------frontins----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

int main()
{
    list<int> coll;

    front_insert_iterator<list<int> > iter(coll);

    *iter = 1;
    iter++;
    *iter = 2;
    iter++;
    *iter = 3;

    PRINT_ELEMENTS(coll);

    front_inserter(coll) = 44;
    front_inserter(coll) = 55;

    PRINT_ELEMENTS(coll);

    copy(coll.begin(), coll.end(), front_inserter(coll));

    PRINT_ELEMENTS(coll);
}
/*-----------------inserter----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

int main()
{
    set<int> coll;

    insert_iterator<set<int> > iter(coll, coll.begin());

    *iter = 1;
    iter++;
    *iter = 2;
    iter++;
    *iter = 3;

    PRINT_ELEMENTS(coll, "set: ");

    inserter(coll, coll.end()) = 44;
    inserter(coll, coll.end()) = 55;

    PRINT_ELEMENTS(coll, "set: ");

    list<int> coll2;
    copy(coll.begin(), coll.end(),
         inserter(coll2, coll2.begin()));

    PRINT_ELEMENTS(coll2, "list: ");

    copy(coll.begin(), coll.end(),
         inserter(coll2, ++coll2.begin()));

    PRINT_ELEMENTS(coll2, "list: ");
}
/*-----------------ostriter----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

int main()
{
    ostream_iterator<int> intWriter(cout, "\n");

    *intWriter = 42;
    intWriter++;
    *intWriter = 77;
    intWriter++;
    *intWriter = -5;

    vector<int> coll;
    for(int i=1; i<=9; ++i)
        coll.push_back(i);
    copy(coll.begin(), coll.end(), ostream_iterator<int>(cout));
    cout << endl;

    copy(coll.begin(), coll.end(),
         ostream_iterator<int>(cout, " < "));
    cout << endl;
}
/*-----------------istriter----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

int main()
{
    istream_iterator<int> intReader(cin);

    istream_iterator<int> intReaderEOF;

    while(intReader != intReaderEOF)
    {
        cout << "once:       " << *intReader << endl;
        cout << "once again: " << *intReader << endl;
        ++intReader;
    }
}
/*-----------------advance2----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

int main()
{
    istream_iterator<string> cinPos(cin);
    ostream_iterator<string> coutPos(cout, " ");

    while(cinPos != istream_iterator<string>())
    {
        advance(cinPos, 2);

        if(cinPos != istream_iterator<string>())
            *coutPos++ = *cinPos++;
    }
    cout << endl;
}
/*-----------------assoiter----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class Container>
class asso_insert_iterator
: public std::iterator<std::output_iterator_tag,
                      void, void, void, void>
{
    protected:
        Container& container;

    public:
        explicit asso_insert_iterator(Container& c)
            : container(c)
        {}

        asso_insert_iterator<Container>&
        operator = (const typename Container::value_type& value)
        {
            container.insert(value);
            return *this;
        }

        asso_insert_iterator<Container>& operator* ()
        { return *this; }

        asso_insert_iterator<Container>& operator++ ()
        { return *this; }

        asso_insert_iterator<Container>& operator++ (int)
        { return *this; }

};

template <class Container>
inline asso_insert_iterator<Container> asso_inserter(Container& c)
{
    return asso_insert_iterator<Container>(c);
}

int main()
{
    set<int> coll;

    asso_insert_iterator<set<int> > iter(coll);

    *iter = 1;
    iter++;
    *iter = 2;
    iter++;
    *iter = 3;

    PRINT_ELEMENTS(coll);

    asso_inserter(coll) = 44;
    asso_inserter(coll) = 55;

    PRINT_ELEMENTS(coll);

    int vals[] = {33, 67, -4, 13, 5, 2};
    copy(vals, vals+(sizeof(vals)/sizeof(vals[0])),
         asso_inserter(coll));

    PRINT_ELEMENTS(coll);
}
/*-----------------general----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

class IntSequence
{
    private:
        int value;
    public:
        IntSequence(int initialValue)
            : value(initialValue)
        { }

        int operator() ()
        { return value++; }
};

int main()
{
    list<int> coll;

    generate_n(back_inserter(coll), 9, IntSequence(1));

    PRINT_ELEMENTS(coll);

    generate(++coll.begin(), --coll.end(), IntSequence(42));

    PRINT_ELEMENTS(coll);
}
/*-----------------general2----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

class IntSequence
{
    private:
        int value;
    public:
        IntSequence(int initialValue)
            : value(initialValue)
        { }

        int operator() ()
        { return value++; }
};

int main()
{
    list<int> coll;
    IntSequence seq(1);

    generate_n<back_insert_iterator<list<int> >,
        int, IntSequence&>(back_inserter(coll),
        4, seq);

    PRINT_ELEMENTS(coll);

    generate_n(back_inserter(coll), 4, IntSequence(42));
    PRINT_ELEMENTS(coll);

    generate_n(back_inserter(coll), 4, seq);
    PRINT_ELEMENTS(coll);

    generate_n(back_inserter(coll), 4, seq);
    PRINT_ELEMENTS(coll);
}
/*-----------------foreach----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

class MeanValue
{
    private:
        long num;
        long sum;
    public:
        MeanValue() : num(0), sum(0) {}
        void operator() (int elem)
        {
            num++;
            sum += elem;
        }

        double value()
        {
            return static_cast<double>(sum) / static_cast<double>(num);
        }
};

int main()
{
    vector<int> coll;

    for(int i=1; i<=8; ++i)
        coll.push_back(i);

    MeanValue mv = for_each(coll.begin(), coll.end(), MeanValue());
    cout << "mean value: " << mv.value() << endl;
}
/*-----------------removeif----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

class Nth
{
    private:
        int nth;
        int count;
    public:
        Nth(int n) : nth(n), count(0){ }

        bool operator() (int)
        { return ++count == nth; }
};

int main()
{
    list<int> coll;

    for(int i=1; i<=9; ++i)
        coll.push_back(i);

    PRINT_ELEMENTS(coll, "coll:      ");

    list<int>::iterator pos;

    pos = remove_if(coll.begin(), coll.end(), Nth(3)), coll.erase(pos, coll.end());

    PRINT_ELEMENTS(coll, "nth removed: ");
}
/*-----------------compose2----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class OP1, class OP2, class OP3>
class compose_f_gx_hx_t
: public std::unary_function<typename OP2::argument_type,
                             typename OP1::result_type>
{
    private:
        OP1 op1;
        OP2 op2;
        OP3 op3;
    public:
        compose_f_gx_hx_t(const OP1& o1, const OP2& o2, const OP3& o3)
            : op1(o1), op2(o2), op3(o3)
        { }

        typename OP1::result_type
        operator()(const typename OP2::argument_type& x) const
        { return op1(op2(x), op3(x)); }

};

template <class OP1, class OP2, class OP3>
inline compose_f_gx_hx_t<OP1, OP2, OP3>
compose_f_gx_hx(const OP1& o1, const OP2& o2, const OP3& o3)
{
    return compose_f_gx_hx_t<OP1, OP2, OP3>(o1, o2, o3);
}

int main()
{
    vector<int> coll;

    for(int i=1; i<=9; ++i)
        coll.push_back(i);

    PRINT_ELEMENTS(coll);

    vector<int>::iterator pos;
    pos = remove_if(coll.begin(), coll.end(),
            compose_f_gx_hx(logical_and<bool>(),
                            bind2nd(greater<int>(), 4),
                            bind2nd(less<int>(), 7)));
    coll.erase(pos, coll.end());

    PRINT_ELEMENTS(coll);
}
/*-----------------fopow----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <cmath>
using namespace std;

template <class T1, class T2>
struct  fopow : public binary_function<T1, T2, T1>
{
    T1 operator()(T1 base, T2 exp) const
    { return pow(base, exp); }
};

int main()
{
    vector<int> coll;

    for(int i=1; i<=9; ++i)
        coll.push_back(i);

    transform(coll.begin(), coll.end(),
              ostream_iterator<float>(cout, " "),
              bind1st(fopow<float, int>(), 3));
    cout << endl;

    transform(coll.begin(), coll.end(),
              ostream_iterator<float>(cout, " "),
              bind2nd(fopow<float, int>(), 3));
    cout << endl;
}
/*-----------------compose11----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class OP1, class OP2>
class compose_f_gx_t
: public std::unary_function<typename OP2::argument_type,
                             typename OP1::result_type>
{
    private:
        OP1 op1;
        OP2 op2;
    public:
        compose_f_gx_t(const OP1& o1, const OP2& o2)
            : op1(o1), op2(o2)
        { }

        typename OP1::result_type
        operator() (const typename OP2::argument_type& x) const
        { return op1 (op2(x)); }

};

template <class OP1, class OP2>
inline compose_f_gx_t<OP1, OP2>
compose_f_gx(const OP1& o1, const OP2& o2)
{
    return compose_f_gx_t<OP1, OP2>(o1, o2);
}

int main()
{
    vector<int> coll;

    for(int i=1; i<=9; ++i)
        coll.push_back(i);
    PRINT_ELEMENTS(coll);

    transform(coll.begin(), coll.end(),
              ostream_iterator<int>(cout, " "),
              compose_f_gx(bind2nd(multiplies<int>(), 5), bind2nd(plus<int>(), 10)));
    cout << endl;
}
/*-----------------compose22----------------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class OP1, class OP2, class OP3>
class compose_f_gx_hy_t
: public std::binary_function<typename OP2::argument_type,
                              typename OP3::argument_type,
                              typename OP1::result_type>
{
    private:
        OP1 op1;
        OP2 op2;
        OP3 op3;
    public:
        compose_f_gx_hy_t(const OP1& o1, const OP2& o2, const OP3& o3)
            : op1(o1), op2(o2), op3(o3)
        { }

        typename OP1::result_type
        operator() (const typename OP2::argument_type& x,
                    const typename OP3::argument_type& y) const
        { return op1 (op2(x), op3(y)); }

};

template <class OP1, class OP2, class OP3>
inline compose_f_gx_hy_t<OP1, OP2, OP3>
compose_f_gx_hy(const OP1& o1, const OP2& o2, const OP3& o3)
{
    return compose_f_gx_hy_t<OP1, OP2, OP3>(o1, o2, o3);
}

int main()
{
    string s("Internationalization");
    string sub("Nation");

    string::iterator pos;
    pos = search(s.begin(), s.end(),
                 sub.begin(), sub.end(),
                 compose_f_gx_hy(equal_to<int>(),
                                 ptr_fun(::toupper),
                                 ptr_fun(::toupper)));
    if(pos != s.end())
        cout << "\"" << sub << "\" is part of \"" << s << "\""
             << endl;
}
/*-----------------algostuff1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

void print(int elem)
{
    cout << elem << ' ';
}

int main()
{
    vector<int> coll;

    INSERT_ELEMENTS(coll, 1, 9);

    for_each(coll.begin(), coll.end(),
             print);
    cout << endl;
}
/*-----------------algostuff2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

template <class T>
class AddValue
{
    private:
        T theValue;
    public:
        AddValue(const T& v) : theValue(v) {}

        void operator() (T& elem) const
        { elem += theValue; }
};

int main()
{
    vector<int> coll;

    INSERT_ELEMENTS(coll, 1, 9);

    for_each(coll.begin(), coll.end(), AddValue<int>(10));

    PRINT_ELEMENTS(coll);

    for_each(coll.begin(), coll.end(), AddValue<int>(*coll.begin()));
    
    PRINT_ELEMENTS(coll);
}
/*-----------------algostuff3-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

class MeanValue
{
    private:
        long num;
        long sum;
    public:
        MeanValue() : num(0), sum(0) {}
        void operator()(int elem)
        {
            num++;
            sum += elem;
        }
        operator double()
        {
            return static_cast<double> (sum) / static_cast<double>(num);
        }
        
};

int main()
{
    vector<int> coll;

    INSERT_ELEMENTS(coll, 1, 8);

    double mv = for_each(coll.begin(), coll.end(), MeanValue());
    cout << "mean value: " << mv << endl;
}
/*-----------------algostuff4-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

bool isEven(int elem)
{
    return (elem%2 == 0);
}

int main()
{
    vector<int> coll;
    int num;

    INSERT_ELEMENTS(coll, 1, 9);
    PRINT_ELEMENTS(coll, "coll: ");

    num = count(coll.begin(), coll.end(), 4);
    cout << "number of elements equal to 4:       "
         << num << endl;

    num = count_if(coll.begin(), coll.end(), isEven);
    cout << "number of elements with even value:  "
         << num << endl;

    num = count_if(coll.begin(), coll.end(),
                   bind2nd(greater<int>(), 4));
    cout << "number of elements greater than 4:   "
         << num << endl;
}
/*-----------------minmax-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

bool absLess(int elem1, int elem2)
{
    return abs(elem1) < abs(elem2);
}

int main()
{
    deque<int> coll;

    INSERT_ELEMENTS(coll, 2, 8);
    INSERT_ELEMENTS(coll, -3, 5);

    PRINT_ELEMENTS(coll);

    cout << "minimum: "
         << *min_element(coll.begin(), coll.end())
         << endl;
    cout << "maximum: "
         << *max_element(coll.begin(), coll.end())
         << endl;
    cout << "minimum: "
         << *min_element(coll.begin(), coll.end(), absLess)
         << endl;
    cout << "maximum: "
         << *max_element(coll.begin(), coll.end(),absLess)
         << endl;
}
/*-----------------find-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    list<int> coll;

    INSERT_ELEMENTS(coll, 1, 9);
    INSERT_ELEMENTS(coll, 1, 9);

    PRINT_ELEMENTS(coll, "coll: ");

    list<int>::iterator pos1;
    pos1 = find(coll.begin(), coll.end(), 4);

    list<int>::iterator pos2;
    if(pos1 != coll.end())
        pos2 = find(++pos1, coll.end(), 4);

    if(pos1 != coll.end() && pos2 != coll.end())
    {
        copy(--pos1, ++pos2, ostream_iterator<int>(cout, " "));
        cout << endl;
    }
}
/*-----------------find2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    vector<int> coll;
    vector<int>::iterator pos;

    INSERT_ELEMENTS(coll, 1, 9);

    PRINT_ELEMENTS(coll, "coll: ");

    pos = find_if(coll.begin(), coll.end(),
            bind2nd(greater<int>(), 3));

    cout << "the "
         << distance(coll.begin(), pos) + 1
         << ". element is the first greater than 3" << endl;

    pos = find_if(coll.begin(), coll.end(),
            not1(bind2nd(modulus<int>(), 3)));

    cout << "the "
         << distance(coll.begin(), pos) + 1
         << ". element is the first divisible by 3" << endl;
}
/*-----------------searchn1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    deque<int> coll;

    INSERT_ELEMENTS(coll, 1, 9);
    PRINT_ELEMENTS(coll);

    deque<int>::iterator pos;
    pos = search_n(coll.begin(), coll.end(), 4, 3);

    if(pos != coll.end())
        cout << "four consecutive elements with value 3 "
             << "start with " << distance(coll.begin(), pos) + 1
             << ". element" << endl;
    else
        cout << "no four consecutive elements with value 3 found"
             << endl;

    pos = search_n(coll.begin(), coll.end(), 4, 3, greater<int>());
    if(pos != coll.end())
        cout << "four consecutive elements with value > 3 "
             << "start with " << distance(coll.begin(), pos) + 1
             << ". element" << endl;
    else
        cout << "no four consecutive elements with value > 3 found"
             << endl;
}
/*-----------------search1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    deque<int> coll;
    list<int> subcoll;

    INSERT_ELEMENTS(coll, 1, 7);
    INSERT_ELEMENTS(coll, 1, 7);

    INSERT_ELEMENTS(subcoll, 3, 6);

    PRINT_ELEMENTS(coll, "coll: ");
    PRINT_ELEMENTS(subcoll, "subcoll: ");

    deque<int>::iterator pos;
    pos = search(coll.begin(), coll.end(),
                 subcoll.begin(), subcoll.end());

    while(pos != coll.end())
    {
        cout << "subcoll found starting with element "
             << distance(coll.begin(), pos) + 1
             << endl;
        ++pos;
        pos = search(pos, coll.end(),
                     subcoll.begin(), subcoll.end());
    }
}
/*-----------------findend1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    deque<int> coll;
    list<int> subcoll;

    INSERT_ELEMENTS(coll, 1, 7);
    INSERT_ELEMENTS(coll, 1, 7);

    INSERT_ELEMENTS(subcoll, 3, 6);

    PRINT_ELEMENTS(coll, "coll: ");
    PRINT_ELEMENTS(subcoll, "subcoll: ");

    deque<int>::iterator pos;
    pos = find_end(coll.begin(), coll.end(),
                   subcoll.begin(), subcoll.end());

    deque<int>::iterator end(coll.end());
    while(pos != end)
    {
        cout << "subcoll found starting with element "
             << distance(coll.begin(), pos) + 1
             << endl;
        end = pos;
        pos = find_end(coll.begin(), end,
                       subcoll.begin(), subcoll.end());

    }
}
/*-----------------findfirstof1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    vector<int> coll;
    list<int> searchcoll;

    INSERT_ELEMENTS(coll, 1, 11);
    INSERT_ELEMENTS(searchcoll, 3, 5);

    PRINT_ELEMENTS(coll,       "coll:       ");
    PRINT_ELEMENTS(searchcoll, "searchcoll: ");

    vector<int>::iterator pos;
    pos = find_first_of(coll.begin(), coll.end(),
                        searchcoll.begin(),
                        searchcoll.end());
    cout << "first element of searchcoll in coll is element "
         << distance(coll.begin(), pos) + 1
         << endl;

    vector<int>::reverse_iterator rpos;
    rpos = find_first_of(coll.rbegin(), coll.rend(),
                         searchcoll.begin(),
                         searchcoll.end());
    cout << "last  element of searchcoll in coll is element "
         << distance(coll.begin(), rpos.base())
         << endl;
}
/*-----------------adjfind1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

bool doubled(int elem1, int elem2)
{
    return elem1 * 2 == elem2;
}

int main()
{
    vector<int> coll;

    coll.push_back(1);
    coll.push_back(3);
    coll.push_back(2);
    coll.push_back(4);
    coll.push_back(5);
    coll.push_back(5);
    coll.push_back(0);

    PRINT_ELEMENTS(coll, "coll: ");

    vector<int>::iterator pos;
    pos = adjacent_find(coll.begin(), coll.end());

    if(pos != coll.end())
        cout << "first two elements with equal value have position "
             << distance(coll.begin(), pos) + 1
             << endl;

    pos = adjacent_find(coll.begin(), coll.end(), doubled);
    if(pos != coll.end())
        cout << "first two elements with second value twice the "
             << "first have pos. "
             << distance(coll.begin(), pos) + 1
             << endl;

}
/*-----------------equal1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

bool bothEvenOrOdd(int elem1, int elem2)
{
    return elem1%2 == elem2 % 2;
}

int main()
{
    vector<int> coll1;
    list<int> coll2;

    INSERT_ELEMENTS(coll1, 1, 7);
    INSERT_ELEMENTS(coll2, 3, 9);

    PRINT_ELEMENTS(coll1, "coll1: ");
    PRINT_ELEMENTS(coll2, "coll2: ");

    if(equal(coll1.begin(), coll1.end(),
             coll2.begin()))
        cout << "coll1 == coll2" << endl;
    else
        cout << "coll1 != coll2" << endl;

    if(equal(coll1.begin(), coll1.end(),
             coll2.begin(),
             bothEvenOrOdd))
        cout << "even and odd elements correspond" << endl;
    else
        cout << "even and odd elements correspond" << endl;

}
/*-----------------mismatch1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

bool bothEvenOrOdd(int elem1, int elem2)
{
    return elem1%2 == elem2 % 2;
}

int main()
{
    vector<int> coll1;
    list<int> coll2;

    INSERT_ELEMENTS(coll1, 1, 6);

    for(int i = 1; i <= 16; i *= 2)
        coll2.push_back(i);
    coll2.push_back(3);

    PRINT_ELEMENTS(coll1, "coll1: ");
    PRINT_ELEMENTS(coll2, "coll2: ");

    pair<vector<int>::iterator, list<int>::iterator> values;
    values = mismatch(coll1.begin(), coll1.end(),
                      coll2.begin());
    if(values.first == coll1.end())
        cout << "no mismatch" << endl;
    else
        cout << "first mismatch: "
             << *values.first << " and "
             << *values.second << endl;

    values = mismatch(coll1.begin(), coll1.end(),
                      coll2.begin(),
                      less_equal<int>());
    if(values.first == coll1.end())
        cout << "always less-or-equal" << endl;
    else
        cout << "not less-or-equal: "
             << *values.first << " and "
             << *values.second << endl;
}
/*-----------------lexico1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

void printCollection(const list<int>& l)
{
    PRINT_ELEMENTS(l);
}

bool lessForCollection(const list<int>& l1, const list<int>& l2)
{
    return lexicographical_compare
        (l1.begin(), l1.end(),
         l2.begin(), l2.end());
}

int main()
{
    list<int> c1, c2, c3, c4;

    INSERT_ELEMENTS(c1, 1, 5);
    c4 = c3 = c2 = c1;

    c1.push_back(7);
    c1.push_back(2);
    c1.push_back(0);
    c1.push_back(2);

    vector<list<int> > cc;

    cc.push_back(c1);
    cc.push_back(c2);
    cc.push_back(c3);
    cc.push_back(c4);
    cc.push_back(c3);
    cc.push_back(c1);
    cc.push_back(c4);
    cc.push_back(c2);

    for_each(cc.begin(), cc.end(),
             printCollection);
    cout << endl;

    sort(cc.begin(), cc.end(),
         lessForCollection);

    for_each(cc.begin(), cc.end(),
             printCollection);
}
/*-----------------copy1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    vector<int> coll1;
    list<int> coll2;

    INSERT_ELEMENTS(coll1, 1, 9);

    copy(coll1.begin(), coll1.end(),
         back_inserter(coll2));

    copy(coll2.begin(), coll2.end(),
         ostream_iterator<int>(cout, " "));
    cout << endl;

    copy(coll1.rbegin(), coll1.rend(),
         coll2.begin());

    copy(coll2.begin(), coll2.end(),
         ostream_iterator<int>(cout, " "));
    cout << endl;
}
/*-----------------copy2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    vector<char> source(10, '.');
    for(int c='a'; c<='f'; c++)
        source.push_back(c);
    source.insert(source.end(), 10, '.');
    PRINT_ELEMENTS(source, "source: ");

    vector<char> c1(source.begin(), source.end());
    copy(c1.begin()+10, c1.begin()+16,
         c1.begin()+7);
    PRINT_ELEMENTS(c1, "c1: ");

    vector<char> c2(source.begin(), source.end());
    copy_backward(c2.begin()+10, c2.begin()+16,
                  c2.begin()+19);
    PRINT_ELEMENTS(c2, "c2: ");

}
/*-----------------copy3-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;


int main()
{
    copy(istream_iterator<string>(cin),
         istream_iterator<string>(),
         ostream_iterator<string>(cout, "\n"));
}
/*-----------------transf1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    vector<int> coll1;
    list<int> coll2;

    INSERT_ELEMENTS(coll1, 1, 9);
    PRINT_ELEMENTS(coll1, "coll1: ");

    transform(coll1.begin(), coll1.end(),
              coll1.begin(),
              negate<int>());
    PRINT_ELEMENTS(coll1, "negated: ");
    
    transform(coll1.begin(), coll1.end(),
              back_inserter(coll2),
              bind2nd(multiplies<int>(), 10));
    PRINT_ELEMENTS(coll2, "coll2: ");
    
    transform(coll2.rbegin(), coll2.rend(),
              ostream_iterator<int>(cout, " "),
              negate<int>());
    cout << endl;
}
/*-----------------transf2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    vector<int> coll1;
    list<int> coll2;

    INSERT_ELEMENTS(coll1, 1, 9);
    PRINT_ELEMENTS(coll1, "coll1: ");

    transform(coll1.begin(), coll1.end(),
              coll1.begin(),
              coll1.begin(),
              multiplies<int>());
    PRINT_ELEMENTS(coll1, "squared: ");
    
    transform(coll1.begin(), coll1.end(),
              coll1.rbegin(),
              back_inserter(coll2),
              plus<int>());
    PRINT_ELEMENTS(coll2, "coll2: ");

    cout << "diff: ";
    transform(coll1.begin(), coll1.end(),
              coll2.begin(),
              ostream_iterator<int>(cout, " "),
              minus<int>());
    cout << endl;
}
/*-----------------swap1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    vector<int> coll1;
    deque<int> coll2;

    INSERT_ELEMENTS(coll1, 1, 9);
    INSERT_ELEMENTS(coll2, 11, 23);

    PRINT_ELEMENTS(coll1, "coll1: ");
    PRINT_ELEMENTS(coll2, "coll2: ");

    deque<int>::iterator pos;
    pos = swap_ranges(coll1.begin(), coll1.end(),
                      coll2.begin());
    PRINT_ELEMENTS(coll1, "\ncoll1: ");
    PRINT_ELEMENTS(coll2, "coll2: ");
    
    if(pos != coll2.end())
        cout << "first element not modified: "
             << *pos << endl;

    swap_ranges(coll2.begin(), coll2.begin()+3,
                coll2.rbegin());
    PRINT_ELEMENTS(coll2, "\ncoll2: ");
}
/*-----------------fill1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    fill_n(ostream_iterator<float>(cout, " "), 10, 7.7);
    cout << endl;

    list<string> coll;

    fill_n(back_inserter(coll), 9, "hello");
    PRINT_ELEMENTS(coll, "coll: ");

    fill(coll.begin(), coll.end(), "again");
    PRINT_ELEMENTS(coll, "coll: ");

    fill_n(coll.begin(), coll.size()-2, "hi");
    PRINT_ELEMENTS(coll, "coll: ");

    list<string>::iterator pos1, pos2;
    pos1 = coll.begin();
    pos2 = coll.end();
    fill(++pos1, --pos2, "hmmm");
    PRINT_ELEMENTS(coll, "coll: ");

}
/*-----------------generate-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    list<int> coll;

    generate_n(back_inserter(coll), 5, rand);
    PRINT_ELEMENTS(coll);

    generate(coll.begin(), coll.end(), rand);
    PRINT_ELEMENTS(coll);
}
/*-----------------replace1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    list<int> coll;

    INSERT_ELEMENTS(coll, 2, 7);
    INSERT_ELEMENTS(coll, 4, 9);
    PRINT_ELEMENTS(coll, "coll: ");

    replace(coll.begin(), coll.end(), 6, 42);
    PRINT_ELEMENTS(coll, "coll: ");

    replace_if(coll.begin(), coll.end(),
               bind2nd(less<int>(), 5),
               0);
    PRINT_ELEMENTS(coll, "coll: ");

}
/*-----------------replace2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    list<int> coll;

    INSERT_ELEMENTS(coll, 2, 6);
    INSERT_ELEMENTS(coll, 4, 9);
    PRINT_ELEMENTS(coll);

    replace_copy(coll.begin(), coll.end(),
                 ostream_iterator<int>(cout, " "),
                 5,
                 55);
    cout << endl;

    replace_copy_if(coll.begin(), coll.end(),
                    ostream_iterator<int>(cout, " "),
                    bind2nd(less<int>(), 5),
                    42);
    cout << endl;

    replace_copy_if(coll.begin(), coll.end(),
                    ostream_iterator<int>(cout, " "),
                    bind2nd(modulus<int>(), 2),
                    0);
    cout << endl;
}
/*-----------------remove1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    vector<int> coll;

    INSERT_ELEMENTS(coll, 2, 6);
    INSERT_ELEMENTS(coll, 4, 9);
    INSERT_ELEMENTS(coll, 1, 7);
    PRINT_ELEMENTS(coll, "coll:                ");

    vector<int>::iterator pos;
    pos = remove(coll.begin(), coll.end(), 5);

    PRINT_ELEMENTS(coll, "size not changed:    ");

    coll.erase(pos, coll.end());
    PRINT_ELEMENTS(coll, "size changed:        ");

    coll.erase(remove_if(coll.begin(), coll.end(),
                         bind2nd(less<int>(), 4)),
                         coll.end());
    PRINT_ELEMENTS(coll, "<4 removed:  :       ");
}
/*-----------------remove2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    list<int> coll1;

    INSERT_ELEMENTS(coll1, 1, 6);
    INSERT_ELEMENTS(coll1, 1, 9);
    PRINT_ELEMENTS(coll1);

    remove_copy(coll1.begin(), coll1.end(),
                ostream_iterator<int>(cout, " "),
                3);
    cout << endl;

    remove_copy_if(coll1.begin(), coll1.end(),
                   ostream_iterator<int>(cout, " "),
                   bind2nd(greater<int>(), 4));
    cout << endl;

    multiset<int> coll2;
    remove_copy_if(coll1.begin(), coll1.end(),
                   inserter(coll2, coll2.end()),
                   bind2nd(less<int>(), 4));
    PRINT_ELEMENTS(coll2);
}
/*-----------------unique1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    int source[] = { 1, 4, 4, 6, 1, 2, 2, 3, 1, 6, 6, 6, 5, 7, 5, 4, 4 };

    int sourceNum = sizeof(source)/sizeof(source[0]);

    list<int> coll;

    copy(source, source+sourceNum, back_inserter(coll));
    PRINT_ELEMENTS(coll);

    list<int>::iterator pos;

    pos = unique(coll.begin(), coll.end());

    copy(coll.begin(), pos, ostream_iterator<int>(cout, " "));
    cout << "\n\n";

    copy(source, source+sourceNum, coll.begin());
    PRINT_ELEMENTS(coll);

    coll.erase(unique(coll.begin(), coll.end(), greater<int>()),
               coll.end());
    PRINT_ELEMENTS(coll);
}
/*-----------------unique2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

bool differenceOne(int elem1, int elem2)
{
    return elem1 + 1 == elem2 || elem1 - 1 == elem2;
}

int main()
{
    int source[] = { 1, 4, 4, 6, 1, 2, 2, 3, 1, 6, 6, 6, 5, 7, 5, 4, 4 };
    int sourceNum = sizeof(source)/sizeof(source[0]);

    list<int> coll;
    copy(source, source+sourceNum, back_inserter(coll));
    PRINT_ELEMENTS(coll);

    unique_copy(coll.begin(), coll.end(),
                ostream_iterator<int>(cout, " "));
    cout << endl;

    unique_copy(coll.begin(), coll.end(),
                ostream_iterator<int>(cout, " "),
                differenceOne);
    cout << endl;
}
/*-----------------unique3-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

bool bothSpaces(char elem1, char elem2)
{
    return elem1 == ' ' && elem2 == ' ';
}

int main()
{
    cin.unsetf(ios::skipws);
    unique_copy(istream_iterator<char>(cin),
                istream_iterator<char>(),
                ostream_iterator<char>(cout),
                bothSpaces);
}
/*-----------------reverse1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    vector<int> coll;

    INSERT_ELEMENTS(coll, 1, 9);
    PRINT_ELEMENTS(coll, "coll: ");

    reverse(coll.begin()+1, coll.end()-1);
    PRINT_ELEMENTS(coll, "coll: ");

    reverse_copy(coll.begin(), coll.end(),
                 ostream_iterator<int>(cout, " "));
    cout << endl;
}
/*-----------------rotate1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    vector<int> coll;

    INSERT_ELEMENTS(coll, 1, 9);
    PRINT_ELEMENTS(coll, "one left: ");

    rotate(coll.begin(),
           coll.end() - 2,
           coll.end());
    PRINT_ELEMENTS(coll, "two right: ");

    rotate(coll.begin(),
           find(coll.begin(), coll.end(), 4),
           coll.end());
    PRINT_ELEMENTS(coll, "4 first: ");
}
/*-----------------rotate2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    set<int> coll;

    INSERT_ELEMENTS(coll, 1, 9);
    PRINT_ELEMENTS(coll);

    set<int>::iterator pos = coll.begin();
    advance(pos, 1);

    rotate_copy(coll.begin(), pos,
                coll.end(),
                ostream_iterator<int>(cout, " "));
    cout << endl;

    pos = coll.end();
    advance(pos, -2);
    rotate_copy(coll.begin(), pos,
                coll.end(),
                ostream_iterator<int>(cout, " "));
    cout << endl;

    rotate_copy(coll.begin(), coll.find(4),
                coll.end(),
                ostream_iterator<int>(cout, " "));
    cout << endl;
}
/*-----------------perm1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    vector<int> coll;

    INSERT_ELEMENTS(coll, 1, 3);
    PRINT_ELEMENTS(coll, "on entry: ");

    while(next_permutation(coll.begin(), coll.end()))
            PRINT_ELEMENTS(coll, " ");
    PRINT_ELEMENTS(coll, "afterward: ");

    while(prev_permutation(coll.begin(), coll.end()))
        PRINT_ELEMENTS(coll, " ");
    PRINT_ELEMENTS(coll, "now: ");

    while(prev_permutation(coll.begin(), coll.end()))
            PRINT_ELEMENTS(coll, " ");
    PRINT_ELEMENTS(coll, "afterward: ");

}
/*-----------------random1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

class MyRandom
{
    public:
        ptrdiff_t operator() (ptrdiff_t max)
        {
            double tmp;
            tmp = static_cast<double>(rand())
                / static_cast<double>(RAND_MAX);
            return static_cast<ptrdiff_t>(tmp *max);
        }
};

int main()
{
    vector<int> coll;

    INSERT_ELEMENTS(coll, 1, 9);
    PRINT_ELEMENTS(coll, "coll: ");

    random_shuffle(coll.begin(), coll.end());

    PRINT_ELEMENTS(coll, "shuffled: ");

    sort(coll.begin(), coll.end());
    PRINT_ELEMENTS(coll, "sorted: ");

    MyRandom rd;
    random_shuffle(coll.begin(), coll.end(),
                   rd);
    PRINT_ELEMENTS(coll, "shuffled: ");
}
/*-----------------part1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    vector<int> coll1;
    vector<int> coll2;

    INSERT_ELEMENTS(coll1, 1, 9);
    INSERT_ELEMENTS(coll2, 1, 9);
    PRINT_ELEMENTS(coll1, "coll1: ");
    PRINT_ELEMENTS(coll2, "coll2: ");
    cout << endl;

    vector<int>::iterator pos1, pos2;
    pos1 = partition(coll1.begin(), coll1.end(),
                     not1(bind2nd(modulus<int>(), 2)));
    pos2 = stable_partition(coll2.begin(), coll2.end(),
                            not1(bind2nd(modulus<int>(), 2)));

    PRINT_ELEMENTS(coll1, "coll1: ");
    cout << "first odd element: " << *pos1 << endl;
    PRINT_ELEMENTS(coll2, "coll2: ");
    cout << "first odd element: " << *pos2 << endl;
}
/*-----------------sort1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    deque<int> coll;

    INSERT_ELEMENTS(coll, 1, 9);
    INSERT_ELEMENTS(coll, 1, 9);

    PRINT_ELEMENTS(coll, "on entry: ");

    sort(coll.begin(), coll.end());

    PRINT_ELEMENTS(coll, "sorted: ");

    sort(coll.begin(), coll.end(), greater<int>());
    PRINT_ELEMENTS(coll, "sort >: ");
}
/*-----------------sort2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

bool lessLength(const string& s1, const string& s2)
{
    return s1.length() < s2.length();
}

int main()
{
    vector<string> coll1;
    vector<string> coll2;

    coll1.push_back("1xxx");
    coll1.push_back("2x");
    coll1.push_back("3x");
    coll1.push_back("4x");
    coll1.push_back("5xx");
    coll1.push_back("6xxxx");
    coll1.push_back("7xx");
    coll1.push_back("8xxx");
    coll1.push_back("9xx");

    coll1.push_back("10xxx");
    coll1.push_back("11");
    coll1.push_back("12");
    coll1.push_back("13");
    coll1.push_back("14xx");
    coll1.push_back("15");
    coll1.push_back("16");
    coll1.push_back("17");
    coll2 = coll1;

    PRINT_ELEMENTS(coll1, "on entry:\n");

    sort(coll1.begin(), coll1.end(),
         lessLength);
    stable_sort(coll2.begin(), coll2.end(),
                lessLength);
    PRINT_ELEMENTS(coll1, "\nwith sort():\n ");
    PRINT_ELEMENTS(coll2, "\nwith stable_sort():\n ");
}
/*-----------------psort1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    deque<int> coll;

    INSERT_ELEMENTS(coll, 3, 7);
    INSERT_ELEMENTS(coll, 2, 6);
    INSERT_ELEMENTS(coll, 1, 5);
    PRINT_ELEMENTS(coll);

    partial_sort(coll.begin(),
                 coll.begin()+5,
                 coll.end(),
                 greater<int>());
    PRINT_ELEMENTS(coll);

    partial_sort(coll.begin(),
                 coll.end(),
                 coll.end());
    PRINT_ELEMENTS(coll);
}
/*-----------------psort2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    deque<int> coll1;
    vector<int> coll6(6);
    vector<int> coll30(30);

    INSERT_ELEMENTS(coll1, 3, 7);
    INSERT_ELEMENTS(coll1, 2, 6);

    INSERT_ELEMENTS(coll1, 1, 5);
    PRINT_ELEMENTS(coll1);

    vector<int>::iterator pos6;
    pos6 = partial_sort_copy(coll1.begin(), coll1.end(),
                             coll6.begin(), coll6.end());

    copy(coll6.begin(), pos6,
         ostream_iterator<int>(cout, " "));
    cout << endl;

    vector<int>::iterator pos30;
    pos30 = partial_sort_copy(coll1.begin(), coll1.end(),
                              coll30.begin(), coll30.end(),
                              greater<int>());
    copy(coll30.begin(), pos30,
         ostream_iterator<int>(cout, " "));
    cout << endl;
}
/*-----------------nth2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    deque<int> coll;

    INSERT_ELEMENTS(coll, 3, 7);
    INSERT_ELEMENTS(coll, 2, 6);
    INSERT_ELEMENTS(coll, 1, 5);
    PRINT_ELEMENTS(coll);

    nth_element(coll.begin(),
                coll.begin()+3,
                coll.end());
    cout << "the four lowest elements are: ";
    copy(coll.begin(), coll.begin()+4,
         ostream_iterator<int>(cout, " "));
    cout << endl;

    nth_element(coll.begin(),
                coll.end()-4,
                coll.end());
    cout << "the four hignest elements are: ";
    copy(coll.end()-4, coll.end(),
         ostream_iterator<int>(cout, " "));
    cout << endl;

    nth_element(coll.begin(),
                coll.begin()+3,
                coll.end(),
                greater<int>());
    cout << "the four highest elements are: ";
    copy(coll.begin(), coll.begin()+4,
         ostream_iterator<int>(cout, " "));
    cout << endl;
}
/*-----------------heap2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    vector<int> coll;

    INSERT_ELEMENTS(coll, 3, 7);
    INSERT_ELEMENTS(coll, 5, 9);
    INSERT_ELEMENTS(coll, 1, 4);

    PRINT_ELEMENTS(coll, "on entry:    ");

    make_heap(coll.begin(), coll.end());

    PRINT_ELEMENTS(coll, "after make_heap(): ");

    pop_heap(coll.begin(), coll.end());
    coll.pop_back();

    PRINT_ELEMENTS(coll, "after pop_heap():  ");

    coll.push_back(17);
    push_heap(coll.begin(), coll.end());

    PRINT_ELEMENTS(coll, "after push_heap");

    sort_heap(coll.begin(), coll.end());

    PRINT_ELEMENTS(coll, "after sort_heap(): ");
}
/*-----------------bsearch1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    list<int> coll;

    INSERT_ELEMENTS(coll, 1, 9);
    PRINT_ELEMENTS(coll);

    if(binary_search(coll.begin(), coll.end(), 5))
        cout << "5 is present" << endl;
    else
        cout << "5 is not present" << endl;

    if(binary_search(coll.begin(), coll.end(), 42))
        cout << "42 is present" << endl;
    else
        cout << "42 is not present" << endl;
}
/*-----------------bounds1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    list<int> coll;

    INSERT_ELEMENTS(coll, 1, 9);
    INSERT_ELEMENTS(coll, 1, 9);
    coll.sort();
    PRINT_ELEMENTS(coll);

    list<int>::iterator pos1, pos2;
    pos1 = lower_bound(coll.begin(), coll.end(), 5);
    pos2 = upper_bound(coll.begin(), coll.end(), 5);

    cout << "5 could get position "
         << distance(coll.begin(), pos1) + 1
         << " up to "
         << distance(coll.begin(), pos2) + 1
         << " without breaking the sorting" << endl;

    coll.insert(lower_bound(coll.begin(), coll.end(), 3), 3);
    coll.insert(upper_bound(coll.begin(), coll.end(), 7), 7);

    PRINT_ELEMENTS(coll);
}
/*-----------------eqrange1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    list<int> coll;

    INSERT_ELEMENTS(coll, 1, 9);
    INSERT_ELEMENTS(coll, 1, 9);
    coll.sort();
    PRINT_ELEMENTS(coll);

    pair<list<int>::iterator, list<int>::iterator> range;
    range = equal_range(coll.begin(), coll.end(), 5);

    cout << "5 could get position "
         << distance(coll.begin(), range.first) + 1
         <<" up to "
         <<distance(coll.begin(), range.second) + 1
         << " without breaking the sorting" << endl;
}
/*-----------------merge1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    list<int> coll1;
    set<int> coll2;

    INSERT_ELEMENTS(coll1, 1, 6);
    INSERT_ELEMENTS(coll2, 3, 8);

    PRINT_ELEMENTS(coll1, "coll1: ");
    PRINT_ELEMENTS(coll2, "coll2: ");

    cout << "merged: ";
    merge(coll1.begin(), coll1.end(),
          coll2.begin(), coll2.end(),
          ostream_iterator<int>(cout, " "));
    cout << endl;
}
/*-----------------setalgos1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    int c1[] = { 1, 2, 2, 4, 6, 7, 7, 9 };
    int num1 = sizeof(c1) / sizeof(int);

    int c2[] = { 2, 2, 2, 3, 6, 6, 8, 9 };
    int num2 = sizeof(c2) / sizeof(int);

    cout << "c1:             ";
    copy(c1, c1+num1,
         ostream_iterator<int>(cout, " "));
    cout << endl;

    cout << "c2:             ";
    copy(c2, c2+num2,
         ostream_iterator<int>(cout, " "));
    cout << "\n" << endl;

    cout << "merge():        ";
    merge(c1, c1+num1,
          c2, c2+num2,
          ostream_iterator<int>(cout, " "));
    cout << endl;

    cout << "set_union():    ";
    set_union(c1, c1+num1,
              c2, c2+num2,
              ostream_iterator<int>(cout, " "));
    cout << endl;

    cout << "set_intersection():  ";
    set_intersection(c1, c1+num1,
                     c2, c2+num2,
                     ostream_iterator<int>(cout, " "));
    cout << endl;

    cout << "set_difference(): ";
    set_difference(c1, c1+num1,
                   c2, c2+num2,
                   ostream_iterator<int>(cout, " "));
    cout << endl;

    cout << "set_symmetric_difference(): ";
    set_symmetric_difference(c1, c1+num1,
                             c2, c2+num2,
                             ostream_iterator<int>(cout, " "));
    cout << endl;
}
/*-----------------imerge1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    list<int> coll;

    INSERT_ELEMENTS(coll, 1, 7);
    INSERT_ELEMENTS(coll, 1, 8);
    PRINT_ELEMENTS(coll);

    list<int>::iterator pos;
    pos = find(coll.begin(), coll.end(), 7);
    ++pos;

    inplace_merge(coll.begin(), pos, coll.end());
    PRINT_ELEMENTS(coll);
}
/*-----------------accu1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    vector<int> coll;

    INSERT_ELEMENTS(coll, 1, 9);
    PRINT_ELEMENTS(coll);

    cout << "sum: "
         << accumulate(coll.begin(), coll.end(), 0)
         << endl;

    cout << "sum: "
         << accumulate(coll.begin(), coll.end(), -100)
         << endl;

    cout << "product: "
         << accumulate(coll.begin(), coll.end(), 1,
                       multiplies<int>())
         << endl;

    cout << "product: "
         << accumulate(coll.begin(), coll.end(), 0,
                       multiplies<int>())
         << endl;
}
/*-----------------inner1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    list<int> coll;

    INSERT_ELEMENTS(coll, 1, 6);
    PRINT_ELEMENTS(coll);

    cout << "inner productor: "
         << inner_product(coll.begin(), coll.end(),
                            coll.begin(), 0)
         << endl;

    cout << "inner reverse productor: "
         << inner_product(coll.begin(), coll.end(),
                            coll.rbegin(), 0)
         << endl;

    cout << "productor of sums: "
         << inner_product(coll.begin(), coll.end(),
                            coll.begin(), 1,
                            multiplies<int>(),
                            plus<int>())
         << endl;
}
/*-----------------partsum1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    vector<int> coll;

    INSERT_ELEMENTS(coll,1, 6);
    PRINT_ELEMENTS(coll);

    partial_sum(coll.begin(), coll.end(),
                ostream_iterator<int>(cout, " "));
    cout << endl;

    partial_sum(coll.begin(), coll.end(),
                ostream_iterator<int>(cout, " "),
                multiplies<int>());
    cout << endl;
}
/*-----------------adjdiff1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    deque<int> coll;

    INSERT_ELEMENTS(coll, 1, 6);
    PRINT_ELEMENTS(coll);

    adjacent_difference(coll.begin(), coll.end(),
                        ostream_iterator<int>(cout, " "));
    cout << endl;

    adjacent_difference(coll.begin(), coll.end(),
                        ostream_iterator<int>(cout, " "),
                        plus<int>());
    cout << endl;

    adjacent_difference(coll.begin(), coll.end(),
                        ostream_iterator<int>(cout, " "),
                        multiplies<int>());
    cout << endl;
}
/*-----------------relabs-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr="")
{
    typename T::const_iterator pos;

    cout << optcstr;
    for(pos = coll.begin(); pos != coll.end(); ++pos)
        cout << *pos << ' ';
    cout << endl;
}

template <class T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
    for(int i=first; i<=last; ++i)
        coll.insert(coll.end(), i);
}

int main()
{
    vector<int> coll;

    coll.push_back(17);
    coll.push_back(-3);
    coll.push_back(22);
    coll.push_back(13);
    coll.push_back(13);
    coll.push_back(-9);
    PRINT_ELEMENTS(coll, "coll: ");

    adjacent_difference(coll.begin(), coll.end(),
                        coll.begin());
    PRINT_ELEMENTS(coll, "relative: ");
    partial_sum(coll.begin(), coll.end(),
                coll.begin());

    PRINT_ELEMENTS(coll, "absolute: ");
}
/*-----------------stack1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
using namespace std;

int main()
{
    stack<int> st;

    st.push(1);
    st.push(2);
    st.push(3);

    cout << st.top() << ' ';
    st.pop();
    cout << st.top() << ' ';
    st.pop();
    
    st.top() = 77;

    st.push(4);
    st.push(5);

    st.pop();

    while(!st.empty())
    {
        cout << st.top() << ' ';
        st.pop();
    }
    cout << endl;
}
/*-----------------stack2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
#include <exception>
using namespace std;

template <class T>
class Stack
{
    protected:
        std::deque<T> c;

    public:
        class ReadEmptyStack : public std::exception
        {
            public:
                virtual const char* what() const throw()
                { return "read empty stack" ; }
        };

        typename std::deque<T>::size_type size() const
        { return c.size(); }

        bool empty() const
        { return c.empty(); }

        void push(const T& elem)
        { c.push_back(elem); }

        T pop()
        {
            if(c.empty())
                throw ReadEmptyStack();
            T elem(c.back());
            c.pop_back();
            return elem;
        }

        T& top()
        {
            if(c.empty())
                throw ReadEmptyStack();
            return c.back();
        }
};

int main()
{
    try
    {
        Stack<int> st;

        st.push(1);
        st.push(2);
        st.push(3);

        cout << st.pop() << ' ';
        cout << st.pop() << ' ';

        st.top() = 77;

        st.push(4);
        st.push(5);
        st.pop();

        cout << st.pop() << ' ';
        cout << st.pop() << endl;
        cout << st.pop() << endl;
    }
    catch(const exception& e)
    {
        cerr << "EXCEPTION: " << e.what() << endl;
    }
}
/*-----------------queue1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
#include <exception>
using namespace std;

int main()
{
    queue<string> q;

    q.push("These ");
    q.push("are ");
    q.push("more than ");

    cout << q.front();
    q.pop();
    cout << q.front();
    q.pop();

    q.push("four ");
    q.push("words!");

    q.pop();

    cout << q.front();
    q.pop();
    cout << q.front() << endl;
    q.pop();

    cout << "number of elements in the queue: " << q.size()
         << endl;
}
/*-----------------queue2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
#include <exception>
using namespace std;

template <class T>
class Queue
{
    protected:
        std::deque<T> c;

    public:
        class ReadEmptyQueue : public std::exception
        {
            public:
                virtual const char* what() const throw()
                { return "read empty queue"; }
        };

        typename std::deque<T>::size_type size() const
        { return c.size(); }

        bool empty() const
        { return c.empty(); }

        void push(const T& elem)
        { c.push_back(elem); }

        T pop()
        {
            if(c.empty())
                throw ReadEmptyQueue();
            T elem(c.front());
            c.pop_front();
            return elem;
        }

        T& front()
        {
            if(c.empty())
                throw ReadEmptyQueue();
            return c.front();
        }
};

int main()
{
    try
    {
        Queue<string> q;

        q.push("Three ");
        q.push("are ");
        q.push("more than ");

        cout << q.pop();
        cout << q.pop();

        q.push("four ");
        q.push("words!");

        q.pop();

        cout << q.pop();
        cout << q.pop() << endl;

        cout << "number of elements in the queue: " << q.size()
             << endl;

        cout << q.pop() << endl;
    }
    catch(const exception& e)
    {
        cerr << "EXCEPTION: " << e.what() << endl;
    }
}
/*-----------------pqueue1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
#include <exception>
using namespace std;

int main()
{
    priority_queue<float> q;

    q.push(66.6);
    q.push(22.2);
    q.push(44.4);

    cout << q.top() << ' ';
    q.pop();
    cout << q.top() << endl;
    q.pop();

    q.push(11.1);
    q.push(55.5);
    q.push(33.3);

    q.pop();

    while(!q.empty())
    {
        cout << q.top() << ' ';
        q.pop();
    }
    cout << endl;
}
/*-----------------btiset1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
#include <exception>
using namespace std;

int main()
{
    enum Color { red, yellow, green, blue, white, black, numColors };

    bitset<numColors> usedColors;

    usedColors.set(red);
    usedColors.set(blue);

    cout << "btifield of used colors: " << usedColors
         << endl;
    cout << "number of used colors: " << usedColors.count()
         << endl;
    cout << "bitfield of unused colors: " << ~usedColors
         << endl;

    if(usedColors.any())
    {
        for(int c = 0; c < numColors; ++c)
            if(usedColors[(Color)c])
                cout << "haha\n";
    }
}
/*-----------------string1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
#include <limits>
#include <exception>
using namespace std;

int main(int argc, char* argv[])
{
    string filename, basename, extname, tmpname;
    const string suffix("tmp");

    for(int i=1; i<argc; ++i)
    {
        filename = argv[i];
        string::size_type idx = filename.find('.');
        if(idx == string::npos)
            tmpname = filename + '.' + suffix;
        else
        {
            basename = filename.substr(0, idx);
            extname = filename.substr(idx+1);
            if(extname.empty())
            {
                tmpname = filename;
                tmpname += suffix;
            }
            else if(extname == suffix)
            {
                tmpname = filename;
                tmpname.replace(idx+1, extname.size(), "xxx");
            }
            else
            {
                tmpname = filename;
                tmpname.replace(idx+1, string::npos, suffix);
            }
        }
        cout << filename << " => " << tmpname << endl;
    }
}
/*-----------------string2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
#include <limits>
#include <exception>
using namespace std;

int main(int argc, char* argv[])
{
    const string delims(" \t,.;");
    string line;

    while(getline(cin, line))
    {
        string::size_type begIdx, endIdx;

        begIdx = line.find_first_not_of(delims);

        while(begIdx != string::npos)
        {
            endIdx = line.find_first_of(delims, begIdx);
            if(endIdx == string::npos)
                endIdx = line.length();

            for(int i=endIdx-1; i>=static_cast<int>(begIdx); --i)
                cout << line[i];
            cout << ' ';

            begIdx = line.find_first_not_of(delims, endIdx);
        }
        cout << endl;
    }
}
/*-----------------iter2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
#include <cctype>
#include <limits>
#include <exception>
using namespace std;

bool nocase_compare(char c1, char c2)
{
    return toupper(c1) == toupper(c2);
}

int main(int argc, char* argv[])
{
    string s1("This is a string");
    string s2("STRING");

    if(s1.size() == s2.size() &&
       equal(s1.begin(), s1.end(),
             s2.begin(),
             nocase_compare))
       cout << "the strings are equal" << endl;
    else
        cout << "the strings are not equal" << endl;

    string::iterator pos;
    pos = search(s1.begin(), s1.end(),
                 s2.begin(), s2.end(),
                 nocase_compare);
    if(pos == s1.end())
        cout << "s2 is not a substring of s1" << endl;
    else
        cout << '"' << s2 << "\" is a substring of \""
             << s1 << "\" (at index " << pos - s1.begin() << ")"
             << endl;

}
/*-----------------iter3-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
#include <cctype>
#include <limits>
#include <exception>
using namespace std;

int main(int argc, char* argv[])
{
    const string hello("Hello, how are you?");

    string s(hello.begin(), hello.end());

    string::iterator pos;
    for(pos = s.begin(); pos != s.end(); ++pos)
        cout << *pos;
    cout << endl;

    reverse(s.begin(), s.end());
    cout << "reverse:    " << s << endl;

    sort(s.begin(), s.end());
    cout << "ordered:    " << s << endl;

    s.erase(unique(s.begin(), s.end()), s.end());
    cout << "no dulplicates: " << s << endl;
}
/*-----------------unique-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <cctype>
#include <limits>
#include <exception>
using namespace std;

class bothWhiteSpaces
{
    private:
        const locale& loc;
    public:
        bothWhiteSpaces(const locale& l) : loc(l)
        { }
        bool operator() (char elem1, char elem2)
        { return isspace(elem1, loc) && isspace(elem2, loc); }
};

int main(int argc, char* argv[])
{
    string contents;

    cin.unsetf(ios::skipws);

    unique_copy(istream_iterator<char>(cin),
                istream_iterator<char>(),
                back_inserter(contents),
                bothWhiteSpaces(cin.getloc()));
    cout << contents;
}
/*-----------------icstring-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <cctype>
#include <limits>
#include <exception>
using namespace std;

struct ignorecase_traits : public std::char_traits<char>
{
    static bool eq(const char& c1, const char& c2)
    { return std::toupper(c1) == std::toupper(c2); }

    static bool It(const char& c1, const char& c2)
    { return std::toupper(c1)<std::toupper(c2); }

    static int compare(const char* s1, const char* s2, size_t n)
    {
        for(size_t i=0; i<n; ++i)
            if(!eq(s1[i], s2[i]))
                return It(s1[i], s2[i]) ? -1 : 1;
        return 0;
    }

    static const char* find(const char* s, size_t n,
                            const char& c)
    {
        for(size_t i=0; i<n; ++i)
            if(eq(s[i], c))
                return &(s[i]);
        return 0;
    }
};

typedef std::basic_string<char, ignorecase_traits> icstring;
std::ostream& operator << (std::ostream& strm, const icstring& s)
{ return strm << std::string(s.data(), s.length()); }

int main(int argc, char* argv[])
{
    using std::cout;
    using std::endl;

    icstring s1("hallo");
    icstring s2("otto");
    icstring s3("hALLo");

    cout << std::boolalpha;
    cout << s1 << " == " << s2 << " : " << (s1==s2) << endl;
    cout << s1 << " == " << s3 << " : " << (s1==s3) << endl;

    icstring::size_type idx = s1.find("All");
    if(idx != icstring::npos)
        cout << "index of \"All\" in \"" << s1 << "\": "
             << idx << endl;
    else
        cout << "\"All\" not found in \"" << s1 << endl;
}
/*-----------------complex1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
using namespace std;

int main(int argc, char* argv[])
{
    complex<double> c1(4.0, 3.0);
    complex<float> c2(polar(5.0, 0.75));

    cout << "c1: " << c1 << endl;
    cout << "c2: " << c2 << endl;

    cout << "c1: magnitude: " << abs(c1)
         << " (squared magnitude: " << norm(c1) << ") "
         << "phase angle: " << arg(c1) << endl;

    cout << "c1 conjugated: " << conj(c1) << endl;
    cout << "c2 conjugated: " << conj(c2) << endl;

    cout << "4.4 + c1 * 1.8: " << 4.4 + c1 * 1.9 << endl;
    cout << "c1 + c2:        "
         << c1 + complex<double>(c2.real(), c2.imag())
         << endl;

    cout << "c1 += sqrt(c1): " << (c1 += sqrt(c1)) << endl;
}
/*-----------------complex2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
using namespace std;

int main(int argc, char* argv[])
{
    complex<long double> c1, c2;

    while(cin.peek() != EOF)
    {
        cout << "complex number c1: ";
        cin >> c1;
        if( !cin )
        {
            cerr << "input error" << endl;
            return EXIT_FAILURE;
        }

        cout << "complex number c2: ";
        cin >> c2;
        if( !cin )
        {
            cerr << "input error" << endl;
            return EXIT_FAILURE;
        }

        if(c1 == c2)
            cout << "c1 and c2 are equal !" << endl;
        
        cout << "c2 raised to the c2: " << pow(c1, c2)
             << endl << endl;

        cin.ignore(numeric_limits<int>::max(), '\n');
    }
}
/*-----------------val1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
using namespace std;

template <class T>
void printValarray(const valarray<T>& va)
{
    for(int i=0; i<va.size(); i++)
        cout << va[i] << ' ';
    cout << endl;
}

int main(int argc, char* argv[])
{
    valarray<double> va1(10), va2(10);
    for(int i=0; i<10; i++)
        va1[i] = i * 1.1;
    va2 = -1;

    printValarray(va1);
    printValarray(va2);

    cout << "min(): " << va1.min() << endl;
    cout << "max(): " << va1.max() << endl;
    cout << "sum(): " << va1.sum() << endl;

    va2 = va1;

    va1.resize(0);

    printValarray(va1);
    printValarray(va2);
}
/*-----------------val2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
using namespace std;

template <class T>
void printValarray(const valarray<T>& va)
{
    for(int i=0; i<va.size(); i++)
        cout << va[i] << ' ';
    cout << endl;
}

int main(int argc, char* argv[])
{
    valarray<double> va(9);
    for(int i=0; i<va.size(); i++)
        va[i] = i* 1.1;
    printValarray(va);

    va *= 2.0;

    printValarray(va);

    valarray<double> vb(va+10.0);
    printValarray(vb);

    valarray<double> vc;
    vc = sqrt(va) + vb/2.0 - 1.0;
    printValarray(vc);
}
/*-----------------slice1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
using namespace std;

template <class T>
void printValarray(const valarray<T>& va, int num)
{
    for(int i=0; i<va.size()/num; ++i)
    {
        for(int j=0; j<num; ++j)
            cout << va[i*num+j] << ' ';
        cout << endl;
    }
    cout << endl;
}

int main(int argc, char* argv[])
{
    valarray<double> va(12);
    for(int i=0; i<12; i++)
        va[i] = i;

    printValarray(va, 3);

    va[slice(0, 4, 3)] = pow(valarray<double>(va[slice(1, 4, 3)]),
                             valarray<double>(va[slice(2, 4, 3)]));
    printValarray(va, 3);

    valarray<double> vb(va[slice(2, 4, 0)]);
    va[slice(2, 4, 3)] *= vb;
    printValarray(va, 3);

    va[slice(2,4,3)] = valarray<double>(va[slice(2,4,3)]) * 2.0;
    printValarray(va, 3);
}
/*-----------------gslice1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
using namespace std;

template <class T>
void printValarray3D(const valarray<T>& va, int dim1, int dim2)
{
    for(int i=0; i<va.size()/(dim1*dim2);++i)
    {
        for(int j=0; j<dim2; ++j)
        {
            for(int k=0; k<dim1; ++k)
                cout << va[i*dim1*dim2+j*dim1+k] << ' ';
            cout << endl;
        }
        cout << endl;
    }
    cout << endl;
}

int main(int argc, char* argv[])
{
    valarray<double> va(24);
    for(int i=0; i<24; i++)
        va[i] = i;

    printValarray3D(va, 3, 4);
    size_t lengthvalues[] = { 2, 3 };
    size_t stridevalues[] = { 12, 3 };
    valarray<size_t> length(lengthvalues, 2);
    valarray<size_t> stride(stridevalues, 2);

    va[gslice(0, length, stride)]
        = valarray<double>(va[gslice(1, length, stride)]);
    va[gslice(0, length, stride)]
        += valarray<double>(va[gslice(2, length, stride)]);
    printValarray3D(va, 3, 4);

}
/*-----------------masked1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
using namespace std;

template <class T>
void printValarray(const valarray<T>& va, int num)
{
    for(int i=0; i<va.size()/num; ++i)
    {
        for(int j=0; j<num; ++j)
            cout << va[i*num+j] << ' ';
        cout << endl;
    }
    cout << endl;
}

int main(int argc, char* argv[])
{
    valarray<double> va(12);
    for(int i=0; i<12; i++)
        va[i] = i;
    printValarray(va, 3);
    va[va<5.0] = 77.0;
    va[va>5.0 && va<9.0]
        = valarray<double>(va[va>5.0 && va<9.0]) + 100.0;
    printValarray(va, 3);
}
/*-----------------indi1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
using namespace std;

template <class T>
void printValarray(const valarray<T>& va, int num)
{
    for(int i=0; i<va.size()/num; ++i)
    {
        for(int j=0; j<num; ++j)
            cout << va[i*num+j] << ' ';
        cout << endl;
    }
    cout << endl;
}

int main(int argc, char* argv[])
{
    valarray<double> va(12);
    for(int i=0; i<12; i++)
        va[i] = (i+1) * 1.01;
    printValarray(va, 4);

    valarray<size_t> idx(4);
    idx[0] = 8;
    idx[1] = 0;
    idx[2] = 3;
    idx[3] = 7;

    printValarray(valarray<double>(va[idx]), 4);

    va[0] = 11.11;
    va[3] = 44.44;

    idx[0] = 1;
    idx[1] = 2;
    idx[2] = 5;
    idx[3] = 8;
    va[idx] = 99;

    printValarray(va, 4);

}
/*-----------------io1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
using namespace std;

int main(int argc, char* argv[])
{
    double x, y;
    cout << "Multiplecation of two floating point values" << endl;
    cout << "first operand: ";
    if(!(cin>>x))
    {
        cerr << "error while reading the first floating value"
             << endl;
        return EXIT_FAILURE;
    }

    cout << "second operand: ";
    if(!(cin >> y))
    {
        cerr << "error while reading the second floating value"
             << endl;
        return EXIT_FAILURE;
    }
    cout << x << " times " << y << " equals " << x * y << endl;
}
/*-----------------iosum1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
using namespace std;

namespace MyLib
{
    double readAndProcessSum(std::istream& strm)
    {
        double value, sum;
        sum = 0;
        while(strm >> value)
            sum += value;
        if(!strm.eof())
            throw std::ios::failure("input error in readAndProcessSum()");
        return sum;
    }
};

int main(int argc, char* argv[])
{
    double sum;

    try
    {
        sum = MyLib::readAndProcessSum(cin);
    }
    catch(const ios::failure& error)
    {
        cerr << "I/O exception: " << error.what() << endl;
        return EXIT_FAILURE;
    }
    catch(const exception& error)
    {
        cerr << "standard exception: " << error.what() << endl;
        return EXIT_FAILURE;
    }
    cout << "sum: " << sum << endl;
}

/*-----------------iocharcat1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
using namespace std;

int main(int argc, char* argv[])
{
    char c;
    while(cin.get(c))
        cout.put(c);
}
/*-----------------charset-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
#include <fstream>
using namespace std;

void writeCharsetToFile(const string& filename);
void outputFile(const string& filename);

int main(int argc, char* argv[])
{
    writeCharsetToFile("charset.out");
    outputFile("charset.out");
}

void writeCharsetToFile(const string& filename)
{
    ofstream file(filename.c_str());

    if(! file)
    {
        cerr << "can't open output file \"" << filename << "\""
             << endl;
        exit( EXIT_FAILURE );
    }

    for(int i=32; i<256; i++)
        file << "value: " << setw(3) << i << " "
             << "char: " << static_cast<char> (i) << endl;
}

void outputFile(const string& filename)
{
    ifstream file(filename.c_str());
    if(!file)
    {
        cerr << "can't open input file \"" << filename << "\""
             << endl;
        exit( EXIT_FAILURE );
    }

    char c;
    while(file.get(c))
        cout.put(c);
}
/*-----------------cat-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
#include <fstream>
using namespace std;


int main(int argc, char* argv[])
{
    ifstream file;

    for(int i=1; i<argc; ++i)
    {
        file.open(argv[i]);
        char c;
        while(file.get(c))
            cout.put(c);
        file.clear();
        file.close();
    }
}
/*-----------------cat2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
#include <fstream>
using namespace std;

void printFileTwice(const char* filename)
{
    std::ifstream file(filename);
    std::cout << file.rdbuf();
    file.clear();
    file.seekg(0);
    std::cout << file.rdbuf();
}

int main(int argc, char* argv[])
{
    for(int i=1; i<argc; ++i)
        printFileTwice(argv[i]);
}
/*-----------------rdbuf1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
#include <fstream>
using namespace std;

int main(int argc, char* argv[])
{
    ostream hexout(cout.rdbuf());
    hexout.setf(ios::hex, ios::basefield);
    hexout.setf(ios::showbase);

    hexout << "hexout: " << 177 << " ";
    cout   << "cout: "   << 177 << " ";
    hexout << "hexout: " << -49 << " ";
    cout   << "cout: "   << -49 << " ";
    hexout << endl;
}
/*-----------------rdbuf2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
#include <fstream>
using namespace std;

void hexMultiplicationTable(std::streambuf* buffer, int num)
{
    std::ostream hexout(buffer);
    hexout << std::hex << std::showbase;

    for(int i=1; i<=num; ++i)
    {
        for(int j=1; j<=10; ++j)
            hexout << i*j << ' ';
        hexout << std::endl;
    }
    
}

int main(int argc, char* argv[])
{
    int num = 5;
    cout << "We print " << num
         << " lines hexdecimal" << endl;

    hexMultiplicationTable(cout.rdbuf(), num);

    cout << "That was the output of " << num
         << " hexadecimal lines " << endl;
}
/*-----------------redirect-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
#include <fstream>
using namespace std;

void redirect(ostream&);

int main(int argc, char* argv[])
{
    cout << "the first row" << endl;
    redirect(cout);
    cout << "the last row" << endl;
}

void redirect(ostream& strm)
{
    ofstream file("redirect.txt");

    streambuf* strm_buffer = strm.rdbuf();

    strm.rdbuf(file.rdbuf());

    file << "one row for the file" << endl;
    strm << "one row for the stream" << endl;

    strm.rdbuf(strm_buffer);

}
/*-----------------rw1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
#include <fstream>
using namespace std;


int main(int argc, char* argv[])
{
    filebuf buffer;
    ostream output(&buffer);
    istream input(&buffer);
    buffer.open("examoke.dat", ios::in | ios::out | ios::trunc);

    for(int i=1; i<=4; i++)
    {
        output << i << ".line" << endl;
    
        input.seekg(0);
        char c;
        while(input.get(c))
            cout.put(c);
        cout << endl;
        input.clear();
    }
}
/*-----------------sstr1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
#include <fstream>
#include <sstream>
using namespace std;


int main(int argc, char* argv[])
{
    ostringstream os;
    os << "dec: " << 15 << hex << " hex: " << 15 << endl;
    cout << os.str() << endl;

    bitset<15> b(5789);
    os << "float: " << 4.67 << " bitset: " << b << endl;

    os.seekp(0);
    os << "oct: " << oct << 15;
    cout << os.str() << endl;
}
/*-----------------sstr1-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <locale>
#include <cmath>
#include <complex>
#include <cctype>
#include <limits>
#include <exception>
#include <valarray>
#include <fstream>
#include <sstream>
using namespace std;


int main(int argc, char* argv[])
{
    istreambuf_iterator<char> inpos(cin);
    istreambuf_iterator<char> endpos;

    ostreambuf_iterator<char> outpos(cout);

    while(inpos != endpos)
    {
        *outpos = *inpos;
        ++inpos;
        ++outpos;
    }
}
/*-----------------btiset2-------------*/
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <cstdlib>
#include <cstddef>
#include <functional>
#include <numeric>
#include <cmath>
#include <limits>
#include <exception>
using namespace std;

int main()
{
    cout << "267 as binary short: "
         << bitset<numeric_limits<unsigned short>::digits>(267)
         << endl;

    cout << "267 as binary long: "
         << bitset<numeric_limits<unsigned long>::digits>(267)
         << endl;

    cout << "10,000,000 with 24 bits: "
         << bitset<24>((unsigned long)(1E7))
         << endl;

    cout << "\"1000101011\" ad number: "
         << bitset<100>(string("1000101011")).to_ulong() << endl;
}
/*-----------------iter1-------------*/
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;

class ToLower
{
    public:
        char operator()(char val)
        {
            return tolower(val);
        }
};

int main(int argc, char* argv[])
{
    string s("The zip code of Hondelage in Gemany is 38108");
    cout << "original: " << s << endl;

    transform(s.begin(), s.end(),
              s.begin(),
              ToLower());
    cout << "lowered: " << s << endl;
}









你 想 要 一 個 適 合 商 務 溝 通 的 新 一 代 電 子 郵 箱 嗎 ?
中 國 最 大 最 穩 定 郵 件 系 統、國 家 骨 幹 網 絡 豪 華 帶 寬、國 際 頂 級 殺 毒 反 垃 圾 引 擎
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章