ó: http://obori.new21.net

α׷ Ŀ´Ƽ www.codein.co.kr (Code)

 

1.

 

̳(container), ˰(algorithm), ݺ(iterator) 3 STL ߿ ͵̴.

̳(container) ü Ƶ ִ ü Ѵ.

˰(algorithm) ̳ʿ ϱ ؼ ʿ ݺ(iterator)̴.

ݺڴ ̳ Ư ġ Ű Ϳ ̶ Ǹ,

ݺڸ ̳ ҵ پ ٷ Ǵ ̴.

 

2. ݺ(iterator)

 

1.

ݺ(iterator), ̳ʿ Ǿ ִ ҵ ü Ⱦ ϴ,

Ϳ ü ִ.

˰򸶴 ٸ ̳ʸ Ⱦ , ݺڿ ִ.

 

past-the-end Ҵ, (range) Ÿ, ġ Ҹ ĪѴ.

 

2. ݺ

ǥ ̺귯 Ǵ ݺڵ 5 µ ִ.

Է ݺ(input iterator)

б⸸ , ̵

ݺ(output iterator)

, ̵

ݺ(forward iterator)

б/ , ̵

ݺ(bidirectional iterator)

б/ , / ̵

ݺ(random access iterator)

б/ ,

 

ݺڴ зȴ.

ݺ

Ǵ

Է ݺ(input iterator)

istream_iterator

ݺ(output iterator)

ostream_iterator / inserter / front_inserter / back_inserter

ݺ(bidirectional iterator)

list / set multiset / map multimap

ݺ(random access iterator)

Ϲ / vector / deque

 

2.1 Է ݺ(input iterator)

ex)        template <class InputIterator, class T>

            InputIterator find (InputIterator first, InputIterator last, const T& value)

            {

                        while (first != last && *first != value)

                                     ++first;

                        return first;

            }

 

Է ݺڿ ִ.

Ϲ

̳ ݺ(container iterator).

ǥ ̺귯 ϴ پ ̳ʷκ ݺڵ, Է ݺڰ ߾ Ѵ.

÷ ù° Ҵ begin()̶ Լ ؼ ,

past-the-end Ű ݺڴ end() Լ ؼ ִ.

 

ݺڸ ϴ ̳ʵ Ŭ ȿ ݺڶ ̸ Ÿ ؾ Ѵ.

ϰ ϴ ̳ʰ ̰ų, const_iterator Ͽ ϸ, ݺڴ ݺڰ ȴ.

 

Է Ʈ ݺ(input stream iterator).

ǥ ̺귯 Է ݺڸ Ͽ Է Ʈ ۾ ִ Ѵ.

̴ istream_iterator Ŭ ȴ.

 

2.2 ݺ(output iterator)

ݺ(output iterator) Է ݺڿʹ ݴǴ .

ݺڴ ϴµ , ϴµ .

ex)        template <class InputIterator, class OutputIterator>

            OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)

            {

                        while (first != last)

                                     *result++ = *first++;

                        return result;

            }

 

2.3 ݺ(forward iterator)

ݺ(forward iterator) Է ݺڿ ݺ Ư¡ ̴.

ϰ ϴ ϰ ̴.

ex)        template <class ForwardIterator, class T>

            void replace (ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value)

            {

                        while (first != last) {

                                     if (*first == old_value)

                                                 *first = new_value;

                                     ++first;

                        }

            }

 

2.4 ݺ(bidirectional iterator)

ݺ(bidirectional iterator) ݺڿ ϴ.

ٸ, ݺڴ (--) ϹǷ, ̳ʳ ҵ ̳ ̵ϴ ȴ.

ex)        template <class BidirectionalIterator, class OutputIterator>

            OutputIterator reverse_copy (BidirectionalIterator first,

                        BidirectionalIterator last, OutputIterator result)

            {

                        while (first != last)

                                     *result++ = *--last;

                        return result;

            }

 

2.5 ݺ(random access iterator)

ݺ(random access iterator) ÷ڿ ϰ,

ٸ ݺڿ ְ, ִ

Ϲ Ͱ ߴ ִ.

 

ݺڸ ϴ ˰򿡴 İ ˻ Ѵ.

