#include <fstream>

#include <sstream>

using namespace std;

 

void foo( pair<string, list<int> > p )

{

        cout << p.first << endl;

}

 

void main()

{

        map<string, list<int> > cross_index; // 이 자료구조를 생각해보자.

 

        //map<string, list<list<int> > >;    // 2차원 배열도 받아서 할 수 있다.

 

        ifstream f("a.cpp");   // 현재 파일이름 넣으세요.

        int line = 0;          // line no 관리

        string temp;

 

        // file에서 한 줄씩 읽어 들인다.

        while( getline( f, temp ) )     // 주의 f >> temp; 한 단어 입력

        {

               ++line; // 라인 no++

 

               istringstream iss( temp );

               string word;

 

               while( iss >> word )          // 한 문장을 단어별로 분리해 낸다.

               {

                       cross_index[ word ].push_back(line);

               }

        }

        //------------------------------------

        // 이제 map을 출력한다.

 

        // print_word_list

        for_each( cross_index.begin(), cross_index.end(), foo );

 

        // print_word_line_list

        map<string, list<int> >::iterator p = cross_index.begin();

       

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

 

        while ( p != cross_index.end() )

        {

               cout << p->first << " : "; // 단어출력

               copy( p->second.begin(), p->second.end(), out );

               cout << endl;

 

               ++p;

        }

}

 

// 응용비디오샵!!!

//map<Customer, list<VideoTape> > retal_list

//copy( cross_index["홍길동"].begin(), cross_index["홍길동"].end(), out );

 

 

Tag |

8.3(금) C++ - stringstream

from Study/C++ 2007/08/08 11:49 view 21322

#include <sstream>

#include <fstream>

 

// stringstream 에 대해서

// stream3개로 구분 할 수 있다. istream, ostream, stringstream

 

void main()

{

        char buf [256];

        cin >> buf;    // 표준입력에서 입력

 

        ifstream f("a.txt");

        f >> buf;      // 화일에서 입력

 

        //////////////////////////////////////////////////////

        string s = "I am a boy";

        istringstream iss(s);

 

        // 하나의 문장을 단어별로 분리하는 기법

        while( iss >> buf )

        {

               cout << buf << endl;

        }

}

//////////////////////////////////////////////////////
void
main()

{

        ofstream f("a.txt");

        ostringstream oss;

 

        cout << "A";   // 표준출력으로 출력

        f       << "A";       // 화일로 출력(a.txt 파일)

        oss     << "A";

 

        string s = oss.str();  // oss 가 내부적으로 가진 string 꺼낸다.

        cout << s << endl;

}

Tag |

8.3(금) C++ - 연관 컨테이너의 종류

from Study/C++ 2007/08/08 11:47 view 18555

// 연관 Container( Associative Container )


// 1. set: 순서를 가지고 값을 저장한다. 중복을 허용하지 않는다.

void main()

{

        // 트리!! 완전이진트리,  AVL, 레드블랙!!

        // 들어올때마다 sort를 하기 때문에  빠르다.

        // 두번째 인자로 단위전략을 쓰게되면 정렬방식을 바꿀 수 있다.

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

        set<int, greater<int> > s;

       

        s.insert(10);

        s.insert(20);

        s.insert(15);

        s.insert(100);

 

        SET::iterator p = s.begin();

        *p = 100;      // 성공은 하지만 정렬관계가 망가진다. 하지말자.

 

        //pair<set<int, greater<int> >::iterator, bool> ret = s.insert(15);

        pair<SET::iterator, bool> ret = s.insert(15);

        // insert가 실패 하면 ret에 fasle가 리턴된다. 중복된 것이므로 실패
       
if ( ret.second == false )

        {

               cout << "실패" << endl;

        } 

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

}



// 2. pair 에대해서

// 서로 다른 type 2개를 보관 할 수 있는 자료구조.


pair
<int, double> foo()

{

        return pair<int, double>(3, 3.4);

}

 

template<typename T1, typename T2> struct pair

