profile-img
์ง€๋ฐ์ด์˜ ํ‹ฐ์Šคํ† ๋ฆฌ
images/slide-image
container + 3
list_img
[C++] STL Pair, Nested Containers ์ค‘์ฒฉ ์ปจํ…Œ์ด๋„ˆ
user-img ์ฐœ๋‹
2024.03.31
STL Pair pair combines two values in a single unit pair ํƒ€์ž…์˜ ๋ชจ๋“  ๊ฐ์ฒด๋Š” ๋‘๊ฐœ์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง - first, second (์ „๋ถ€ public) #include ๋ฅผ ๊ผญ ๋„ฃ์–ด์•ผ๋Œ default constructor pair pairObj; overloaded constructor (with two parameters) pair pairObj(T1, T2); copy constructor pair pairObj(otherPairObj); EX1) #include ... void someFuction() { pair pair1(3, 5.4); cout
list_img
[C++] STL ์—ฐ๊ด€์ปจํ…Œ์ด๋„ˆ STL Associative Container (sets and multisets, maps and multimaps)
user-img ์ฐœ๋‹
2024.03.29
ใ…Žใ…Ž ์—ฐ๊ด€ ์ปจํ…Œ์ด๋„ˆ Associative Container automatically sorted ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋จ ( ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์ด์ฆˆ๊ฐ€ 0์ธ set์ด ๋งŒ๋“ค์–ด์ง ๋ณต์‚ฌ์ƒ์„ฑ์ž copy constructor set set3(set1); set set4 = set1; ์ดˆ๊ธฐํ™” ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ์ž initializer list constructor set set5 = {10, 30, 20}; //Elements will be stored in ascending order ๋ฒ”์œ„ ์ƒ์„ฑ์ž Range constructor set::iterator iter = set3.begin(); ++iter; // ๋˜๋Š” auto iter = set3.begin()์œผ๋กœ ์จ๋„๋จ set set4(iter, set3.end()); // set4..
list_img
[C++] STL ์ˆœ์ฐจ ์ปจํ…Œ์ด๋„ˆ STL Sequence Container (Vector, List)
user-img ์ฐœ๋‹
2024.03.29
๋“œ๋””์–ด ์ปจํ…Œ์ด๋„ˆ ์ฑ•ํ„ฐ๋กœ ๋„˜์–ด๊ฐ„๋‹ค !! 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 ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„๋œ ์ž๋ฃŒ๊ตฌ์กฐ, ๊ฐ ์š”์†Œ๊ฐ€ ์ด์ „ ์š”์†Œ์™€ ๋‹ค์Œ ์š”์†Œ์˜ ์ฃผ์†Œ..

STL Pair 

  • pair combines two values in a single unit
  • pair ํƒ€์ž…์˜ ๋ชจ๋“  ๊ฐ์ฒด๋Š” ๋‘๊ฐœ์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ง - first, second (์ „๋ถ€ public)
  • #include <utility> ๋ฅผ ๊ผญ ๋„ฃ์–ด์•ผ๋Œ 
default constructor pair<T1, T2> pairObj;
overloaded constructor (with two parameters) pair<T1, T2> pairObj(T1, T2);
copy constructor pair<T1, T2> pairObj(otherPairObj);

 

EX1)

#include <utility>
...
void someFuction()
{
	pair<int, double> pair1(3, 5.4);
    	cout << pair1.first() << ", " << pair1.second() <<endl;
}

 

EX2)

#include <utility>

pair<int, double> p1;
p1.first() = 3;
p1.second() = 2.5;

pair<int, double> p2(13, 4.5);
pair<string, int> student("Bob", 123);

cout << student.first();
cout << student.second();

 

EX3)

Pair's memebr variables should be PUBLIC ! The program will CRASH!!!

#include <utility>
...
void someFunction()
{
	pair<int,MyClass> pair1; //default values
    cout << pair1.first() << pair1.second(); 
}

