C++ STL

2023. 1. 30. 22:43ใ†๐Ÿ’ป C++/C++ TIL

๋ฐ˜์‘ํ˜•

STL ์€ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ฐ์ฒด๋ฅผ ๋‹ด๋Š” '์ปจํ…Œ์ด๋„ˆ', ๊ฐ์ฒด์— ์ ‘๊ทผํ•˜๋Š” '๋ฐ˜๋ณต์ž', ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃจ๋Š” '์•Œ๊ณ ๋ฆฌ์ฆ˜'์œผ๋กœ ์ด๋ฃจ์–ด์กŒ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

STL ์ปจํ…Œ์ด๋„ˆ ์ข…๋ฅ˜(container)

์„ ํ˜• ์ปจํ…Œ์ด๋„ˆ

  • array
    • #include <array>
  • vector
    • #include <vector>
  • list
    • #include <list>
  • forward_list
    • #include <forward_list>
  • deque
    • #include <deque>

์ปจํ…Œ์ด๋„ˆ ์–ด๋‹ตํ„ฐ

  • stack
    • #include <stack>
  • queue
    • #include <queue>
  • priority_queue
    • #include <queue>

์—ฐ๊ด€ ์ปจํ…Œ์ด๋„ˆ

  • set (tree)
    • #include <set>
  • map (tree)
    • #include <map>
  • unorder_set (hash table)
    • #include <unorder_set>
  • unorder_map (hash table)
    • #include <unorder_map>

 

STL ์ปจํ…Œ์ด๋„ˆ์˜ ๊ณตํ†ต์ ์ธ ํŠน์ง•

1. ๋Œ€๋ถ€๋ถ„ ์ปจํ…Œ์ด๋„ˆ์˜ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๊ฐ€ ๋™์ผํ•˜๋‹ค

STL์˜ ์žฅ์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š๊ณ  ์ปจํ…Œ์ด๋„ˆ ๊ต์ฒด๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค

 

์•ž์— ๋„ฃ๊ธฐ / ๋นผ๊ธฐ

  • push_front()
  • pop_front()
  • front()

๋’ค์— ๋„ฃ๊ธฐ / ๋นผ๊ธฐ

  • push_back()
  • pop_back()
  • back()

์ค‘๊ฐ„์— ๋„ฃ๊ธฐ / ๋นผ๊ธฐ

  • insert()
  • erase()

์ฃผ์˜) vector ๋Š” push_front() ๊ฐ€ ์—†์Œ!

(์ž๋ฃŒ๊ตฌ์กฐ ํŠน์„ฑ์ƒ ์•ž์ชฝ์— ์š”์†Œ๋ฅผ ๋„ฃ๋Š” ๊ฒƒ์ด ์„ฑ๋Šฅ์— ์ข‹์ง€ ์•Š์œผ๋ฏ€๋กœ...)

 

2. ์ œ๊ฑฐ์™€ ๋ฐ˜ํ™˜์„ ๋™์‹œ์— ํ•˜์ง€ ์•Š๋Š”๋‹ค

pop_front() / pop_back() ํ•จ์ˆ˜๋Š” ์ œ๊ฑฐ๋งŒ ํ•˜๊ณ ,

front() / back() ํ•จ์ˆ˜๋Š” ๋ฐ˜ํ™˜๋งŒ ํ•œ๋‹ค!

 

 

์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•

#include <iostream>
#include <list>
#include <vector>


int main() {
	std::list<int> s1;
	std::list<int> s2(10); // 10๊ฐœ๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”
	std::list<int> s3(10, 3); // 10๊ฐœ๋ฅผ 3์œผ๋กœ ์ดˆ๊ธฐํ™”
	std::list<int> s4{10, 3}; // 2๊ฐœ๋ฅผ 10, 3์œผ๋กœ ์ดˆ๊ธฐํ™”
	std::list<int> s5 = {10, 3};
	std::list<int> s6 = {1,2,3,4,5,6,7};
	std::list<int> s7{1,2,3,4,5,6,7};
	std::list s8 = {1,2,3,4,5,6,7}; // std::list<int> ๋กœ ์ดˆ๊ธฐํ™” C++17 ๋ถ€ํ„ฐ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
}

 

 


 

