资料: STL相关

vs2005中使用stl函数 make编译不过, 换个stl看看.


VC6.0、VS2005、VS2008安装STLport-5.2.1

http://hi.baidu.com/needwrong/blog/item/e13508e948e68132b90e2d7e.html


<2011_0820>

stl下载页面

http://sourceforge.net/projects/stlport/, 当前版本 5.2.1


在vs2005下编译StlPort5.2.1

进入vs2005命令行

D:\STLport-5.2.1>configure.bat msvc8

STLport Configuration Tool for Windows

Setting compiler: Microsoft Visual C++ 2005

Setting platform: Windows XP

Done configuring STLport.

Go to build/lib folder and type "nmake clean install" to build  and
install STLport to the "lib" and "bin" folders.
Go to build/test/unit folder and type nmake clean install to
build unit tests and install them in bin folder.

cd build\lib

D:\STLport-5.2.1\build\lib\nmake clean install

D:\STLport-5.2.1\build\test\unit>nmake clean install

单元测试编译不过去, 不影响Stl的使用
http://www.zgepsa.com/archiver/tid-80873.html

编译完成后
D:\STLport-5.2.1\bin 是StlPort's dll
D:\STLport-5.2.1\stlport 是vs2005编译时要包含的头路径
D:\STLport-5.2.1\lib 是vs2005编译时要包含的lib路径

运行下面的demo, 调整vsIde设置
The C++ Standard Library - A Tutorial and Reference
http://www.josuttis.com/libbook/toc.html


stl demo from   http://www.josuttis.com/libbook/toc.html, 例子太多了~, 适合作为参考手册, 用啥去找啥. 比看那本<<The C++ Standard Library - A Tutorial and Reference>>效果好得多.

/**
* @file stl-demo.cpp
*/

/**
* @note
* vs2005's include directory first is : D:\STLport-5.2.1\stlport
* vs2005's lib directory is : D:\STLport-5.2.1\lib
*/

#define NOMINMAX			/**< not use windows.h's max */
#include <windows.h>
#include <locale.h>			/**< for LC_ALL */
#include <tchar.h>			/**< for _TCHAR */
#include <string>			/**< for _StlMsg_AUTO_PTR_NULL */
#include <stdio.h>			/**< for getchar() */
#include <iostream>			/**< for cout */
#include <limits>			/**< for numeric_limits */

#include "stl/_auto_ptr.h"	/**< for auto_ptr */
#include <algorithm>		/**< for max */
#include <vector>			/**< for vector */
#include <deque>			/**< for deque */
#include <list>				/**< for list */
#include <set>				/**< for set */
#include <map>				/**< for map */
#include <iterator>			/**< ostream_iterator */

#ifndef tstring
#ifdef _UNICODE
#define tstring std::wstring
#else
#define tstring std::string
#endif
#endif

VOID fn_autoptr();			/**< autoptr's demo */
VOID fn_show_type();
VOID fn_MyMax();

/**< function that compares two pointers by comparing the values to which they point */
BOOL int_ptr_less (PINT a, PINT b);

VOID show_vector();
VOID show_deque();
VOID show_List();
VOID show_List1();
VOID show_Set();
VOID show_map1();
VOID show_map2();
VOID show_map3();
VOID show_algorithm1();
VOID show_find1();
VOID show_copy1();
VOID show_copy2();
VOID show_copy3();
VOID show_remove();
VOID show_remove1();

int main()
{
    _tsetlocale(LC_ALL, _T("Chinese_People's Republic of China.936"));
    _tprintf(_T("测试STL的使用方法\n"));

    fn_show_type();
    fn_autoptr();
    fn_MyMax();
    show_vector();
    show_deque();
    show_List();
    show_List1();
    show_Set();
    show_map1();
    show_map2();
    show_map3();
    show_algorithm1();
    show_find1();
    show_copy1();
    show_copy2();
    show_copy3();
    show_remove();
    show_remove1();

    _tprintf(_T("END, press any key to quit \n"));
    getchar();
    return 0;
}

