profile-img
์ง€๋ฐ์ด์˜ ํ‹ฐ์Šคํ† ๋ฆฌ
images/slide-image

๋“œ๋””์–ด ์ปจํ…Œ์ด๋„ˆ ์ฑ•ํ„ฐ๋กœ ๋„˜์–ด๊ฐ„๋‹ค !!

STL Sequence Container์€ ๊ฐ์ฒด๋ฅผ sequential oreder (์ˆœ์ฐจ์ ) ์œผ๋กœ ์ €์žฅํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ด๋‹ค

 

Sequence Containers Type of Iterator Supported description
vector (implemented as a dynamic array) Random Access ๋™์  ๋ฐฐ์—ด๋กœ ๊ตฌํ˜„๋œ ์ž๋ฃŒ๊ตฌ์กฐ, ๋ฐฐ์—ด๊ณผ ์œ ์‚ฌํ•œ ์„ ํ˜•๊ตฌ์กฐ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ์†์ ์œผ๋กœ ์ €์žฅํ•˜์—ฌ ๋™์ ์œผ๋กœ ํฌ๊ธฐ๋ฅผ ์กฐ์ ˆํ•  ์ˆ˜ ์žˆ์Œ. ๋ฐ์ดํ„ฐ ์‚ฝ์ž…, ์‚ญ์ œ๊ฐ€ ์žฆ์€ ๊ฒฝ์šฐ์—๋Š” ์ ์ ˆํ•˜์ง€ ์•Š์Œ.
list (implemented as a doubly-linked list) Bidirectional ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„๋œ ์ž๋ฃŒ๊ตฌ์กฐ, ๊ฐ ์š”์†Œ๊ฐ€ ์ด์ „ ์š”์†Œ์™€ ๋‹ค์Œ ์š”์†Œ์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ €์žฅํ•จ. ๋ฐ์ดํ„ฐ ์‚ฝ์ž… ์‚ญ์ œ๊ฐ€ ์žฆ์„ ๊ฒฝ์šฐ์—๋Š” ๋น ๋ฅธ ์„ฑ๋Šฅ์„ ๋ณด์ด์ง€๋งŒ, ์ž„์˜์˜ ์œ„์น˜์— ์žˆ๋Š” ์š”์†Œ์— ์ ‘๊ทผํ• ๋•Œ๋Š” ํšจ์šฉ์„ฑ์ด ๋–จ์–ด์ง. 

 

* Random Access : ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ์ž„์˜์˜ ์œ„์น˜์—์„œ ์ง์ ‘ ์•ก์„ธ์Šค ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ 

-> ์ธ๋ฑ์Šค ์—ฐ์‚ฐ์ž(sunscript operator) ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์˜ ํŠน์ •์š”์†Œ์— ์•ก์„ธ์Šค : array[i]


vector container

  • implemented as a dynamic array : ๋™์ ๋ฐฐ์—ด๋กœ ๊ตฌํ˜„๋จ. array์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์—ฐ์†์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ์š”์†Œ๋ฅผ ์ €์žฅํ•˜๊ณ  ํ•„์š”์— ๋”ฐ๋ผ ํฌ๊ธฐ๋ฅผ ๋™์ ์œผ๋กœ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Œ.
  • can access elements randomly (์ž„์˜์ ์œผ๋กœ ์ ‘๊ทผ)
  • ๊ธฐ๋ณธ์ƒ์„ฑ์ž(default constructor)์ด์™ธ์—๋„ ๋‹ค์–‘ํ•œ ์ƒ์„ฑ์ž๋ฅผ ํฌํ•จ -> ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ vector์„ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ์ƒ์„ฑ๊ฐ€๋Šฅ
  • size -> number of elements / capcity -> how much memeory is allocated to contain elements.

๊ธฐ๋ณธ ์ƒ์„ฑ์ž default constructor

.reserve()

vector<int> vector1; //capacity = 0
vector1.reserve(20);
//reserve() sets an initial capacity for the vector = 20
vector1.push_back(10); //capacity = 1
vector1.push_back(20); //capacity = 2

 

 

๋ณต์‚ฌ ์ƒ์„ฑ์ž copy constructor

vector<int> vector2(vector1);
vector<int> vector3 = vector1;

 

์ดˆ๊ธฐํ™” ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ์ž initializer list constructor

vector<int> vector4{ 130, 750, 259, 1234};
vector<int> vector5 = {234, 54, 5634, 65};
// set size and capacity right away + with specific elements!!

 

๋ฒ”์œ„ ์ƒ์„ฑ์ž Range constructor

