반응형


 http://blog.naver.com/uleena?Redirect=Log&logNo=80042072372




#include <iostream>

#include <string>

#include <functional>

using namespace std;

 

#include <ctime>

#include <bitset>

 

// unique 한랜덤을만들어내는함수객체

template<int MAX> class URandom

{

        bitset<MAX> bs;

        bool recycle;

public:

        URandom(bool b = false) : recycle(b)

        {

               srand(time(0)); // 난수초기화

               bs.set();       // 모든비트를1

        }

 

        int operator()()

        {

               if ( bs.none() )   // 모두0인경우

               {

                       if ( recycle == false// 재사용안하는경우

                              return -1;

                       else

                              bs.set();  // 모두1

               }

 

               int n = -1;

               while ( !bs.test( n = (rand() % MAX) ));

               bs.reset(n);

               return n;

        }

};

 

int main()

{

        URandom<5> r;

 

        for ( int i = 0; i < 15;++i)

               cout << r() << endl;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

// 3. bitset : 비트의집합

#include <bitset>

 

int main()

{

        bitset<10> bs;

        bs.reset(); // 모두0

 

        // 각비트를조작하는법

        bs.set(4);

        bs[5] = true;

        bs[6].flip();

 

        // 비트조사

        if ( bs.test(4) ) cout << "비트4set" << endl;

        if ( bs[5]      ) cout << "비트5set" << endl;

 

        unsigned long n = bs.to_ulong();

        cout << n << endl;

 

        string s = bs.to_string();

        cout << s << endl;

}

 

 

       

 

 

 

 

// 2. set : 순서를가지고요소를저장한다. (대부분의STL구현물은RB-Tree사용)

 

#include <set>

 

int main()

{

        typedef set<int, greater<int>> SET;

 

        //set< int, greater<int> > s;

        SET s;

       

        s.insert(10);

        s.insert( 5);

        s.insert(15);

        s.insert(30);

        s.insert(4);

 

        pair< SET::iterator, bool> ret = s.insert(10); // 중복허용될까?

 

        if ( ret.second == false )

               cout << "이미요소가있습니다." << endl;

 

 

        ostream_iterator<int> os(cout, " ");

 

        copy( s.begin(), s.end(), os);

}

 

 

 

 

 

 

 

 

 

 

// 1. pair의개념

 

 

template<typename T1, typename T2> struct pair

{

        T1 first;

        T2 second;

 

        pair(T1 a = T1(), T2 b = T2() ) :first(a), second(b) {}

};

 

pair<intint> foo()

{

        // 이안에서2개의값을리턴해주고싶다.

}

 

 

int main()

{

        // TR1(또는boost)에는pair에개선된버전인tuple이있다.

        tuple<> none;

        tuple<int> one;

        tuple<intint> two;

        tuple<intintint> three;

        tuple<intintintint> n;

 

 

 

        pair<int, pair<intint>> p2;

 

        pair<intdouble> p1(1,3.4);

 

        cout << p1.second << endl; // ?

}

반응형

+ Recent posts