{

        T1 first;

        T2 second;

        //- zero initialize

        //T b = T(); // built in type이면으로

        //           // User type이면default 생성자호출

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

 

void main()

{

        pair<int, pair<int, double> > p2;     // 3개의 인자를 받고 싶을때!!

 

        // BOOST 안에는 pair를 개선한 tuple이 있습니다.

        //tuple<int> one;

        //tuple<int, int> two;

        //tuple<int, int, int> three;

 

        pair<int, double> p;

 

        cout << sizeof(p) << endl;

}


// 3. map : pair를 저장하는 set. key를 가지고 data를 저장.

void main()

{

        map<string, string> m;

 

        // map에 요소를 넣는방법1. insert 사용

        pair<string, string> p("mon", "월요일");

 

        m.insert(p);

 

        // 2. [] 연산자 사용

        m["tue"] = "화요일";

 

        cout << m["mon"] << endl;     // 월요일

        cout << m["web"] << endl;

}


// 4. stringstream

#include <sstream>

#include <fstream>

 

int main()

{

        int n = 10;

        ostringstream oss;

 

        // n을 문자열로 바꾸고 싶다.

        oss << "n = " << n;           // C라면 sprintf( buf, "n = %d", n );

        string s = oss.str();  // oss가 가진 string을 꺼낸다.

        cout << s << endl;

 

        char buf[256];

        //cin >> buf;

        //ifstream f( "a.cpp" );

        //f >> buf;

 

        // Cstrtok()STL style로 구현한 기법.

        istringstream iss( "I am a boy" );

 

        while( iss >> buf ) cout << buf << endl;

}

Tag |

8.3(금) C++ - queue에서의 단위전략

from Study/C++ 2007/08/07 21:26 view 17125

#include <queue>       // Q 또는우선순위Q를사용.

                       // 내부적으로 사용 할 container의 헤더도include 해야 한다.

#include <functional>

 

 

template<typename T, typename Cont = vector<T>, typename Pred = less<T> >

class priority_queue

{

        Pred f; // 조건자함수 객체생성

public:

        void foo()

        {

               // 크기비교가 필요하다면

               bool b = f(1, 2);

        }

};

 

// 절대값으로 크기를 비교하는 함수객체

template<typename T> struct abs_less

{

        bool operator()( T a, T b )

        {

               return abs(a) < abs(b);

        }

};

 

void main()

{

// greater<int> 는 템플릿 인자를 넘기기 때문에 타입을 넘긴다. , () 를 쓰지 않는다.!!!

// 결국 단위전략의 크기 비교객체를 다양하게 활용 할 수 있다.

        // 디폴트로 vector를 사용.
       
priority_queue<int, vector<int>, abs_less<int> > pq;
 

        pq.push(10);

        pq.push(20);

        pq.push(-50);

        pq.push(40);

        pq.push(30);

 

        cout << pq.top() << endl;
}

Tag |

8.3(금) C++ - stack에서의 단위전략

from Study/C++ 2007/08/07 21:22 view 16890

// 어답터 디자인패턴 기존의 있던 것을 이름만 바꿔서 새롭게 구성.

// Container 어답터

// list가있다. 그런데stack이필요하다.

 

template<typename T, typename Cont = deque<T> > class stack

{

        Cont st;

public:

        void push( T a ) { st.push_back(a); }

        void pop()       { st.pop_back(); }

        T&   top()       { return st.back(); }

};

 

#include <stack>

 

void main()

{

        stack<int, list<int> > st;

        stack<int> s; 

}

Tag |

// using namespace std::rel_ops;

// std::rel_ops 이름 공간안에는 아래 함수가 있다.

// < == 연산자를 정의해두면 다른연산자를 지원할 수 있다.!!!!!!!!!!

template<typename T> bool operator>( const T& a, const T& b)

{

        return !( a == b || a < b );

}

 

struct Point

{

        int x;

        int y;

public:

        Point( int a = 0, int b = 0 ) : x(a), y(b) {}

 

        bool operator==( const Point& p ) const

        {

               return true;

        }

        bool operator<( const Point& p ) const       // less than

        {

               return true;

        }

};

 

void main()

{

        Point p1, p2;

 

        bool b = p1 > p2;      // greater than

}

Tag |

8.3(금) C++ - 단위전략의 기본

from Study/C++ 2007/08/07 21:12 view 17514

// STLstring이 가지고 있는 단위전략패턴의 개념

// STLstring은 문자열의 비교를 위해 단위전략클래스를 사용하고 있다.

// 사용자가 만들어서 전달할수도있다.

 

struct my_traits : public char_traits<char>

{

        // 두문자가 같은 지를 비교하기 위해 아래함수를 호출한다.

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

        static bool lt(char c1, char c2)
        {
               return toupper(c1) < toupper(c2);
        }

        static int  compare( const char* s1, const char* s2, size_t n )

        {

               return memicmp( s1, s2, n );

        }

 

        // 그 외의 몇개의 함수를 더 제공해야 한다.

        // 하지만 상속 받았으므로 부모 것을 그냥 사용 할 수도 있다.

};

 

typedef basic_string<char, my_traits, allocator<char> > ci_string;

 

void main()

{

        // 템플릿 인자를 사용하는 단위전략, 함수 단위전략보다 다양한 작업가능.

        // , 컴파일시간에 결정되므로 오버헤드도 줄인다.

        // basic_string<char, my_traits, allocator<char> > s1 = "AA"; 
        //
유니코드, 단위전략, 할당

        ci_string s1 = "hello";

        ci_string s2 = "HELLO";

 

        //string s1 = "hello";

        //string s2 = "HELLO";

 

        if( s1 == s2 )

        {

               cout << "same" << endl;

 

        }

        else

        {

               cout << "NotSame" << endl;

        }

}

Tag | ,

8.3(금) C++ - 다형성의 기본

from Study/C++ 2007/08/07 20:54 view 18197

// C++에서다형성을지원하기위해있는문법

// 1. 가상함수- 실행시간다형성

// 2. 템플릿  - 컴파일시간다형성

// 3. 오버로딩- 컴파일시간


class Animal

{

public:

        virtual void Cry() {}  // 1

};

 

class Dog : public Animal

{

public:

        virtual void Cry() {}  // 2

};

 

template<typename T> void foo( T a )  // Cry라는함수가없다면에러가나온다.

{

        a.Cry();       // 다형성-> 컴파일타임에결정된다. ( type을알고있다. )

}

 

 

void foo( Animal* p )  // Animal의자식들만받는다.

{

        p->Cry();      // 1 ? 2 다형성-> 실행시간다형성( 메모리를찾아간다. )

}

 

 

Tag |

8.3(금) C++ - Sequence의 생성

from Study/C++ 2007/08/07 20:49 view 18168

vector, deque, list 는 다음과 같은 생성자를 갖는다.
 

vector<int> v1;
vector<int> v2(5);
vector<int> v3( 5, 5 );                 // 5개를5로초기화
vecotr<int> v4( v3.begin(), v3.end() ); // v3의끝과처음으로초기화
vector
<int> v5( v4 );                   // v4로초기화
Tag |

8.3(금) C++ - Allocator, 단위전략

from Study/C++ 2007/08/07 20:36 view 22862

** 사용자 정의 type을 Sequence Container에 넣으려면

1. 기본 생성자가 반드시 정의되어 있어야 한다.
2. 복사 생성자도 명시적으로든 묵시적으로든 반드시 존재해야 한다.
3. 일부 generic 알고리즘에서 원소들간의 상등관계나 대소 관계를 필요로 하기 때문에
   == 연산자와 < 연산자를 정의해 두는 것이 좋다.


// Allocator
의개념...

// 메모리 할당만 책임을 지는 클래스를 설계한다.

template<typename T> class allocator

{

public:

        T* allocate( size_t sz )

        {

        }

        void deallocate( void* p )

        {

        }

};

 

// 단위전략기반의 설계: 한가지한가지의 정책을 template 인자로 전달할수 있게하는 기법.!!

template<typename T, typename Alloc = allocator<T> > class vector

{

        T* buf;

        Alloc alloc;           // 메모리할당기객체.

public:

        void push_front( T a )

        {

               // 메모리할당이 필요하게 되었다. !!

               // 어떤 방법으로 할당을 하는게 최고일까?
               // new ? malloc ? object pooling

               int* p = (int*)alloc.allocate();

        }

};

 

// 메모리 할당전략을 바꾸고 싶을때 내가 만든 클래스로 전달하게 해준다.!!!

// 기본할당자 allocator 를 가로챌수 있게 된다.

template<typename T> class myallocator

{

public:

        T* allocate( size_t sz )

        {

        }

        void deallocate( void* p )

        {

        }

};

 

void main()

{

        vector<int, myallocator<int> > v;

}

Tag |