VOID show_remove1()
{
	std::set<int> coll;

    // insert elements from 1 to 9
    for (int i=1; i<=9; ++i)
    {
        coll.insert(i);
    }

    // print all elements of the collection
    std::copy (coll.begin(), coll.end(),
		std::ostream_iterator<int>(std::cout," "));
    std::cout << std::endl;

    /* Remove all elements with value 3
     * - algorithm remove() does not work
     * - instead member function erase() works
     */
    size_t num = coll.erase(3);

    // print number of removed elements
    std::cout << "number of removed elements: " << num << std::endl;

    // print all elements of the modified collection
    std::copy (coll.begin(), coll.end(),
		std::ostream_iterator<int>(std::cout," "));
    std::cout << std::endl;

	/** run results
	1 2 3 4 5 6 7 8 9
	number of removed elements: 1
	1 2 4 5 6 7 8 9
	*/
}

VOID show_remove()
{
    size_t nLen = 0;
    size_t nLen1 = 0;
    std::list<int>::iterator it;
    std::list<int>::iterator it_end_after_remove;
    std::list<int> coll;
    std::list<int> col2;

    // insert elements from 6 to 1 and 1 to 6
    for (int i=1; i<=6; ++i)
    {
        coll.push_front(i);
        coll.push_back(i);
    }

    // print all elements of the collection
    std::cout << "pre:  ";
    col2.resize(coll.size());
    std::copy(coll.begin(), coll.end(),           // source
              col2.begin());   // destination

    for (it = col2.begin(); it != col2.end(); ++it)
    {
        _tprintf(_T("%d "), *it);
    }
    _tprintf(_T("\n"));

    _tprintf(_T("before remove size = %d\n"), col2.size());
    it_end_after_remove = std::remove(col2.begin(), col2.end(),         // range
                                      2);                               // value

    /**< !!!在remove之后, 重新设置size, 否则被操作的容器size不变 */
    col2.erase(it_end_after_remove, col2.end());
    _tprintf(_T("after remove size = %d\n"), col2.size());

    // print all elements of the collection
    std::cout << "post: ";
    for (it = col2.begin(); it != col2.end(); ++it)
    {
        _tprintf(_T("%d "), *it);
    }
    _tprintf(_T("\n"));

    /** run results
    * pre:  6 5 4 3 2 1 1 2 3 4 5 6
    * before remove size = 12
    * after remove size = 10
    * post: 6 5 4 3 1 1 3 4 5 6
    */
}

VOID show_copy3()
{
    std::list<int> coll1;

    // insert elements from 1 to 9 into the first collection
    for (int i=1; i<=9; ++i)
    {
        coll1.push_back(i);
    }

    // copy the elements of coll1 into coll2 by appending them
    std::vector<int> coll2;
    std::copy (coll1.begin(), coll1.end(),      // source
               std::back_inserter(coll2));           // destination

    // copy the elements of coll1 into coll3 by inserting them at the front
    // - reverses the order of the elements
    std::deque<int> coll3;
    std::copy (coll1.begin(), coll1.end(),      // source
               std::front_inserter(coll3));          // destination

    // copy elements of coll1 into coll4
    // - only inserter that works for associative collections
    std::set<int> coll4;
    std::copy (coll1.begin(), coll1.end(),      // source
               std::inserter(coll4,coll4.begin()));  // destination
}

VOID show_copy2()
{
    std::list<int>   coll1;
    std::vector<int> coll2;

    // insert elements from 1 to 9
    for (int i=1; i<=9; ++i)
    {
        coll1.push_back(i);
    }

    // resize destination to have enough room for the overwriting algorithm
    coll2.resize (coll1.size());

    /* copy elements from first into second collection
     * - overwrites existing elements in destination
     */
    copy (coll1.begin(), coll1.end(),     // source
          coll2.begin());                 // destination

    /* create third collection with enough room
     * - initial size is passed as parameter
     */
    std::deque<int> coll3(coll1.size());

    // copy elements from first into third collection
    copy (coll1.begin(), coll1.end(),     // source
          coll3.begin());                 // destination
}