ex)        template <class RandomAccessIterator>

            void mixup(RandomAccessIterator first, RandomAccessIterator last)

            {

                        while (first < last) {

                                     iter_swap(first, first + randomInteger(last - first));

                                     ++first;

                        }

            }

 

2.6 ݺ(reverse iterator)

ݺ(reverse iterator) ǥ ݺڰ ο ʹ ݴǴ Ѵ.

, vector list ݺڴ Ҹ ϰ, ù° Ҹ Ѵ.

set ū Ұ , Ұ ٵȴ.

ؼ, ݺڴ ο ݺڰ ƴϴ.

 

list, set, map Ÿ ݺڸ ϴ Լ ѽ ϰ ִ.

rbegin() rend() Լ ش ̳ʸ ȴ ݺڸ Ѵ.

ݺڿ ǹϰ, ҿ ǹѴ.

 

ϰ, vector deque ŸԿ ݺڸ ϴ Լ(rbegin() rend()) ִ.

Ӹƴ϶, ÷ڿ ǹѴ.

 

3. Ʈ ݺ(stream iterator)

Ʈ ݺڴ ݺ ̿Ͽ Է Ǵ Ʈ ϴµ

ȴ.

 

3.1 Է Ʈ ݺ(input stream iterator)

ǥ ̺귯 Է Ʈ Է ݺڷ ȯϴ istream_iterator Ѵ.

, 4 ø ڰ ʿѵ,

Ÿ, Ʈ Ÿ, trait Ÿ, ׸ Ұ Ÿ ϴ Ÿ ʿϴ.

2 Ʈ char_traits ptrdiff_t .

istream_iterator ڷ ڴ ϳε, ̴ Ʈ Ÿ.

 

Է Ʈ ݺڿ (++)

Ʈκ ο о(>> ) Ѵ. (*) Ͽ ִ.

 

ex)        istream_iterator<int, char> intstream(cin), eof;

            istream_iterator<int, char>::iterator where =

                        find(intstream, eof, 7);

 

3.2 Ʈ ݺ(output stream iterator)

Ʈ ݺ(output stream iterator),

ݺڷ Ե >> ڸ Ͽ ش Ʈ Ѵ.

Ʈ ݺڸ ϱ ؼ ڷ Ʈ ؾ Ѵ.

ι° ڴ µǴ иڷ ڿ Ÿ.

 

ex)        copy(newdata.begin(), newdata.end(), ostream_iterator<int, char>(cout, " "));

 

4. ݺ(insert iterator)

ݺڿ 3 ° ִµ, ̵ ٲٴµ ȴ.

Ϲ · inserter() ִ.

inserter ΰ ڸ ϴµ, ̳ʿ ̳ʳ ݺڰ װ, ҵ ̳ Ư ġ Ѵ.

front_inserter() Ͽ ݺڴ ش ̳ ʿ Ѵ.

back_inserter() ݺڴ ش ̳ ʿ Ѵ.

ΰ list deque Բ , set vector .

 

void main()

{

            int threeToOne[] = {3, 2, 1};

            int fourToSix[] = {4, 5, 6};

            int sevenToNine[] = {7, 8, 9};

 

            list<int> aList;

 

            copy(threeToOne, threeToOne+3, front_inserter(aList));          // first insert into the front

 

            copy(sevenToNine, sevenToNine+3, back_inserter(aList));        // then insert into the back

 

            // find the seven, and insert into middle

            list<int>::iterator seven = find(aList.begin(), aList.end(), 7);

            copy(fourToSix, fourToSix+3, inserter(aList, seven));

 

            // copy result to output

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

            cout << endl;

}

 

5. ݺ (iterator operation)

ǥ ̺귯 ݺڸ ٷµ Ǵ ΰ Լ Ѵ.

advance() Լ ϳ ݺڿ ϳ ڰ ڷ ޾Ƽ, ݺڸ ־ ŭ ̵Ų.

 

            void advance(InputIterator& iter, Distance& n);

 

ι° Լ distance() ݺڰ Ÿ ȯѴ.

 

            void distance(InputIterator first, InputIterator last, Distance& n);

 