class MyClass
{
	public:
    	...
    private:
    	string str;
        double d;
}

Nested Containers ์ค‘์ฒฉ์ปจํ…Œ์ด๋„ˆ 

  • An STL container can store instances of other containers.
    • A vector of vectors
    • A list of pointers to vectors
    • A map of strings and sets
    • ๋Œ€์ถฉ ์ปจํ…Œ์ด๋„ˆ ์•ˆ์— ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค๋Š” ๋œป .. 

EX1) Vectors of Vectors

vector<vector<int>> studentGrades = {
		{98, 90, 100},	// student 1
        	{75, 97, 87},	// student 2
        	{96, 74, 88}	// student 3
};

cout << "Total number of students: " <<	studentGrades.size() << endl;
		// studentGrades ๋ฒกํ„ฐ์— ์ €์žฅ๋œ ํ•™์ƒ์˜ ์ˆ˜ -> 3
        
//iterating the elements in a vector of vectors
int row = 0;
for (const auto& students : studentGrades)
{
	cout << "Student " << row << "grades: ";
    for (auto grade : students)
    {
    	cout << grade << " ";
    }
    ++row;
    cout << "\n";
}

 

EX2) Map of Strings and Vectors

map<string, vector<int>> studentGrades = {
            {"Jane", {98, 90, 100}},
            {"Bob", {75, 97, 87}},
            {"Jill", {96, 74, 88}},
            };
            
cout << "Total number of students: " << studentGrades.size() << endl;

for (const auto& grades : studentGrades)
{
    cout << grades.first << ": ";
    for (auto grade : grades.second)
    {
        cout << grade << " ";
    }
    cout << "\n";
}

 

.. ๋€ผ

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

ใ…Žใ…Ž 

 

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

automatically sorted
์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋จ (<)

 


Sets and Multisets

  • automatically sorted ( By default, in ascending order)
  • multisets์€ ์ค‘๋ณตํ—ˆ์šฉ, sets๋Š” ํ—ˆ์šฉ ์•ˆํ•จ !

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

set<int> set1;
set1.insert(6);
set1.insert(2);
set1.insert(4);  // set is: 2 4 6

-> ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์ด์ฆˆ๊ฐ€ 0์ธ set์ด ๋งŒ๋“ค์–ด์ง

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

set<int> set3(set1);
set<int> set4 = set1;


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

set<int> set5 = {10, 30, 20};
//Elements will be stored in ascending order


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

set<int>::iterator iter = set3.begin(); ++iter;
// ๋˜๋Š” auto iter = set3.begin()์œผ๋กœ ์จ๋„๋จ
set<int> set4(iter, set3.end());
// set4 is: 20 30 40

 

set::erase(value)

size_type erase(const value_type& val);
// erase() ํ•จ์ˆ˜๋Š” ์‚ญ์ œ๋œ ์š”์†Œ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•จ! 

multiset<int> aMultiSet =
{ 14, 12, 13, 17, 14, 19, 18, 16, 14, 14, 19, 11, 15 };
size_t itemsDeleted = aMultiSet.erase(14);
cout << itemsDeleted << "\n"; //4, ์‚ญ์ œ๋œ 14์˜ ๊ฐฏ์ˆ˜
for (auto elem : aMultiSet) cout << elem << " "; // 11 12 13 15 16 17 18 19 19

 

set::erase(position)

iterator erase(const_iterator position);

multiset<int> aMultiset = { 11, 12, 13, 15, 17 };
auto iterSet = ++aMultiset.begin(); 
cout << *iterSet << endl; //12

iterSet = aMultiset.erase(iterSet); //-> ์‚ญ์ œ๋œ ์š”์†Œ์˜ ๋‹ค์Œ์š”์†Œ๋ฅผ ๊ฐ€๋ฅดํ‚ด
cout << *iterSet << endl; // 13

for(auto elem : aMultiSet) 
	cout << elem << endl; // 11 13 15 17

 

set::erase(range)

iterator erase(const_iterator first, const_iterator last);