VOID show_copy1()
{
    std::list<int>   coll1;
    std::vector<int> coll2;

    // insert elements from 1 to 9
    for (int i=1; i<=9; ++i)
    {
        coll1.push_back(i);
    }

    coll2.resize(coll1.size()); /**< 在拷贝之前, 要保证目标容器有足够的size */
    std::copy(coll1.begin(), coll1.end(),     // source
              coll2.begin());                 // destination
}

VOID show_find1()
{
    _tprintf(_T(">> show_find1\n"));
    std::list<int> coll;
    std::list<int>::iterator pos;

    // insert elements from 20 to 40
    for (int i=20; i<=40; ++i)
    {
        coll.push_back(i);
    }

    /* find position of element with value 3
     * - there is none, so pos gets coll.end()
     */
    pos = find (coll.begin(), coll.end(),    // range
                3);                          // value

    /* reverse the order of elements between found element and the end
     * - because pos is coll.end() it reverses an empty range
     */
    reverse (pos, coll.end());

    // find positions of values 25 and 35
    std::list<int>::iterator pos25, pos35;
    pos25 = find (coll.begin(), coll.end(),  // range
                  25);                       // value
    pos35 = find (coll.begin(), coll.end(),  // range
                  35);                       // value

    /* print the maximum of the corresponding range
     * - note: including pos25 but excluding pos35
     */
    std::cout << "max: " << *max_element (pos25, pos35) << std::endl;

    // process the elements including the last position
    std::cout << "max: " << *max_element (pos25, ++pos35) << std::endl;

    /** run results
    * max: 34
    * max: 35
    */
}

VOID show_algorithm1()
{
    std::vector<int> coll;
    std::vector<int>::iterator pos;

    // insert elements from 1 to 6 in arbitrary order
    coll.push_back(2);
    coll.push_back(5);
    coll.push_back(4);
    coll.push_back(1);
    coll.push_back(6);
    coll.push_back(3);

    // find and print minimum and maximum elements
    pos = std::min_element (coll.begin(), coll.end());
    _tprintf(_T("min value [%d]\n"), *pos);

    pos = std::max_element (coll.begin(), coll.end());
    _tprintf(_T("max value [%d]\n"), *pos);

    // sort all elements
    std::sort(coll.begin(), coll.end());
    // print all elements
    for (pos=coll.begin(); pos!=coll.end(); ++pos)
    {
        _tprintf(_T("%d, "), *pos);
    }
    _tprintf(_T("\n"));

    // find the first element with value 3
    pos = std::find(coll.begin(), coll.end(),  // range
                    3);                        // value
    if (pos && (pos != coll.end()))
    {
        _tprintf(_T("find 3\n"));
    }
    else
    {
        _tprintf(_T("not find 3\n"));
    }

    // find the first element with value note exist
    pos = std::find(coll.begin(), coll.end(),  // range
                    99);                        // value
    if (pos && (pos != coll.end()))
    {
        _tprintf(_T("find 99\n"));
    }
    else
    {
        _tprintf(_T("not find 99\n"));
    }

    // reverse the order of the found element with value 2 and all following elements
    pos = std::find(coll.begin(), coll.end(),  // range
                    2);                        // value
    if (pos && (pos != coll.end()))
    {
        _tprintf(_T("find 2\n"));
        std::reverse(pos, coll.end());
    }
    else
    {
        _tprintf(_T("not find 2\n"));
    }

    // print all elements
    for (pos=coll.begin(); pos!=coll.end(); ++pos)
    {
        _tprintf(_T("%d, "), *pos);
    }
    _tprintf(_T("\n"));

    /** run results
    * min value [1]
    * max value [6]
    * 1, 2, 3, 4, 5, 6,
    * find 3
    * not find 99
    * find 2
    * 1, 6, 5, 4, 3, 2,
    */
}