์˜ˆ์ œ

  1. 1~10์œผ๋กœ ์ดˆ๊ธฐํ™”๋œ vector ๋งŒ๋“ค๊ธฐ
  2. push_back ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋์— 20์„ ์ถ”๊ฐ€ํ•˜์„ธ์š”
  3. ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ถœ๋ ฅํ•ด ๋ณด์„ธ์š”
    1.  [ ] ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ - vector, deque๋Š” ๊ฐ€๋Šฅํ•˜์ง€๋งŒ list๋Š” ๋ถˆ๊ฐ€๋Šฅ
    2. range for ์‚ฌ์šฉ (for (auto &n : v)) - list ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    3. ๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉ
  4. ์ •๋ ฌํ•˜๊ณ  ํ•ฉ์„ ๊ตฌํ•˜๊ณ  ๊ฒ€์ƒ‰,๋“ฑ ๋ณด๋‹ค ๋งŽ์€ ์ž‘์—… ํ•˜๋ ค๋ฉด ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ ์•Œ์•„์•ผ ํ•œ๋‹ค
#include <iostream>
#include <vector>

int main() {
	std::vector<int> v = {1,2,3,4,5,6,7,8,9,10};

	v.push_back(20);

	for (int i = 0; i <v.size(); i++) {
		std::cout << v[i] << std::endl;
	}

	for(auto& n : v) {
		std::cout << n << std::endl;
	}

}

 

 

 

 

STL ๋ฐ˜๋ณต์ž (iterator)

๋ฐฐ์—ด ์š”์†Œ์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•

  • ๋ฐฐ์—ด์˜ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉ
  • ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ ‘๊ทผ
#include <iostream>
#include <vector>
#include <list>

int main() {
	int x[10] = {1,2,3,4,5,6,7,8,9,10};
	int *p1 = x;
	++p1;
	*p1 = 20;
}

๋ฐ˜๋ณต์ž (iterator) ๋ž€?

  • ํฌ์ธํ„ฐ์™€ ์œ ์‚ฌํ•˜๊ฒŒ ๋™์ž‘ํ•˜๋Š” ๊ฐ์ฒด๋กœ์„œ, ๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ปจํ…Œ์ด๋„ˆ์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜๋ณต์ž์˜ ์žฅ์ 

  • ์ปจํ…Œ์ด๋„ˆ์˜ ๋‚ด๋ถ€ ๊ตฌ์กฐ(๋ฉ”๋ชจ๋ฆฌ ๊ตฌ์กฐ)์— ์ƒ๊ด€์—†์ด ๋™์ผํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์š”์†Œ์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค
#include <iosteram>
#include <vector>
#include <list>

int main() {
	std::list<int> v = {1,2,3,4,5,6,7,8,9,10};
	auto p2 = v.begin();
	++p2;
	*p2  20;
	++p2;
	std::cout << *p2 << std::endl;
}

 

 

๋ฐ˜๋ณต์ž์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…

์ปจํ…Œ์ด๋„ˆ ์ด๋ฆ„ :: iterator

ex) std::vector<int>::iterator

 

#include <iostream>
#include <vector>
#include <list>


int main() {

	std::vector<int> v = {1,2,3,4,5,6,7,8,9,10};
	// std::vector<int>::iterator p1 = v.begin();
	auto p1 = v.begin(); // 1์„ ๊ฐ€๋ฆฌํ‚ด

	auto p2 = v.end(); // end๋Š” 10์„ ๊ฐ€๋ฆฌํ‚ค๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ 10 ๋‹ค์Œ์„ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋จ
}

์ฃผ์˜) end() ๋กœ ์–ป์€ ๋ฐ˜๋ณต์ž๋Š” ์ปจํ…Œ์ด๋„ˆ์˜ ๋งˆ์ง€๋ง‰ ๋‹ค์Œ์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค

  • past the end
  • end() ๋กœ ์–ป์€ ๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์š”์†Œ์— ์ ‘๊ทผํ•˜๋ฉด ์•ˆ๋œ๋‹ค
  • ๋์— ๋„๋‹ฌํ–ˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
while(p1 != p2) {
	std::cout << *p1 << std::endl;
	++ p1;
}

 

 

๋ฐ˜๋ณต์ž๋ฅผ ๊บผ๋‚ด๋Š” ๋ฐฉ๋ฒ•

#include <iostream>
#include <vector>
#include <list>

int main() {
	// std::list<int> v = {1,2,3,4,5,6,7,8};
	int v[10] = {1,2,3,4,5,6,7,8};
	// auto p1 = v.begin();
	// auto p2 = v.end();
	auto p1 = std::begin(v);
	auto p2 = std::end(v);

	while(p1 != p2) {
		std::cout << *p1 << std::endl;
		++p1;
	}
}

1. ๋ฉค๋ฒ„ ํ•จ์ˆ˜ ์‚ฌ์šฉ

v.begin() / v.end()

** ๋ฐฐ์—ด์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค

 

2. ์ผ๋ฐ˜ ํ•จ์ˆ˜ ์‚ฌ์šฉ (C++11 ๋ถ€ํ„ฐ)

