๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“š ์ „๊ณต ๊ณต๋ถ€/๋ฌธ์ œํ•ด๊ฒฐ๊ธฐ๋ฒ•

[๋ฌธ์ œํ•ด๊ฒฐ๊ธฐ๋ฒ•] 1. C++

728x90

1. ์‚ฌ์šฉ ์–ธ์–ด

 

C : ๊ฐ€์žฅ ์ต์ˆ™ํ•˜์ง€๋งŒ ์ง€์› ๊ธฐ๋Šฅ์ด ์ ์Œ

C++ : STL์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์„ ํ™œ์šฉ

Java : java.util์— ์œ ์šฉํ•œ ๊ธฐ๋Šฅ์ด ๋งŽ๋‹ค. ์‹คํ–‰์‹œ๊ฐ„์ด ๋Š๋ ค์„œ ์†๋„์—์„œ ์ œํ•œ์„ ๋ฐ›์Œ

Python : ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด ์‰ฝ์ง€๋งŒ, ์‹คํ–‰์‹œ๊ฐ„์ด ๊ฐ€์žฅ ๋Š๋ฆฌ๋‹ค.

 

2. ๋ฌธ์ œ ํ•ด๊ฒฐ

-> ๊ธฐ๋ณธ์ ์œผ๋กœ ์ž๋ฃŒ๊ตฌ์กฐ, ์•Œ๊ณ ๋ฆฌ์ฆ˜, ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋Šฅ๋ ฅ์„ ๋ฌป๋Š” ๋ฌธ์ œ

 

3. auto

for(auto a : A){ ~ }

 

4. for : ๋ฐ˜๋ณต๋ฌธ

 

5. pair<int,int>

 

6. STL : standard template library

made by HP in 1994

์ฝ”๋“œ๋ฅผ ์งง๊ณ  ๋น ๋ฅด๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์คŒ.

 

7. template

template <typename T> const T& my_max(const T&x, const T&y){

    return (y<x)? x : y;

}

 

8. ๋ฐ˜๋ณต์ž Iterator

๋‹ค๋ฅธ ์ปจํ…Œ์ด๋„ˆ์— ๋Œ€ํ•ด์„œ ๋™์ผํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ• (ํฌ์ธํ„ฐ์˜ ์ผ๋ฐ˜ํ™”)

 

9. begin() : ์ฒซ ๊ฐ’์„ ๊ฐ€๋ฆฌํ‚ด , end() : ๋งˆ์ง€๋ง‰ ๊ฐ’์˜ ๋‹ค์Œ์„ ๊ฐ€๋ฆฌํ‚ด

 

list<int>::iterator it;

for(it = A.begin(); it!=A.end(); it++){  // A์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ถœ๋ ฅ

    cout << *it << "\n"; // iterator๋Š” ํฌ์ธํ„ฐ์ด๋ฏ€๋กœ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ ค๋ฉด *์„ ์‚ฌ์šฉ

}

 

ํ˜น์€ auto a : A ์‚ฌ์šฉ

ํ˜น์€ int i=0; i<A.size(); ++i ์‚ฌ์šฉ

 

10. Containers against ADT

ADT STL
List list
Stack vector
Queue deque
Dictionary map
Mathematical Set set

 

11. sequence containers : vector, deque, list

     associateive containers : set, multiset, map, multimap

 

12. Vector

array์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ ํฌ๊ธฐ๊ฐ€ ๋ฐ”๋€” ์ˆ˜ ์žˆ๋‹ค (๊ธธ์ด ์ง€์ •์ด ์ž์œ ๋กญ๋‹ค)

์ž๋™ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ, ๋‹ค์–‘ํ•œ ๋ฉ”์†Œ๋“œ ๋“ฑ์˜ ์žฅ์ 

Random access

๋ฒกํ„ฐ์˜ ๋๋ถ€๋ถ„์—์„œ ์‚ฝ์ž…/์‚ญ์ œ : ์ƒ์ˆ˜ ์‹œ๊ฐ„ O(1)

๊ทธ ์™ธ ์œ„์น˜์—์„œ ์‚ฝ์ž…/์‚ญ์ œ : ์„ ํ˜• ์‹œ๊ฐ„ O(n)

 

์ดˆ๊ธฐํ™”

vector<int> ve(5) : ํฌ๊ธฐ๊ฐ€ 5์ธ ๋นˆ ๋ฒกํ„ฐ ์„ ์–ธ {0,0,0,0,0}

vector<int> v(5,-1) : ํฌ๊ธฐ๊ฐ€ 5์ธ ๋ฒกํ„ฐ๋ฅผ -1๋กœ ์ฑ„์›€ {-1,-1,-1,-1,-1}

 

13. Deque(๋ฑ) : double ended queue

์‚ฝ์ž… ๋ฐ ์‚ญ์ œ ์ž‘์—…์ด ์–‘์ชฝ ๋์—์„œ ์ˆ˜ํ–‰๋˜๋Š” ์„ ํ˜• ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ

deque์˜ ์‚ฝ์ž… ๋ฐ ์‚ญ์ œ๋Š” ์–‘์ชฝ์—์„œ ๋ชจ๋‘ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ FIFO ๊ทœ์น™์„ ๋”ฐ๋ฅด์ง€ ์•Š์Œ