3. Լ(function) (predicate)

 

1. Լ(function)

ǥ ̺귯 ϴ ˰ ߿ Լ ڷ 䱸ϴ ͵ .

, for_each(), transform() ˰ ̳ʿ ҿ ڷ Ѱܹ Լ Ѵ.

 

2. (predicate)

(predicate) Ұ(true/false) Ǵ ϴ Լ̴.

 

            bool isLeapYear(unsigned int year)   // 'year' ̸ Ѵ.

            {

            int flag = false;

 

            if ((year % 4) == 0) {       // 4 ̰,

                        flag = true;

                        if((year % 100) == 0) {     // 100 ƴϰ,

                                     flag = false;

                                     if((year % 400) == 0) {     // 400

                                                 flag = true;

                                     }

                        }

            }

            return flag;

            }

 

3. Լ ü(function object)

Լ ü(function object) ȣ ڸ Լ ִ Ŭ ü̴.

Ϲ Լ ſ Լ ü ϴ .

ù°, ο Լ ʰ ǥ ̺귯 Ǵ Լü ϰ,

°, ζ Լ ȣν ӵ Ű ϸ,

, Լü Ͽ ڽ ִ ϰ, ֵ Ѵ.

ǥ ̺귯 Ǵ Լü .

Լ(arithmetic function)

            plus(), minus(), multiplies(), divides(), modulus(), negate()        // +, -, *, /, %, ~

Լ(comparison function)

            equal_to(), not_equal_to(), greater(), less(), greater_equal(), less_equal()          // ==, !=, >, <, >=, <=

Լ(logical function)

            logical_and(), logical_or(), logical_not()         // &&, ||, !

 

4. Լ (function adaptor)

Լ (function adaptor)

Լ Լü ֵ Լ Ǵ Լ ٲִ Ŭ ü̴.

Լ ʹ Լ Ǵ Լ ڷ ϴ ڸ ִ.

 

5. (negator) δ(binder)

(negator) δ(binder)

ִ Լ üκ ο Լ ü µ Ǵ Լ ̴.

̵ ٸ Լ generic ˰ ȣϱ⿡ ռ

Ʈ ϴ Ϻη Ǵ ̴.

 

not1() not2() װ ü ڷ ޾Ƶ鿩,

ݴ밪 ο Լ ü Ѵ.

 

δ ΰ ڸ Լ ޾Ƽ,

ù° ڳ ι° ڸ Ư ε Ѱ ڸ Լ .

, δ ڷ Ѿ Լ binary_function Ŭ Ŭ ؾ Ѵ.

 

4. ̳(container) Ŭ

 

1.

ǥ ̺귯 10 ̳ʸ Ѵ.

̸

Ư¡

vector

ϸ, ڿ

list

ġ ԰

deque

ϸ, հ ڿ

set

ҵ ϸ, ҼӰ˻ ,

multiset

ߺ ϴ set

map

Ű ϸ, ԰

multimap

ߺŰ ϴ map

stack

top ԰

queue

ʿ, ʿ

priority_queue

ū ٰ

 

 

2. ̳ ϱ

1) ݷdz ϴ°?

ʿϴٸ, vector deque ϶

 

2) ݷdz ű ΰ?

̳ʰ Ǵ Ǿ Ѵٸ, set ؾ Ѵ.

set ϸ ڵ ° .

ݸ鿡, ѽ ߿õȴٸ, list vector ϰ,

ش ̳ʸ ϴ ϴ.

ͱ Ǵ ϴ ִٸ,

stack̳ queueǴ list ֻ ̴.

 

3) ߿ ũⰡ ϰ ϴ°?

׷ٸ, list set ϴ .

vector deque ݷκ ҵ ڿ Ŀٶ ۸ ϰ ִ.

, ݷ ũⰡ Ǿ ִٸ, vector deque

Ҹ ϰ ִ list setٴ ޸𸮸 Ѵ.

 

4) ݷ ũ⸦ ִ°?

vector ϴ ũ ޸ ̸ Ҵ ִ(reserve() Լ ).

̴ ٸ ̳ʿ ̴.

 

5) ݷdz ԵǾ ִ Ȯϴ°?