VOID show_map3()
{
    /* type of the container:
     * - map: elements key/value pairs
     * - string: keys have type string
     * - float: values have type float
     */
    typedef std::map<tstring,float> StringFloatMap;

    StringFloatMap coll;

    // insert some elements into the collection
    coll[_T("VAT")] = 0.15f;
    coll[_T("Pi")] = 3.1415f;
    coll[_T("an arbitrary number")] = 4983.223f;
    coll[_T("Null")] = 0.0f;

    /* print all elements
     * - iterate over all elements
     * - element member first is the key
     * - element member second is the value
     */
    StringFloatMap::iterator pos;
    for (pos = coll.begin(); pos != coll.end(); ++pos)
    {
        _tprintf(_T("key: \"%s\" value:%f\n"), pos->first.c_str(), pos->second);
    }

    /** run results
    * key: "Null" value:0.000000
    * key: "Pi" value:3.141500
    * key: "VAT" value:0.150000
    * key: "an arbitrary number" value:4983.223145
    */
}

VOID show_map2()
{
    size_t nLen1 = 0;
    std::multimap<tstring, tstring> DepartmentMap;
    std::multimap<tstring, tstring>::iterator it;

    DepartmentMap.insert(std::make_pair(_T("研发一部"), _T("赵星星")));
    DepartmentMap.insert(std::make_pair(_T("研发二部"), _T("李星星")));
    DepartmentMap.insert(std::make_pair(_T("研发一部"), _T("钱星星")));
    DepartmentMap.insert(std::make_pair(_T("研发一部"), _T("孙星星")));

    nLen1 = DepartmentMap.count(_T("研发一部"));
    it = DepartmentMap.find(_T("研发一部"));
    while (nLen1-- > 0)
    {
        _tprintf(_T("%s.%s\n"), it->first.c_str(), it->second.c_str());
        it++;
    }

    _tprintf(_T("\n"));

    /** run results
    * 研发一部.赵星星
    * 研发一部.钱星星
    * 研发一部.孙星星
    */
}

VOID show_map1()
{
    // type of the collection
    typedef std::multimap<int,tstring> IntStringMMap;

    IntStringMMap coll;        // container for int/string values

    // insert some elements in arbitrary order
    // - a value with key 1 gets inserted twice
    coll.insert(std::make_pair(5,_T("tagged")));
    coll.insert(std::make_pair(2,_T("a")));
    coll.insert(std::make_pair(1,_T("this")));
    coll.insert(std::make_pair(4,_T("of")));
    coll.insert(std::make_pair(6,_T("strings")));
    coll.insert(std::make_pair(1,_T("is")));
    coll.insert(std::make_pair(3,_T("multimap")));

    /* print all element values
     * - iterate over all elements
     * - element member second is the value
     */
    IntStringMMap::iterator pos;
    for (pos = coll.begin(); pos != coll.end(); ++pos)
    {
        _tprintf(_T("%s "), pos->second.c_str());
    }
    _tprintf(_T("\n"));

    /** run results
    * this is a multimap of tagged strings
    */
}

VOID show_Set()
{
    // type of the collection
    typedef std::set<BYTE> IntSet;

    IntSet coll;        // set container for int values

    /* insert elements from 1 to 6 in arbitrary order
     * - value 1 gets inserted twice
     */
    coll.insert(3);
    coll.insert(1);
    coll.insert(5);
    coll.insert(4);
    coll.insert(1);
    coll.insert(6);
    coll.insert(2);

    /* print all elements
     * - iterate over all elements
     */
    IntSet::const_iterator pos;
    for (pos = coll.begin(); pos != coll.end(); ++pos)
    {
        _tprintf(_T("0x%2.2x, "), *pos);
    }
    _tprintf(_T("\n"));

    /** run results
    * 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
    */
}

VOID show_List1()
{
    std::list<TCHAR> coll;      // list container for character elements

    // append elements from 'a' to 'z'
    for (TCHAR c = _T('a'); c <= _T('z'); ++c)
    {
        coll.push_back(c);
    }

    /* print all elements
     * - iterate over all elements
     */
    std::list<TCHAR>::const_iterator pos;
    for (pos = coll.begin(); pos != coll.end(); ++pos)
    {
        _tprintf(_T("%c, "), *pos);
    }
    _tprintf(_T("\n"));
}