Circular Queue

Random access

push_front(), pop_front()

#include <deque>

 

์„ ์–ธ : deque<int> dq;

dq.push_front(1); dq.push_front(2); dq.push_front(3); // -> 3,2,1

dq.pop_front(); // -> 2,1

 

14. List

Doubly linked list

์ž„์˜์˜ ์œ„์น˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์—†๊ณ , ์•ž/๋’ค๋งŒ ์˜๋ฏธ (bidirectional iterator)

#include <list>

 

์„ ์–ธ : list<int> lst;

lst.push_back(1);

 

15. Set : ์ง‘ํ•ฉ

๋น„๊ต์—ฐ์‚ฐ์ž ๋˜๋Š” ๋น„๊ต ํ•จ์ˆ˜๊ฐ€ ๊ตฌํ˜„๋˜์–ด์•ผ ํ•จ (์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ )

#include <set>

 

set<int> S;

S.insert(1); S.insert(2); S.insert(3); // -> { 1, 2, 3 }

set<int>::iterator it;

it = S.find(2);

S.erase(it); // -> { 1, 3 }

 

#include <iostream>
#include <set>
using namespace std;

class Student{
	public :
    	double height, weight;
    bool operator<(const Student &s) const{
    	if(height != weight) return height < s.height;
        return weight < s.weight;
    }
};

int main(){
	set<Student> Sett;
    set<Student>::iterator it;
    Student e;
    
    e.height = 150; e.weight = 50;
    Sett.insert(e);
    e.height = 180; e.weight = 80;
    Sett.insert(e);
    
    for(it = Sett.begin(); it!=Sett.end(); ++it){
    	cout << it->height << " "<< it->weight << "\n";
    }
}

 

16. Map : (key, value) ์Œ์„ ์ €์žฅํ•จ

like dictionary, associative array, hash

๋น„๊ต์—ฐ์‚ฐ์ž ๋˜๋Š” ๋น„๊ต ํ•จ์ˆ˜๊ฐ€ ๊ตฌํ˜„๋˜์–ด์•ผ ํ•จ

#include <map>

 

map<Student, string> mp;

 

๋ฐ˜๋ณต๋ฌธ ์ถœ๋ ฅ : cout << it->first.height << it->first.weight << it->second;

 

 

17. ์šฐ์„ ์ˆœ์œ„ ํ Priority_queue

#include <queue>

 

priority_queue<Point> pq;

Point p;

 

p.x = 0; p.y = 1; pq.push(p);

p.x = 2; p.y = 3; pq.push(p);

 

while(!pq.empty()){

    p = pq.top();

    cout << p.x. << p.y;

    pq.pop();

}

 

 

18. sort

#include <algorithm>

๊ธฐ๋ณธ์€ ์˜ค๋ฆ„์ฐจ์ˆœ, ๋น„๊ตํ•จ์ˆ˜๋‚˜ ๋น„๊ต ํด๋ž˜์Šค ์„ ์–ธ ํ›„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

 

typedef struct {
    int x;
    int y;
} point;

class less_point{
    public :
    bool operator() (const point &p1, const point &p2){ // x ๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ, x๊ฐ€ ๊ฐ™์œผ๋ฉด y๊ธฐ์ค€ ์˜ค๋ฆ„์ฐจ์ˆœ
    if (p1.x != p2.x) return p1.x < p2.x;
    return p1.y < p2.y;
    }
};

int main(){
    vector<point> v(5);
    sort(v.begin(), v.end(), less_point());
}

 

19. lower_bound / upper_bound

#include <algorithm>

 

 

vector<int>::iterator low, hi;

sort(v.begin(), v.end()); // ์ •๋ ฌ

low = lower_bound(v.begin(), v.end(), 4);

hi = upper_bound(v.begin(), v.end(), 4);

cout << low - v.begin(); // ์œ„ ์ด๋ฏธ์ง€์—์„œ๋Š” 4. ํฌ์ธํ„ฐ๋ผ์„œ begin()์„ ๋นผ์ฃผ๋ฉด ์ธ๋ฑ์Šค๊ฐ€ ๋‚˜์˜จ๋‹ค. 4๊ฐ€ ์ฒ˜์Œ ๋‚˜์˜จ ์ธ๋ฑ์Šค

cout << hi - v.begin(); // 8. ๋งˆ์ง€๋ง‰ 4์˜ ๋‹ค์Œ ์ธ๋ฑ์Šค.

 

 

20. next_permutation & prev_permutation : ์ˆœ์—ด ๊ตฌํ•˜๊ธฐ

sort๋กœ ์ •๋ ฌ์„ ๋ฐ˜๋“œ์‹œ ํ•ด์ค˜์•ผ ์ˆœ์—ด์„ ๋น ์ง์—†์ด ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

// ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ ์™„๋ฃŒ ์‹œ
do{
   for(int i=0; i<3; ++i) cout << A[i];
} while(next_permutation(A, A + A.length());

// ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ ์™„๋ฃŒ ์‹œ
do{
   for(int i=0; i<3; ++i) cout << A[i];
} while(prev_permutation(A, A + A.length());

 

728x90