׷ٸ, set̳ map ϴ ڴ.

set̳ map ̳ʿ ִ ˻ϴ ſ ,

ٸ ݷǿ ̸ ˻ϱ ؼ ̳ʿ ҵ ؾ Ѵ.

 

6) ݷǿ ε Ѱ? ٽø, ݷ Ű

ִ°?

Ű 0 Ѽ ̶, vector deque ؾ Ѵ.

ݸ鿡, Ű ִ ̶(ڳ ڿ Ǵ Ÿ԰ ) map Ѵ.

 

7) ִ°?

ǥ ̺귯 ϴ ̳ʿ ٸ ־ Ѵ.

, ̳ʰ less-than ڸ ʿ .

׷, less-than ڸ ؼ set̳ map ϸ ȵȴ.

 

8) ݷκ ū ãƼ ϴ Ͼ°?

׷ٸ, priority_queue ּ ̴̳.

 

9) ġ Եǰ ŵǴ°?

߰뿡 Եǰ ŵȴٸ, list ּ ̴.

ʿ Եȴٸ, deque list ڰ,

԰ ̷ٸ, stack̳ queue ̴.

 

10) ΰ ̻ ϳ ġ Ͼ ΰ?

׷ٸ, set̳ list ̴.

߿ Ǵ° ο ϸ ȴ.

set ġ ſ ȿ ̴.

ݷ ʰ, list ϴ ȿ splice() Լ ϸ,

list ϴ ڴ. ֳϸ, ٸ ̳ʿ ʴ ̱ ̴.

 

3. STL ̳ ŸԵ

ǰ ִ ̳ ŸԵ ǥ ̺귯 ͵ ִ.

κ , ̹ ̳ʵ ϰ ̿ϸ

پϰ ֱ ̴.

 

tree ݷ . ׷, set Ÿ ˻Ʈ ¸ Ͽ Ǿ ִ.

. ׷, vector ҷ vector ִ.

׷ . ׷, graph ǥ ٸ ҷ ϴ ִ.

sparse . ذå graph ǥ ְڴ.

ؽ ̺ . ׷, ؽ ̺ list set ҷ ϴ vector deque ִ.

 

5. vector

 

1. vector

vector ̳ Ŭ C ϹȭŲ ̴.

, vector ÷ڿ ϴ.

vector ִ.

-> vector ũ ִ. ο Ҹ vector ̳ ߰ ִ.

            ޸ ȿ̰ ڵ ٷ.

            ׷, vector ߰ Ҹ listó ȿ ʴٴ ָؾ Ѵ.

            Ѵٸ, vectorſ list ϴ .

 

vector deque ε ڷᱸ̴.

ū deque ̳ հ ȿ ݸ鿡,

vector ȿ̶ ̴.

Ϲ vector ϴ ȭ ִ ݸ鿡,

deque Ǵ α׷ Ѵ.

 

vector Ϸ vector ȭ ԽѾ Ѵ.

            #include <vector>

 

2. vector ϴ Լ

1) ʱȭ

vector ø Ŭ̹Ƿ Ÿ ־ Ѵ.

            ex) vector<int> vec_one(10);

 

2) Ÿ

            ex) vector<int>::iterator location;

 

3) Լ...

assign()

swap()

at() : ÷ ڿ ڸ ȯѴ.

front() : vector տ ִ Ҹ ȯ

back() : Ҹ ȯ

 

size() : vector ִ

capacity() : ο ޸ Ҵ vector Ȯ ִ ִ

max_size() : vector ִ Ѽ

reserve()

empty()

resize()

push_back(), pop_back(), insert(), erase()

begin(), end(), rbegin(), rend()

find(), count()

sort(), merge()

 

cf) generic ˰

̸

fill

־ ʱⰪ ͸ ä.

copy

Ѵ.

generate

߻(generator) Ϳ ִ´.

find

ϴ Ҹ ã´.

adjacent_find

ߺ Ҹ ã´.

search

ͳ ã´.

max_element, min_element

ִ Ǵ ּ Ҹ ã´.

reverse

´.

replace

ҵ ο ġѴ.

rotate