vector<int> vector5 {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
vector<int> vector6 (vector5.begin() + 2, vector5.begin() +6);
//vector6 becomes : 30 40 50 60
  • (20, 50) "closed range" => 20 30 40 50 
  • [20, 50] "open range" => 30 40 
  • [20, 50) "half open range" => 30 40 50

vector capacity 

  • vector grows automatically; by default their capacity is increased as needed
  • tho do not shrink automatically; maintain the same capacity 
#include <iostream>
#include <vector>

int main() {
    std::vector<int> vec; 
    
    std::cout << "Initial capacity: " << vec.capacity() << std::endl;
    
    for (int i = 0; i < 10; ++i) 
    {
        vec.push_back(i); //vector capacity automatically grows
        std::cout << "Size: " << vec.size() << ", Capacity: " << vec.capacity() << std::endl;
    }
    
    return 0;
}

 

elements๋ฅผ ์ €์žฅํ•  ๊ณต๊ฐ„์ด ์—†์„๊ฒฝ์šฐ... -> ๋™์ ๋ฐฐ์—ด์ด ์ƒ์„ฑ (dynamic array) -> ๋ชจ๋“  elements๊ฐ€ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์— ๋ณต์‚ฌ๋จ

 

Inserting Elements

iterator insert(const_iterator position, cosnt value_type& val);
		//๋„ฃ๊ณ ์‹ถ์€ position, ๋„ฃ๊ณ ์‹ถ์€ single element
iterator insert(const_iterator position, size_type n, const value_type& val);
		//๋„ฃ๊ณ ์‹ถ์€ position, ๋„ฃ๊ณ ์‹ถ์€ ํšŸ์ˆ˜, ๋„ฃ๊ณ ์‹ถ์€ single element
iterator insert(const_iterator position, initializer_list<value_type> il);
		//๋„ฃ๊ณ ์‹ถ์€ position, ๋„ฃ๊ณ ์‹ถ์€ initializer list object

vector<int> vector1 = {10, 20, 30, 40};
vector1.insert(vector1.begin() + 2, 100); // 10 20 100 30 40
vector1.insert(vector1.begin() + 2, 3, 100); // 10 20 100 100 100 30 40 
vector1.insert(vector1.begin() + 2, {1, 2, 3, 4}); // 10 20 1 2 3 4 30 40

 

Subscript Operator ์ฒจ์ž ์—ฐ์‚ฐ์ž 

vector<int> vector1 = {1, 2, 3, 4, 5};
vector1[2] = 999;

//vector1 is 1 2 999 4 5

 

Other STL vector functions

front() / back() / begin() / end() / erase() / pop_back() 

vector<string> names = { "Ted", "Sue", "Bob", "Jill"};
// Return the first element.(/= .begin())
cout << names.front() << "\n"; //Ted
// Return the last element.(/= .end())
cout << names.back() << "\n"; //Jill 
// Delete "Bob".
names.erase(names.begin() + 2); // vector is: Ted Sue Jill
// Delete the last element.(which is Jill)
names.pop_back(); // vector is: Ted Sue

list container

  • implemented as a doubly-linked list : ์ด์ค‘์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„๋จ. ์ด๋Š” ์—ฐ์†์ ์ธ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ์š”์†Œ๋ฅผ ์ €์žฅํ•˜์ง€์•Š๋Š”๋‹ค๋Š” ๋ง๊ณผ ๊ฐ™์Œ.. ->  ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ์š”์†Œ๋Š” ๋‹ค์Œ ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ, ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์ œ๊ฑฐํ• ๋•Œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์žฌํ• ๋‹น ํ•  ํ•„์š”๊ฐ€ ์—†์Œ. ๋”ฐ๋ผ์„œ ๋ฆฌ์ŠคํŠธ๋Š” ๋™์ ์œผ๋กœ ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•˜๋Š”๋ฐ์— ํšจ์œจ์ ์ด๋‹ค. 
  • has no random access (์ž„์˜์ ์œผ๋กœ ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ!!!!) -> ++iter, --iter ์€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ (iter +2) ๋Š” ๋ถˆ๊ฐ€๋Šฅ!!!!
  • ์ฒจ์ž ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ๋ถˆ๊ฐ€ ! (subscript operator) 
  • slist(singly-linked-list) ๋„ ์กด์žฌํ•จ ใ…Ž

๊ธฐ๋ณธ ์ƒ์„ฑ์ž default constructor

* By default, an empty list will be created of size 0.

list<int> list1;
list1.push_back(10);
list1.push_back(80);
list1.push_back(54); // list is now: 10 80 54
list1.push_front(3);
list1.push_front(9); // list is now: 9 3 54 80 10

๋ณต์‚ฌ ์ƒ์„ฑ์ž copy constructor

list<int> list2(list1); //list1์ด ๊ทธ๋Œ€๋กœ ๋ณต์‚ฌ๋œ list2์ƒ์„ฑ
list<int> list3 = list1;

์ดˆ๊ธฐํ™” ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ์ž initializer list constructor

list<int> list4{ 130, 750, 259, 1234};
list<int> list5 = {234, 54, 5634, 65};
// set size and capacity right away + with specific elements!!

 

๋ฒ”์œ„ ์ƒ์„ฑ์ž Range constructor

list<int> list5 {10, 20, 30, 40};
list<int>::iterator iter = list5.begin();
++iter;
++iter;

list<int> list6(iter, list5.end());

//list6 contains : 30 40 
//list cannot use list5.begin() + 2!!!! ์ž„์˜์  ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅ!

 

For loop to Increment

list<int> aList = { 1, 2, 3, 4, 5, 6, 7, 8 };
auto iter = aList.begin(); // iter์€ 1
for (int i = 0; i < 6; ++i) // ์ด 6 ๋ฒˆ ๋ฐ˜๋ณต 
	++iter; // 1์—์„œ ์‹œ์ž‘ํ•ด์„œ 2 3 4 5 6 7 (6๋ฒˆ๋ฐ˜๋ณต) 
cout << *iter; // will print 7

 

Transferring elements

list::splice()

void splice(const_iterator position, list& x);
void splice(const_iterator position, list& x, const_iterator first, const_iterator last);

list<int> nums1 = { 5, 4, 3, 2, 1};
list<int> nums2 = { 77, 66, 55}; 
nums1.splice(++nums1.begin(), nums2); //nums1 is : 5 77 66 55 4 3 2 1 , num2 is empty
nums1.splice(nums1.begin(), ++nums2.begin(), nums2.end()); //nums1 is : 66 55 5 4 3 2 1 , nums2 is 77

Merging lists

void sort();
void merge(list& x);

list<int> nums5 = { 1, 7, 3, 4, 5};
list<int> nums6 = { 999, 888, 777};
list<int> nums7 = { 89, 23, 874};
nums5.sort(); // nums5 is : 1 3 4 5 7
nums6.sort(); // nums6 is : 777 888 999
nums5.merge(nums6) // nums5 is : 1 3 4 5 7 777 888 999, num6 is empty

//what happens if we do not sort the list? 
nums7.merge(nums6); // nums7 is : ...์กธ๋ผ ๋ณต์žกํ•ด์ง. ๊ทธ๋ƒฅ sortํ•˜์ž ใ…Ž

Other STL list functions

push_front() / pop_front() / reverse() / sort() / remove() 

list<int> scores = { 24, 76, 99, 87 };
// Add a new element to the front.
scores.push_front(75); // list is: 75 24 76 99 87
// Delete the front element.
scores.pop_front(); // list is: 24 76 99 87
// Reverse the order of elements.
scores.reverse(); // list is: 87 99 76 24
// Sort the list.
scores.sort(); // list is: 24 76 87 99
// Delete a given element in the list.
scores.remove(87); // list is: 24 76 99

 


Returning Iterators

std::vector::erase
iterator erase(const_iterator position);

vector<int> v = {1, 4, 5, 2, 3};
v.erase(v.begin() + 3;); // v is : 1 4 5 3

vector<int> v = {1, 4, 5, 2, 3};
vector<int>::iterator iter = v.erase(v.begin() + 3); 
	/* ๋จผ์ € v.begin() + 3์„ ํ†ตํ•ด ์ธ๋ฑ์Šค๊ฐ€ 3์ธ ์š”์†Œ์— ๋Œ€ํ•œ ๋ฐ˜๋ณต์ž๋ฅผ ์–ป๊ณ , 
   	 v.erase() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์š”์†Œ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. 
   	 ์ด ๋ฐ˜๋ณต์ž๋Š” ์‚ญ์ œ๋œ ์š”์†Œ์˜ ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. */
v.insert(iter, 100); // v is : 1 4 5 100 3

 

Q. iter์ด ์ธ๋ฑ์Šค๊ฐ€ 3์ธ ์š”์†Œ์— ๋Œ€ํ•œ ๋ฐ˜๋ณต์ž๋ฅผ ์–ป๊ณ , ๊ทธ๊ฑธ ์‚ญ์ œํ•จ. ๊ทธ๋Ÿผ ์œ„์น˜๊ฐ’(position)์„ ๊ธฐ๋กํ•˜๊ณ , ๊ทธ  value๋Š”nullptr ์ด ๋˜๋Š”๊ฑด๊ฐ€?? 

A. 

 

๋— ! ์–ด๋ ต๋‹ค .. 

'เซฎโ‚หถแต” แต• แต”หถโ‚Žแƒโ™ก/coding' Related Articles +