<algorithm>).tpp 파일에 구현해도 됨표준 템플릿 라이브러리
https://cplusplus.com/reference/stl/
여러 객체들을 보관하는 보관소
http://tcpschool.com/cpp/cpp_iterator_intro
https://ansohxxn.github.io/stl/chapter16-2/
컨테이너 원소에 접근할 수 있는 포인터 같은 객체
begin()과 end()로 반복자를 얻을 수 있음begin(): 첫 번째 원소를 가리키는 반복자 리턴end(): 마지막 원소 한 칸 뒤를 가리키는 반복자 리턴 (빈 벡터를 표현하기 위함)const_iterator (cbegin(), cend())reverse_iterator(rbegin(), rend())const_reverse_iterator(crbegin(), crend())const 반복자들은 C++11 부터 추가됨https://modoocode.com/225
컨테이너에 대해 반복자를 가지고 여러 작업을 쉽게 수행할 수 있도록 도와주는 라이브러리
http://tcpschool.com/cpp/cpp_container_sequence
배열처럼 객체들을 순차적으로 보관
- 일반적인 상황에서는 벡터 사용
- 중간에 원소를 추가하거나 삭제하는 일이 많고, 순차적인 접근만 하는 경우 리스트 사용
- 맨 처음과 끝에 원소를 추가하거나 삭제하는 일이 많으면 덱 사용
가변길이 배열
O(1)O(n)으로 늘어나지만 이렇게 수행되는 경우가 드물기 때문에 빠르다고 함O(n)으로 느림[]를 이용하거나 at() 함수 사용push_back() (임의 위치: insert())pop_back() (임의 위치: erase())size()capacity()// constructing vectors
#include <iostream>
#include <vector>
int main ()
{
// constructors used in the same order as described above:
std::vector<int> first; // empty vector of ints
std::vector<int> second (4,100); // four ints with value 100
std::vector<int> third (second.begin(),second.end()); // iterating through second
std::vector<int> fourth (third); // a copy of third
// the iterator constructor can also be used to construct from arrays:
int myints[] = {16,2,77,29};
std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
std::cout << "The contents of fifth are:";
for (std::vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
양방향 연결 리스트
[]나 at() 사용 불가)itr++, iter-- 등은 가능하지만 iter + 5처럼 임의의 위치에 접근하는 것도 불가능insert())하거나 제거(erase())할 때 앞 뒤 링크만 바꾸면 되므로 빠름 O(1)O(1)push_front()와 pop_front() 가능O(n)이지만 더 빠름함수 템플릿 easyfind() 구현
T, 두 번째 인자는 intT는 integer 컨테이너<algorithm>의 find()사용addNumber()를 여러번 추가하는 것을 방지할 수 있음Span 클래스N개의 정수를 저장N을 받음addNumber()Span에 하나의 숫자를 추가N개의 원소가 있는데 추가하려고 하면 예외 던지기shortestSpan(), longestSpan()<algorithm>의 min_element(), max_element(), sort(),<numeric>의 adjacent_difference() 활용insert()로 범위에 해당하는 값을 모두 추가할 수도 있음distance()로 구함int main(void)
{
Span sp = Span(5);
sp.addNumber(6);
sp.addNumber(3);
sp.addNumber(17);
sp.addNumber(9);
sp.addNumber(11);
std::cout << sp.shortestSpan() << std::endl;
std::cout << sp.longestSpan() << std::endl;
return (0);
}
$> ./ex01
2
14
$>
기존 컨테이너의 인터페이스를 제한하여 기능이 제한되거나 변형된 컨테이너
LIFO(후입선출) 방식의 구조
template<class T, Class C = deque<T> >
class std::stack {
protected:
C c;
public:
typedef typename C::value_type value_type;
typedef typename C::size_type size_type;
typedef C container_type;
explicit stack(const C& a = C()) : c(a){} // Inherit the constructor
bool empty() const { return c.empty(); }
size_type size() const { return c.size(); }
value_type& top() const { return c.back(); }
const value_type& top() const { return c.back(); }
void push(const value_type& n) { c.push_back(n); }
void pop() { c.pop_back(); }
};
empty() size() top() push() pop()// constructing stacks
#include <iostream> // std::cout
#include <stack> // std::stack
#include <vector> // std::vector
#include <deque> // std::deque
int main ()
{
std::deque<int> mydeque (3,100); // deque with 3 elements
std::vector<int> myvector (2,200); // vector with 2 elements
std::stack<int> first; // empty stack
std::stack<int> second (mydeque); // stack initialized to copy of deque
std::stack<int,std::vector<int> > third; // empty stack using vector
std::stack<int,std::vector<int> > fourth (myvector);
std::cout << "size of first: " << first.size() << '\n';
std::cout << "size of second: " << second.size() << '\n';
std::cout << "size of third: " << third.size() << '\n';
std::cout << "size of fourth: " << fourth.size() << '\n';
return 0;
}
std::stack을 iterable하게 만들기
MutantStack 클래스std::stack 상속하여 모든 멤버 함수 제공stack은 기반이 되는 컨테이너를 protected 멤버 변수로 가지기 때문에 이를 통해 구현int main(void) {
MutantStack<int> mstack;
mstack.push(5);
mstack.push(17);
std::cout << mstack.top() << std::endl;
mstack.pop();
std::cout << mstack.size() << std::endl;
mstack.push(3);
mstack.push(5);
mstack.push(737);
//[...]
mstack.push(0);
MutantStack<int>::iterator it = mstack.begin();
MutantStack<int>::iterator ite = mstack.end();
++it;
--it;
while (it != ite)
{
std::cout << *it << std::endl;
++it;
}
std::stack<int> s(mstack);
return (0);
}
MutantStack을 사용하든 std::list 같은 다른 컨테이너를 사용하든 결과가 같아야 함push() => push_back() 등)