std::begin(v) / std::end(v)

** ๋ฐฐ์—ด์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค

 

 

์ปจํ…Œ์ด๋„ˆ์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์—ด๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•

1. ๋ฐฐ์—ด ์—ฐ์‚ฐ์ž ([])

์—ฐ์†๋œ ๋ฉ”๋ชจ๋ฆฌ์™€ ์œ ์‚ฌํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„ ๊ฒฝ์šฐ๋งŒ ๊ฐ€๋Šฅ

vector , array, deque, string

  • list ๋‚˜ forward_list ๋Š” [] ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ
for (int i = 0; i < v.size(); i++) {
	std::cout << v[i] << std::endl;
}

๋ฐฐ์—ด์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋„๋ก v.size() ๋Œ€์‹  std::size(v) ์‚ฌ์šฉ

 

 

2. ๋ฐ˜๋ณต์ž ์‚ฌ์šฉ

๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

auto p1 = std::begin(v);

while(p1 != std::end(v)) {
	std::cout << *p1 << std::endl;
	++p1;
}

 

 

3. range for ๊ตฌ๋ฌธ ์‚ฌ์šฉ

๋ชจ๋“  ์ปจํ…Œ์ด๋„ˆ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

for (auto &n : v) {
	std::cout << n << std::endl;
}

๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“ค์ง€ ์•Š๊ธฐ ์œ„ํ•ด & ๋ฅผ ๋ถ™์—ฌ์ฃผ์ž

 

 

 

 

 


STL ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํŠน์ง•

์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด๋ž€?

์–ด๋–ค ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์ ˆ์ฐจ, ๋ฐฉ๋ฒ•, ๋ช…๋ น์–ด ๋“ค์˜ ์ง‘ํ•ฉ

STL ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด๋ž€?

C++ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ STL ์ด ์ œ๊ณตํ•˜๋Š” ํ•จ์ˆ˜

  • ์ •๋ ฌ, ๊ฒ€์ƒ‰, ์ˆœ์—ด, ๋“ฑ ๋‹ค์–‘ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„ํ•œ ํ•จ์ˆ˜์˜ ์ง‘ํ•ฉ

STL ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ํŠน์ง•

  • ๋ฉค๋ฒ„ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ์ผ๋ฐ˜ ํ•จ์ˆ˜ (ํ…œํ”Œ๋ฆฟ) ์œผ๋กœ ์ œ๊ณต ๋œ๋‹ค

→ ํ•˜๋‚˜์˜ ํ•จ์ˆ˜(ํ…œํ”Œ๋ฆฟ)์œผ๋กœ ๋‹ค์–‘ํ•œ ์ปจํ…Œ์ด๋„ˆ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค (์ž๋ฃŒ๊ตฌ์กฐ์— ๋…๋ฆฝ์ )

→ ๋Œ€๋ถ€๋ถ„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ํ•จ์ˆ˜์˜ ์ธ์ž์™€ ๋ฐ˜ํ™˜ ํƒ€์ž…์œผ๋กœ ๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉ

๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด, ์ปจํ…Œ์ด๋„ˆ์˜ ๋‚ด๋ถ€ ๊ตฌ์กฐ์— ์ƒ๊ด€์—†์ด ๋™์ผํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์š”์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค

  • <algorithm> ํ—ค๋”๊ฐ€ ํ•„์š”ํ•˜๋‹ค
  • ๋Œ€๋ถ€๋ถ„์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋ฐ˜๋ณต์ž ๋ฅผ ํ•„์š”๋กœ ํ•œ๋‹ค

 

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

int main() {
	std::list<int> s = {1,2,3,4,5,6,7,8,9,10};
	std::vector<int> v= {1,2,3,4,5,6,7,8,9,10};

	// s.find(5); X

	std::find(begin(v), end(v), 3);

}

 

 

๊ฒ€์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (find)

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

int main() {
	int x[10] = {1,2,3,4,5,6,7,8,9,10};
	std::list<int> s = {1,2,3,4,5,6,7,8,9,10};
	std::vector<int> v= {1,2,3,4,5,6,7,8,9,10};

	// auto p = std::find(begin(v), end(v), 3);
	auto p = std::find(x, x+10, 3); // x+9 ๊ฐ€ ์•„๋‹ˆ๋ผ x์˜ ๋‹ค์Œ์ฃผ์†Œ์ธ x+10 ๋„ฃ์–ด์•ผ ํ•œ๋‹ค

}
  • 2๊ฐœ์˜ ๋ฐ˜๋ณต์ž ([first, last), ๊ตฌ๊ฐ„) ์™€ ๊ฒ€์ƒ‰ํ•  ๊ฐ’์„ ๋ฐ›์•„ ์„ ํ˜•๊ฒ€์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
    • first ๋Š” ๊ฒ€์ƒ‰ ๋Œ€์ƒ์— ํฌํ•จ๋˜์ง€๋งŒ, last๋Š” ๊ฒ€์ƒ‰ ๋Œ€์ƒ์— ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค

๋ฐ˜ํ™˜๊ฐ’

๊ฒ€์ƒ‰ ์„ฑ๊ณต์‹œ - ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐ˜๋ณต์ž

์‹คํŒจ์‹œ - last // null ์•„๋‹˜!

if ( p == end(v)) {
	// ์‹คํŒจ
} else {
	// ์„ฑ๊ณต
    std::cout << *p << std::endl;
}

 

 

STL ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์กฐ๊ฑด์ž

#include <iostream>
#include <algorithm>
#include <vector>

int main() {
	std::vector <int> v = {10,9,8,7,6,5,4,3,2,1};
	// v ์•ˆ์—์„œ ์ฒ˜์Œ ๋‚˜์˜ค๋Š” 3์˜ ๋ฐฐ์ˆ˜๋ฅผ ๊ตฌํ•ด๋ณด์„ธ์š”
	auto p = std::find(begin(v), end(v), 3);
	std::cout << *p << std::endl;

}

find_if ([first, last),ํ•จ์ˆ˜)

bool foo(int n) {
	return n % 3 = 0;
}


...

auto p = std::find_if(begin(v), end(v), foo);

 

 

 

์กฐ๊ฑด์ž predicator

  • bool ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, ํ•จ์ˆ˜ ๊ฐ์ฒด, ๋žŒ๋‹ค ํ‘œํ˜„์‹
  • _if ๋กœ ๋๋‚˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ์ „๋‹ฌ๋˜์–ด ์ •์ฑ…์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค
auto p = std::find_if(begin(v), end(v), [](int n) { return n % 3 == 0;});

 

 

 

๋”๋งŽ์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ข…๋ฅ˜๋ฅผ ๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด?

cppreference.com algorithm library ์ฐธ์กฐ

 

 

 

 


์˜ˆ์ œ

  1. -1์„ ์ž…๋ ฅํ•  ๋•Œ ๊นŒ์ง€ ์ž…๋ ฅ์„ ๋ฐ›์•„ vector์— ๋ณด๊ด€ํ•ด๋ผ → push_back ์‚ฌ์šฉ
  2. ์ž…๋ ฅํ•œ ๊ฐ’ ์ค‘ 10 ๋ณด๋‹ค ํฐ ๊ฐ’์€ ๋ชจ๋‘ 0 ์œผ๋กœ ๋ณ€๊ฒฝํ•ด๋ผ → replace / replace_if ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์šฉ
  3. ๋ชจ๋“  ์š”์†Œ์˜ ํ•ฉ์„ ๊ตฌํ•ด์„œ ์ถœ๋ ฅํ•ด๋ผ → accumulate ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์šฉ (numeric ํ—ค๋”์— ์กด์žฌ)
  4. vector์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ(sort) ํ•ด์„œ ์ถœ๋ ฅํ•˜์„ธ์š” → sort ์•Œ๊ณ ๋ฆฌ์ฆ˜ + ์กฐ๊ฑด์ž ์‚ฌ์šฉ
  5. vector์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ 1๋กœ ์ฑ„์›Œ์„œ ์ถœ๋ ฅํ•ด๋ณด์„ธ์š” → fill ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์šฉ

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>


using namespace std;

int main() {
	vector<int> v;
	int n = 0;

	while(1) {
		cin >> n;
		if (n == -1) break;
		v.push_back(n);
	}

	for (auto &n : v) {
		if (n >= 10) n = 0;
	}
	// replace ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์šฉ
	replace_if(begin(v), end(v), [](int n) { return n>=10; }, 0);

	// ๋ชจ๋“ ์š”์†Œ์˜ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ -> accumulate ์•Œ๊ณ ๋ฆฌ์ฆ˜
	int sum = accumulate(begin(v), end(v), 0);

	// sort (begin(v), end(v)); // ์˜ค๋ฆ„์ฐจ์ˆœ, ํ€ต์†ŒํŠธ
	sort(begin(v), end(v), [](int a, int b) { return a > b; }); // ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
	
	// ๋ชจ๋“  ์š”์†Œ 1๋กœ ์ฑ„์šฐ๊ธฐ
	fill(begin(v), end(v), 1);

}
๋ฐ˜์‘ํ˜•