VOID show_List()
{
    std::list<TCHAR> coll;      // list container for character elements

    // append elements from 'a' to 'z'
    for (TCHAR c = _T('a'); c <= _T('z'); ++c)
    {
        coll.push_back(c);
    }

    /* print all elements
     * - while there are elements
     * - print and remove the first element
     */
    while (!coll.empty())
    {
        _tprintf(_T("%c, "), coll.front());
        coll.pop_front();
    }
    _tprintf(_T("\n"));

    /** run results
    * a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z,
    */
}

VOID show_deque()
{
    INT i = 0;
    size_t n = 0;
    std::deque<float> coll;    // vector container for integer elements

    for (n = 1; n <= 6; ++n)
    {
        coll.push_front(n * 1.1f);
    }

    for (n = 0; n < coll.size(); ++n)
    {
        _tprintf(_T("%f, "), coll[n]);
    }
    std::cout << std::endl;

    /** run results
    * 6.600000, 5.500000, 4.400000, 3.300000, 2.200000, 1.100000,
    */
}

VOID show_vector()
{
    INT i = 0;
    size_t n = 0;
    std::vector<int> coll;    // vector container for integer elements

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

    for (n = 0; n < coll.size(); ++n)
    {
        _tprintf(_T("%d, "), coll[n]);
    }
    std::cout << std::endl;

    /** run results
    * 0, 1, 2, 3, 4, 5,
    */
}

VOID fn_MyMax()
{
    int x = 17;
    int y = 42;
    int* px = &x;
    int* py = &y;
    int* pmax;

    pmax = std::max(px, py, int_ptr_less); /**< call max() with special comparison function */
    _tprintf(_T("max(%d, %d) is <%d>\n"), *px, *py, *pmax);

    /** run results
    * max(17, 42) is <42>
    */
}

BOOL int_ptr_less (PINT a, PINT b)
{
    return *a < *b;
}

VOID fn_show_type()
{
    // print maximum of integral types
    std::cout << "max(short): " << std::numeric_limits<short>::max() << std::endl;
    std::cout << "max(int):   " << std::numeric_limits<int>::max() << std::endl;
    std::cout << "max(long):  " << std::numeric_limits<long>::max() << std::endl;
    std::cout << std::endl;

    // print maximum of floating-point types
    std::cout << "max(float):       "
              << std::numeric_limits<float>::max() << std::endl;
    std::cout << "max(double):      "
              << std::numeric_limits<double>::max() << std::endl;
    std::cout << "max(long double): "
              << std::numeric_limits<long double>::max() << std::endl;
    std::cout << std::endl;

    // print whether char is signed
    std::cout << "is_signed(char): "
              << std::numeric_limits<char>::is_signed << std::endl;
    std::cout << std::endl;

    // print whether numeric limits for type string exist
    std::cout << "is_specialized(string): "
              << std::numeric_limits<std::string>::is_specialized << std::endl;

    /** run results
    * max(short): 32767
    * max(int):   2147483647
    * max(long):  2147483647
    *
    * max(float):       3.40282e+38
    * max(double):      1.79769e+308
    * max(long double): 1.79769e+308
    *
    * is_signed(char): true
    *
    * is_specialized(string): false
    */
}

/**
* autop_ptr 的使用习惯和普通指针不一样, 用起来很容易报错
* 要仔细调整才行, 接受不了 =_=
*/
VOID fn_autoptr()
{
    INT iRc = 0;
    std::auto_ptr<int> p(new int(42));
    std::auto_ptr<int> q;

    q = p;
    _tprintf(_T("q = <%d>\n"), *q);

    iRc = *q;
    _tprintf(_T("iRc = <%d>\n"), iRc);

    *q += 13;
    _tprintf(_T("q = <%d>\n"), *q);

    p = q;
    _tprintf(_T("p = <%d>\n"), *p);

    /**
    * run results
    * q = <42>
    * iRc = <42>
    * q = <55>
    * p = <55>
    */
}


你可能感兴趣的:(Algorithm,vector,list,iterator,insert,pair)