생성일: 2022년 12월 14일 오후 11:13
최종 편집 일시: 2022년 12월 22일 오후 3:50
cpp notion
STL 사용하기
#include , templated container, iter .. extra..
ex00 Easy find
template 의존타입 dependent type
template <class C> // typename
typename C::iterator easyfind( C &container, int const value ){
C::iterator* iter;
// 타입이 아니라고 가정하여 값으로 생각한다. ?
// C에 iterator 데이터 멤버가 있다면 값 *(곱셈) iter; 의 연산이 될 수 있다.
}
template <class C> // typename
typename C::iterator easyfind( C &container, int const value ){
typename C::iterator *iter;
// typename C::iterator iter;
// 중첩 의존 타입의 모호성을 없애고 'typename'을 붙여 타입이라는 것을 알려주었다.
// 이 경우 '*' 는 포인터로 읽힐 것이다.
}
컨테이너 활용 및 사용시 주의사항
순차 컨테이너 Sequence Containers
std::array<T,N>
, std::vector<T>
, std::deque<T>
는 선형구조로 되어있다. C++11부터 추가된 std::forward_list<T>
는 단일 연결 리스트(single linked list)이며 std::list<T>
는 이중 연결 리스트(double linked list)이다. 리스트도 operator[] 를 지원하지만 선형구조의 무작위 접근(random access) 방식과는 다르고 성능이 좋지 않다.연관 컨테이너 Associative Containers
비정렬 연관 컨테이너 Unordered Associative Containers
컨테이너 어댑터 Container Adapters
std::deque<T>
로 구현된 std::stack<T>
, std::queue<T>
와 std::vector<T>
로 힙 자료구조를 사용할 수 있게 만든 std::priority_queue<T>
가 있다.이터레이터 Iterator
**ex01 Span**
최대 n개의 integer을 담을 수 있는 class를 만들어라
std::sort
멤버함수
void addNumber( int value );
// 템플릿으로 변경해야함
//void addRange( std::vector<int>::iterator begin, std::vector<int>::iterator end );
int shortestSpan( void );
int longestSpan( void ) const;
ex02 Mutated abomination
not iterable 한 std::stack
을 iterable 하도록 MutantStack
클래스를 만들어 보자.
template<
class T,
class Container = std::deque<T>
> class stack;
Container c | the underlying container |
---|
https://en.cppreference.com/w/cpp/container/deque/begin(C++11) | returns an iterator to the beginning (public member function) |
---|---|
https://en.cppreference.com/w/cpp/container/deque/end(C++11) | returns an iterator to the end (public member function) |
https://en.cppreference.com/w/cpp/container/deque/rbegin(C++11) | returns a reverse iterator to the beginning (public member function) |
https://en.cppreference.com/w/cpp/container/deque/rend(C++11) | returns a reverse iterator to the end (public member function) |