[따배C++] 16. 표준 템플릿 라이브러리

Created: June 6, 2021 11:27 AM
Tag: algorithms, containers, iterators, standard template libraries

16.1 표준 템플릿 라이브러리, 컨테이너 소개 standard template libraries, containers


#include <iostream>
#include <deque>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
using namespace std;

void sequence_containers()
{
	// vector
	{
		vector<int> vec; // #include <vector>
		for (int i = 0; i < 10; ++i)
			vec.push_back(i);

		for (auto &e : vec)
			cout << e << " ";
		cout << endl;
	}

	// deque
	{
		deque<int> deq; // #include <deque>
		for (int i = 0; i < 10; ++i)
		{
			deq.push_back(i);
			deq.push_front(i);
		}

		for (auto &e : deq)
			cout << e << " ";
		cout << endl;
	}
}

void associative_containers()
{
	// set(집합): 원소가 중복이 되지 않는다
	{
		set<string> str_set;

		str_set.insert("Hello"); // 컨테이너 마다 넣는 방법이 다르다
		str_set.insert("World");
		str_set.insert("Hello");

		cout << str_set.size() << endl; // result 2

		for (auto &e : str_set)
			cout << e << " ";
		cout << endl;
	}

	// multiset : duplication is allowed
	{
		std::multiset<string> str_set;

		str_set.insert("Hello");
		str_set.insert("World");
		str_set.insert("Hello");

		cout << str_set.size() << endl;

		for (auto &e : str_set)
			cout << e << " ";
		cout << endl;
	}

	// map: key/value (jason의 원리)
	{
		std::map<char, int> map;
		map['a'] = 10;
		map['b'] = 20;
		map['c'] = 50; // 순서를 바꾸어도 자동으로 정렬된다

		cout << map['a'] << endl;

		map['a'] = 100;

		cout << map['a'] << endl;

		for (auto &e : map)
			cout << e.first << " " << e.second << " ";
		cout << endl;
	}

	// multimap : duplicated keys
	{
		std::multimap<char, int> map;
		map.insert(std::pair('a', 10)); // before c++ 14, pair<char, int>('a', 10)
		map.insert(std::pair('b', 10));
		map.insert(std::pair('c', 10));
		map.insert(std::pair('a', 100));

		cout << map.count('a') << endl;

		for (auto &e : map)
			cout << e.first << " " << e.second << " ";
		cout << endl;
	}
}

void container_adapters()
{
	// stack
	{
		cout << "stack" << endl;

		std::stack<int> stack;
		stack.push(1); // push adds a 'copy'
		stack.emplace(2); // emplace constructs a new object
		stack.emplace(3);
		cout << stack.top() << endl;
		stack.pop();
		cout << stack.top() << endl;
	}

	// queue
	{
		cout << "queue" << endl;

		std::queue<int> queue;
		queue.push(1);
		queue.push(2);
		queue.push(3);
		cout << queue.front() << " " << queue.back() << endl;
		queue.pop();
		cout << queue.front() << " " << queue.back() << endl;
	}

	// priority queue -> sorting
	{
		cout << "priority queue" << endl;

		std::priority_queue<int> queue;

		for (const int n : {1, 8, 5, 6, 3, 4, 0, 9, 7, 2})
			queue.push(n);

		for (int i = 0; i < 10; ++i)
		{
			cout << queue.top() << endl;
			queue.pop();
		}
	}
}

int main(void)
{
	// container의 종류
	sequence_containers(); // 1.

	associative_containers(); // 2.

	containers_adapters(); // 3.

	return (0);
}

16.2 STL 반복자 소개 iterators


컨테이너에 저장되어 있는 맴버들에 한번씩 접근 시 공통적으로 사용할 수 있는 반복자에 대해 알아보자.

#include <iostream>
#include <vector>
#include <list>
#include <set>
#include <map> // 다양한 컨테이너에 반복자 적용 가능

using namespace std;

int main(void)
{
	vector<int> container;
	for (int i = 0; i < 10; ++i)
		container.push_back(i);

	for (auto itr = container.begin(); itr != container.end(); ++itr)
		cout << *itr << " ";
	cout << endl;

	return (0);
}

16.3 STL 알고리즘 소개 algorithms


#include <algorithm>
using namespace std;

int main(void)
{
	using namespace std;

	vector<int> container; // list에서도 거의 동일하게 작동
	for (int i = 0; i < 10; ++i)
		container.push_back(i);

	auto itr = li.begin();
	//list<int>::const_iterator itr; // <-
	auto itr = min_element(container.begin(), container.end());
	cout << *itr << endl;

	itr = max_element(container.begin(), container.end());
	cout << *itr << endl;

	cout << endl;

	itr = find(container.bein(), container.end(), 3);
	container.insert(itr, 128);

	for (auto &e : container) cout << e << " ";
	cout << endl;

	sort(container.begin(), container.end());
	//li.sort();

	for (auto &e : container) cout << e << " ";
	cout << endl;
}

좋은 웹페이지 즐겨찾기