߽ ҵ ȯŲ.

partition

ҵ α׷ ɰ.

next_permutation

(permutation) Ѵ.

inplace_merge

ͳ պ?

random_shuffle

ͳ ҵ Ƿ ´.

count

ϴ ҵ .

accumulate

ͷκ ϳ .

inner_product

Ѵ.

equal

κ͸ ѽ־ Ͽ ˻Ѵ.

lexicographical_compare

transform

Ϳ ȯ Ѵ.

partial_sum

κ Ѵ.

adjacent_difference

̿ϴ Ѵ.

for_each

ҵ鿡 Լ Ѵ.

 

 

3. vector<bool>

flip() : ȣǾ , Լ Ʈ ´.

swap()

 

6. list

 

1. list

list ʰ ʿ ο ϰų ,

ݺڷ ġ Ͽ list ߰ ҵ ߰ϰų ִ.

list ̴. list ҵ ÷ڿ ؼ Ұϰ,

ҵ Ϸ ȸؾ Ѵ.

 

list ݵ list ȭ ؾ Ѵ.

            #include <list>

 

2. list ϴ Լ

1) list ʱȭ

            ex) list<int> list_one;

 

2) Ÿ

            ex) list<int>::iterator location;

 

3) Լ...

push_back(), push_front(), insert(), splice(), merge(), splice()

pop_front(), pop_back(), erase(), remove(), remove_if(), unique()

front(), back(), begin(), end(), rbegin(), rend()

size(), empty(), resize()

sort()

 

7. stack queue

 

1.

stack δ, å ö̳ õ ִ.

, stack ŵǴ ֱٿ stack ߰ ̴.

, queue ̳ ȭ տ ִ.

queue ڿ ̷( ó),

queue տ ̷( ȭ ó).

queue stack ݴ̴.

queue queue ־ ̴.

ǥ ̺귯 stack queue (adaptor)̰,

̵ ϴµ ϴ ̳ʸ .

stack vector, list, dequeκ , queue list dequeκ .

 

2. stack

stack ü ǵǾ ִ.

empty()           stack (true) ȯ

size()            stack ȯ

top()             stack ġ Ҹ ȯ( ʴ´)

push(newElement)  stack Ҹ ߰

pop()             stack ġ Ҹ (ȯ ʴ´)

 

front Ҹ ϴ ϴ ȴٴ ؾ Ѵ.

stack ϴ α׷ stack عħϰ ִ ̳ Ÿ( , vector) ȭ Ӹƴ϶

stack ȭ ؾ Ѵ.

            #include <stack>

            #include <vector>

 

3. stack ʱȭ

stack ΰ ڸ ؾ Ѵ.

ϳ stack Ե Ÿ̰, ϳ ҵ µ ̴̳.

stack ̳ʷ vector deque , list ̳ʷ ִ.

deque ϸ , vector ϸ ũⰡ ۾.

            stack<int, vector<int> > stackOne;

            stack<double, deque<double> > stackTwo;

            stack<Part*, list<Part* > > stackThree;

 

4. queue

queue Ϲ üμ ǵȴ.

empty()           queue (true) ȯ

size()            queue ȯ

front()           queue տ ġ Ҹ ȯ( ʴ´)

back()            queue ڿ ġ Ҹ ȯ

push(newElement)  queue ڿ Ҹ ߰

pop()             queue տ ġ Ҹ (ȯ ʴ´)

 

queue Ǿտ ġ Ҹ ϴ ϴ ȴٴ ؾ Ѵ.

queue ݺڸ ʱ , generic ˰ queue .

queue ϴ α׷ queue ̳ Ÿ( , list) ϻӸ ƴ϶,

queue ȭ ؾ Ѵ.

            #include <queue>

            #include <list>

 

5. queue ʱȭ

queue ִ ̳ʿ Ÿ ؾ Ѵ.

queue ̳ Ÿ list deque Ѵ.

list ϸ ڵ尡 ۾ ݸ鿡, deque ϸ ڵ尡 .

queue ϴ ̴.

            queue<int, list<int> > queueOne;

            queue<double, deque<double> > queueTwo;

            queue<Part*, list<Part* > > queueThree;