set<int> aSet = {11, 12, 13, 15, 17, 18, 19};

auto iterSet = aSet.begin();
for (int i = 1; i < 6; ++i) 
{
	++iterSet; //5๋ฒˆ๋ฐ˜๋ณต 
}
cout << *iterSet << endl; // 18 (6th element)

iterSet = aSet.erase(++aSet.begin(), iterSet); // 12 ~ 18 ์‚ฌ์ด์— ์žˆ๋Š”๊ฑฐ ๋‹ค์ง€์›€ 
cout << *iterSet << endl; // 18(์‚ญ์ œ๋œ ์š”์†Œ ๋’ค์— ์žˆ๋Š” ๋ฐ”๋กœ ์ฒซ ์š”์†Œ

for(auto elem:aSet)
	cout << elem << endl; // 11 18 19

-> ์™œ ๋งˆ์ง€๋ง‰์ด 11 12 18 19 ๊ฐ€ ์•„๋‹ˆ์ง€?? 

set::find(value)

set<int> aSet = { 23, 34, 48, 57, 68, 71, 83 };
auto iter = aSet.find(57); //iter์€ 57์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฐ˜๋ณต์ž๊ฐ€๋จ 

if (iter != aSet.end())
cout << "Element found."; // set์—์„œ 57์„ ์ฐพ์œผ๋ฉด ์ถœ๋ ฅ !
else
cout << "Element not found.";

 

* find ํ•จ์ˆ˜๋Š” True and False, Boolean Value๋กœ ๋ฆฌํ„ดํ•  ์ˆ˜ ์—†๋‹ค ! 

 


Maps and Multimaps

  • elements๋ฅผ key/value์Œ์˜ ํ˜•ํƒœ๋กœ ๊ด€๋ฆฌํ•จ 
  • automatically sorted ( By default, in ascending order)
  • multimaps์€ ์ค‘๋ณตํ—ˆ์šฉ, maps๋Š” ํ—ˆ์šฉ ์•ˆํ•จ !
 

 

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

-> When data is immediately available !! 

map<int, string> aMap = {{1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}};

auto iterEnd = aMap.cend();
for (auto iter = aMap.cbegin(); iter != iterEnd; ++iter)
cout << iter->first << " " << iter->second << "\n";

 

 

๊ธฐ๋ณธ์ƒ์„ฑ์ž์™€ ๋Œ€๊ด„ํ˜ธ(?) Default Constructor + Subscript

-> When data is NOT immediately available !!

map<char, int> aMap;
aMap['A'] = 90;
aMap['b'] = 80;
aMap['C'] = 70;
aMap['D'] = 60;

auto iterEnd = aMap.cend();
for (auto iter = aMap.cbegin(); iter != iterEnd; ++iter)
cout << iter->first << " " << iter->second << "\n";

// Output : A 90, C 70, D 60, b 80
// ASCII code ์— ๋”ฐ๋ผ์„œ b 80์ด ๋’ค๋กœ ๊ฐ

 

๊ธฐ๋ณธ์ƒ์„ฑ์ž Default Constructor + map::insert

-> insert / make_pair ์‚ฌ์šฉ !!

map<int, int> intMap;

for (int i = 1; i < 10; ++i) // insert integers
intMap.insert (make_pair (i, (100 / i)));

auto itEnd = intMap.cend();
for (auto it = intMap.cbegin(); it != itEnd; ++it)
cout << it->first << " " << it->second << "\n";

// map is: {(1,100),(2,50),(3,33),...(8,12),(9,11)}

 

map::find(value)

const_iterator find(const value_type& k) const;
map<string, int> states = {{"AK", 1959}, {"CA", 1850}, {"FL", 1845}, {"IL", 1818}, {"IO", 1846}, {"OR", 1859}};

auto iter = states.find("CA");
if (iter != states.end())
	cout << "Year: " << iter->second;
else
	cout << "State not found.";

 

 

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

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

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 +