๐ŸŒˆ๋ช…ํ’ˆ C++ ํ”„๋กœ๊ทธ๋ž˜๋ฐ 10์žฅ๐Ÿง‘โ€๐Ÿ’ป

Se0ng_1lยท2022๋…„ 7์›” 3์ผ
0

๋ช…ํ’ˆCPPํ”„๋กœ๊ทธ๋ž˜๋ฐ

๋ชฉ๋ก ๋ณด๊ธฐ
10/10
post-thumbnail

10์žฅ ์š”์ 

ํ…œํ”Œ๋ฆฟ๊ณผ ํ‘œ์ค€ ํ…œํ”Œ๋ฆฟ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(STL)

ํ…œํ”Œ๋ฆฟ

ํ…œํ”Œ๋ฆฟ

ํ•จ์ˆ˜๋‚˜ ํด๋ž˜์Šค ์ฝ”๋“œ๋ฅผ ์ฐ์–ด๋‚ด๋“ฏ์ด ์ƒ์‚ฐํ•  ์ˆ˜ ์žˆ๋„๋ก ์ผ๋ฐ˜ํ™”(generic)์‹œํ‚ค๋Š” ๋„๊ตฌ

์„ ์–ธ

template <class T>
// ์—ฌ๊ธฐ์„œ T ์ž„์˜๋กœ ๋ถ™์ธ ๊ฒƒ์ด๋ฉฐ ์–ด๋–ค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•ด๋„ ์ƒ๊ด€์—†๋‹ค.

template <class T1, class T2, class T3>
// ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ์„ ์–ธ ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋ฅผ ์ด์šฉํ•ด ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ž๋ฃŒํ˜•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์‚ฌ์šฉ ์˜ˆ์‹œ) ํ•จ์ˆ˜ ์ค‘๋ณต

#include <iostream>
using namespace std;

void Swap(int &a, int &b)
{
    int temp;
    temp = a;
    a = b;
    b = temp;
}

void Swap(double &a, double &b)
{
    double temp;
    temp = a;
    a = b;
    b = temp;
}


int main()
{
    int a = 1, b = 2;
    double c = 3.0, d = 0.14;

    cout << a << " " << b << endl;
    cout << c << " " << d << endl;
    Swap(a, b);
    Swap(c, d);
    cout << "์Šค์™‘ ํ›„" << endl;
    cout << a << " " << b << endl;
    cout << c << " " << d << endl;
    
}

์œ„ swap ํ•จ์ˆ˜ ์ค‘๋ณต์€ ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ํ˜•๋งŒ ๋‹ค๋ฅด์ง€ ์ˆ˜ํ–‰๊ณผ์ •์€ ๋˜‘๊ฐ™๋‹ค.

ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ)

#include <iostream>
using namespace std;

template <class T>

void Swap(T &a, T &b)
{
    T temp;
    temp = a;
    a = b;
    b = temp;
}


int main()
{
    int a = 1, b = 2;
    double c = 3.0, d = 0.14;

    cout << a << " " << b << endl;
    cout << c << " " << d << endl;
    Swap(a, b);
    Swap(c, d);
    cout << "์Šค์™‘ ํ›„" << endl;
    cout << a << " " << b << endl;
    cout << c << " " << d << endl;
}

ํ•จ์ˆ˜๋ฅผ ์ผ๋ฐ˜ํ™”ํ•˜์—ฌ genericํ•จ์ˆ˜๋กœ ๋งŒ๋“ ๋‹ค๋ฉด, ์œ„ ์ฒ˜๋Ÿผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์“ธ ์ˆ˜ ์žˆ๋‹ค.
์‚ฌ์šฉ์— ๋”ฐ๋ผ T์˜ ํ˜•์ด ๋ฐ”๋€Œ๋Š” ๊ณผ์ •์„ "๊ตฌ์ฒดํ™”"๋ผ๊ณ  ํ•œ๋‹ค.

โš ๏ธ ์ œ๋„ค๋ฆญ ํ•จ์ˆ˜์™€ ์ผ๋ฐ˜ํ•จ์ˆ˜๋ฅผ ์ค‘๋ณต์„ ์–ธ ํ•  ๊ฒฝ์šฐ ์šฐ์„ ์ˆœ์œ„๋Š” ์ผ๋ฐ˜ํ•จ์ˆ˜์˜ ์šฐ์„ ์ˆœ์œ„๊ฐ€
โ€‚โ€‚โ€‚ ๋” ๋†’์•„ ์ผ๋ฐ˜ํ•จ์ˆ˜๋ฅผ ๋ฐ”์ธ๋”ฉํ•œ๋‹ค.

โ€‚โ€‚โ€‚์ œ๋„ฅ๋ฆญ ํ•จ์ˆ˜์—๋„ ๋””ํดํŠธ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค.

generic ํด๋ž˜์Šค

genericํด๋ž˜์Šค๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

ํ™œ์šฉ ์˜ˆ)

// ์„ ์–ธ๋ถ€
template <class T>
class MyStack{ 
    int tos;
    T data[100];
public:
    MyStack();
    void push(T element);
    T pop();
};

// ๊ตฌํ˜„๋ถ€
template <class T>
void MyStack<T>::push(T element) {
    
}
template <class T> T MyStack<T>::pop() {
    
}

int main()
{	
	// ๊ฐ์ฒด ์ƒ์„ฑ
    MyStack<int> iStack;
    MyStack<double> dStack;
    MyStack<int *> ipStack; // int* ์Šคํƒ
    MyStack<Point> pointStack; // Pointํด๋ž˜์Šค ์Šคํƒ
    // ๋™์  ์ƒ์„ฑ
    MyStack<char> *p = new MyStack<char>();
    delete p;
}

STL(Standard Template Library)

STL

ํ…œํ”Œ๋ฆฟ์œผ๋กœ ์ž‘์„ฑ๋œ ๋งŽ์€ ์ œ๋„ค๋ฆญ ํด๋ž˜์Šค์™€ ํ•จ์ˆ˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

Container

ํ…œํ”Œ๋ฆฟ ํด๋ž˜์Šค, ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ฒ€์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ด์•„๋‘๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค
์ข…๋ฅ˜ : ๋ฆฌ์ŠคํŠธ(list), ํ(queue), ์Šคํƒ(stack), ๋ฒกํ„ฐ(vector), ๋””ํ(deque),
์…‹(set), ๋งต(map) ๋“ฑ

iterator

๋ฐ˜๋ณต์ž, ์ปจํ…Œ์ด๋„ˆ ์›์†Œ์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ, ์›์†Œ๋“ค์„ ํ•˜๋‚˜์”ฉ ์ˆœํšŒ ์ ‘๊ทผํ•˜๊ธฐ์œ„ํ•ด ๋งŒ๋“ฌ

์•Œ๊ณ ๋ฆฌ์ฆ˜

ํ…œํ”Œ๋ฆฟ ํ•จ์ˆ˜ ์ปจํ…Œ์ด๋„ˆ ์•ˆ์—์„œ ์ˆ˜ํ–‰ํ•  ํ…œํ”Œ๋ฆฟ ํ•จ์ˆ˜, ํด๋ž˜์Šค์˜ ๋ฉค๋ฒ„ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋‹ค.
์ข…๋ฅ˜ : copy, find, random, swap, sort, min, max ๋“ฑ

STL์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” STL๊ด€๋ จ ํ—ค๋”ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

#include <vector> // vector์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด

โš ๏ธvector์˜ ์›์†Œ์‚ญ์ œ

vector<int> v;
/* ..... */
vector<int>::iterator it; // iterator๋ณ€์ˆ˜ ์ƒ์„ฑ
it = v.begin(); // ๋ฒกํ„ฐ์˜ 0๋ฒˆ์งธ ์›์†Œ ๊ฐ€๋ฆฌํ‚ด
it = v.erase(it); // ์ฒซ๋ฒˆ์งธ ์›์†Œ ์‚ญ์ œ

it = v.erase(it); 
// it๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์›์†Œ๋ฅผ ์‚ญ์ œ ํ›„ it๋’ค์— ์›์†Œ๋“ค์„ ๋ชจ๋‘ ํ•œ์นธ ์”ฉ ๋‹น๊ธด๋‹ค.
// ๊ทธ๋ฆฌ๊ณ  ์‚ญ์ œ๋˜์—ˆ๋˜ ์›์†Œ์˜ ๋‹ค์Œ ์›์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
// ๋”ฐ๋ผ์„œ erase()์˜ ๋ฆฌํ„ด๊ฐ’์œผ๋กœ it ์žฌ์„ค์ •

โš ๏ธ iterator๊ด€๋ จ

vector<int>::iterator it; // iterator๋ณ€์ˆ˜ ์ƒ์„ฑ

it = v.begin(); // ๋ฒกํ„ฐ v์˜ 0๋ฒˆ์งธ ์›์†Œ ๊ฐ€๋ฆฌํ‚ด

it++ // ๋ฒกํ„ฐ v์˜ 0๋ฒˆ์งธ ์›์†Œ ๊ฐ€๋ฆฌํ‚ด

int n = *it; // n์— ํ˜„์žฌ it๊ฐ€ v์—์„œ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’์„ n์— ๋„ฃ์Œ
*it = 8; // ํ˜„์žฌ v์—์„œ it๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ณณ์— 8์„ ๋„ฃ๋Š”๋‹ค.

v.end() // v์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ ๋‹ค์Œ ์œ„์น˜์— ๋Œ€ํ•œ ์ฃผ์†Œ ๋ฆฌํ„ด

// v์˜ ๋ชจ๋“  ์›์†Œ ๊ฒ€์ƒ‰ ๋ฐฉ๋ฒ•
for(it = v.begint(); it != v.end(); it++)
{
	int n = *it;
    cout << n << endl;
}

vector ๊ด€๋ จ
https://blockdmask.tistory.com/70

map ์ปจํ…Œ์ด๋„ˆ

๋‹ค๋ฅธ ์–ธ์–ด์˜ Dictionary์™€ ๊ฐ™์€ ํ˜•ํƒœ
ํ‚ค์™€ ๊ฐ’์œผ๋กœ ๊ตฌ์„ฑ์ด ๋˜์–ด์žˆ๋‹ค.

#include <iostream>
#include <map>
using namespace std;

int main()
{
    map<string, string> dic;
    dic.insert(make_pair("peace", "ํ‰ํ™”")); // dic์— ("peace", "ํ‰ํ™”") ์ €์žฅ
    dic["Love"] = "์‚ฌ๋ž‘"; // dic์— ("Love", "์‚ฌ๋ž‘") ์ €์žฅ

    string peaceKor = dic["peace"];
    string loveKor = dic.at("Love");
    cout << peaceKor << " " << loveKor << endl;

    // ์›์†Œ ์ฐพ๋Š” ๋ฐฉ๋ฒ•
    // ์—†์œผ๋ฉด true ์žˆ์œผ๋ฉด false๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.
    if(dic.find("eng") == dic.end())
    {
        cout << "eng๋Š” ์—†์Šต๋‹ˆ๋‹ค." << endl;
    }
    if(dic.find("Love") == dic.end())
    {
        cout << "Love ์—†์Šต๋‹ˆ๋‹ค." << endl;
    }
}

์ถœ๋ ฅ

STL ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์‚ฌ์šฉํ•˜๊ธฐ

STL์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ „์—ญ ํ•จ์ˆ˜๋กœ ์ปจํ…Œ์ด๋„ˆ์˜ ํด๋ž˜์Šค ๋ฉค๋ฒ„๊ฐ€ ์•„๋‹Œ ํ…œํ”Œ๋ฆฟ์œผ๋กœ ์ž‘์„ฑ๋˜์–ด ์žˆ๋‹ค.

#include <algorithm>
// ์‚ฌ์šฉํ•˜๊ธฐ์œ„ํ•ด์„  ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ—ค๋”ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•œ๋‹ค.
sort(v.begin(), v.end()); // v์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ

auto์™€ ๋žŒ๋‹ค์‹

auto

์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๋ณ€์ˆ˜ํƒ€์ž…์„ ์ถ”๋ก ํ•ด์„œ ๋ถ™์—ฌ์ค€๋‹ค.
๋ณ€์ˆ˜์˜ ํ˜•์„ ๋”ฐ๋กœ ์ง€์ •์•ˆํ•ด์ค˜๋„ ๋˜์–ด ์ž๋ฃŒํ˜• ๊ด€๋ จ syntax์˜ค๋ฅ˜ ํ•ด๊ฒฐํ•˜๋Š”๋ฐ ์žฅ์ 

// b์˜ ์ž๋ฃŒํ˜•์€ int๊ฐ€ ๋œ๋‹ค.
int a = 3;
auto b = a;

๋žŒ๋‹ค ๋Œ€์ˆ˜์™€ ๋žŒ๋‹ค์‹

์ˆ˜ํ•™์—์„œ ์ด๋ฆ„ ์—†๋Š” ํ•จ์ˆ˜๋ฅผ ๋žŒ๋‹ค์‹์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ๋Š” ์ด๋ฆ„ ์—†๋Š” ์ต๋ช… ํ•จ์ˆ˜๋ฅผ ๋žŒ๋‹ค์‹ ๋˜๋Š” ๋žŒ๋‹ค ํ•จ์ˆ˜๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
โš ๏ธ
1. ๊ผญ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์€ ์•„๋‹ˆ์ง€๋งŒ, ์งง๊ณ  ๊ฐ„๊ฒฐํ•œ ์ฝ”๋“œ ์ž‘์„ฑํ•˜๋Š”๋ฐ ์ข‹๋‹ค.
2. ํ•œ๋ฒˆ๋งŒ ํ˜ธ์ถœํ•˜๊ณ  ์žฌ์‚ฌ์šฉ ์•ˆํ•˜๋Š” ํ•จ์ˆ˜ ์‚ฌ์šฉ์‹œ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉ
3. STL์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์—ฐ์‚ฐ ์ฝ”๋“œ๋ฅผ ๋„˜๊ธฐ๋Š” ๊ฒฝ์šฐ, ์—ฐ์‚ฐ ์ฝ”๋“œ๋ฅผ ์ต๋ช…์˜ ๋žŒ๋‹ค์‹์œผ๋กœ ์ž‘์„ฑ

์„ ์–ธ
[์บก์ณ๋ฆฌ์ŠคํŠธ](๋งค๊ฐœ๋ณ€์ˆ˜ ๋ฆฌ์ŠคํŠธ)->๋ฆฌํ„ดํƒ€์ž…(์ƒ๋žต ๊ฐ€๋Šฅ){ํ•จ์ˆ˜ ๋ฐ”๋””}

์บก์ณ๋ฆฌ์ŠคํŠธ : ๋žŒ๋‹ค์‹ ์™ธ๋ถ€์— ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ๋„ฃ๋Š”๋‹ค.
[x] : ๋ณ€์ˆ˜ x์˜ ๊ฐ’ ํ™œ์šฉ
[&x] : ์ฐธ์กฐ ๋ณ€์ˆ˜ x ํ™œ์šฉ
[=] : ๋ชจ๋“  ๋ณ€์ˆ˜ ํ™œ์šฉ
[&] : ๋ชจ๋“  ์ฐธ์กฐ ๋ณ€์ˆ˜ ํ™œ์šฉ

๋งค๊ฐœ๋ณ€์ˆ˜ ๋ฆฌ์ŠคํŠธ : ๋ณดํ†ต ํ•จ์ˆ˜์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜ ๋ฆฌ์ŠคํŠธ์™€ ๊ฐ™๋‹ค.

ํ•จ์ˆ˜ ๋ฐ”๋”” : ์‹คํ–‰ํ•˜๊ณ ์ž ํ•˜๋Š” ์ฝ”๋“œ

#include <iostream>
using namespace std;

int main()
{
    [](int x, int y) {cout << x + y;};
    [](int x, int y) -> int { return x + y;};
    [](int x, int y){ cout << x + y;}(2,3); // 5์ถœ๋ ฅ
}

๋žŒ๋‹ค์‹ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•

auto ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•œ๋‹ค.

์˜ˆ์ œ 1

#include <iostream>
#include <string>
using namespace std;

int main()
{
    auto building = [](string a, string b){
        cout << a << "๊ฐ€ " << b << "๋ณด๋‹ค ๋‚ฎ๋‹ค." << endl;
    };

    building("๊ด‘ํ™”๋ฌธ", "63๋นŒ๋”ฉ");
    building("63๋นŒ๋”ฉ", "๋กฏ๋ฐํƒ€์›Œ");
}

์˜ˆ์ œ 2

#include <iostream>
using namespace std;

int main()
{
    double pi = 3.14;

    auto area = [pi](double r) ->double {return pi * r * r;};
    cout << area(10) << endl; 
    // ์ถœ๋ ฅ : 314
}

์˜ˆ์ œ 3

#include <iostream>
using namespace std;

int main()
{
    int pow = 0;

   [&pow](int r){pow = r * r;} (15);
    cout << pow << endl; 
    // ์ถœ๋ ฅ : 225
}

โ—๏ธ+) for_each๋ฌธ ์‚ฌ์šฉ ์˜ˆ์‹œ

1. ์ •์ ๋ฐฐ์—ด
๊ตฌ์ฒดํ™”๋ฅผ ํ†ตํ•ด autoArr์€ intํ˜•์ด ๋˜๊ณ  arr์ด ๋ณต์‚ฌ๋˜์–ด ๋“ค์–ด๊ฐ„๋‹ค.
๋ณต์‚ฌ๋˜์—ˆ์œผ๋ฏ€๋กœ ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค.

#include <iostream>
using namespace std;

int main()
{
    int arr[5] = {1, 2, 3, 4, 5};
    for(auto autoArr : arr){
        autoArr *= 3;
        cout << autoArr << " ";
    }
    cout << endl;
    for(int i = 0; i < 5; i++)
    {
        cout << arr[i] << " ";
    }
}
// ์ถœ๋ ฅ 3 6 9 12 15
//	  1 2 3 4 5

2. ์ •์ ๋ฐฐ์—ด (์ฐธ์กฐ ์‚ฌ์šฉ)
๊ตฌ์ฒดํ™”๋ฅผ ํ†ตํ•ด autoArr์€ intํ˜•์ด ๋˜๊ณ  arr์ด ๋ณต์‚ฌ๋˜์–ด ๋“ค์–ด๊ฐ„๋‹ค.
๋ณต์‚ฌ๋˜์—ˆ์œผ๋ฏ€๋กœ ๊ฐ’์ด ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค.

#include <iostream>
using namespace std;

int main()
{
    int arr[5] = {1, 2, 3, 4, 5};
    for(auto &autoArr : arr){
        autoArr *= 3;
        cout << autoArr << " ";
    }
    cout << endl;
    for(int i = 0; i < 5; i++)
    {
        cout << arr[i] << " ";
    }
}

// ์ถœ๋ ฅ 3 6 9 12 15
//	  3 6 9 12 15

3. foreach๋ฌธ์—์„œ ๋™์ ๋ฐฐ์—ด์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ๋Œ€์‹ ์— vector๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋™์ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> arr = {1,2,3,4,5};
    for(auto autoArr : arr){
        autoArr *= 3;
        cout << autoArr << " ";
    }
    cout << endl;
    for(int i = 0; i < 5; i++)
    {
        cout << arr[i] << " ";
    }
}
// ์ถœ๋ ฅ 3 6 9 12 15
//	  1 2 3 4 5

4. STLํ…œํ”Œ๋ฆฟ๊ณผ algorithm ํ—ค๋”ํŒŒ์ผ์† for_each()ํ•จ์ˆ˜ ์‚ฌ์šฉ๋ฐฉ๋ฒ•

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void print(int n){
    cout << n << " ";
}
int main()
{
    vector<int> arr = {1,2,3,4,5};
    for_each(arr.begin(), arr.end(), print);
    // arr์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์›์†Œ๋“ค์„ ๊ฒ€์ƒ‰ํ•˜๋ฉด์„œ ๊ฐ ์›์†Œ๋ฅผ print์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ n์œผ๋กœ ๋„˜๊ฒจ์ค€๋‹ค.
}
// ์ถœ๋ ฅ 
// 1 2 3 4 5

5. STLํ…œํ”Œ๋ฆฟ๊ณผ algorithm ํ—ค๋”ํŒŒ์ผ์† for_each()ํ•จ์ˆ˜ + ๋žŒ๋‹ค์‹

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    vector<int> arr = {1,2,3,4,5};
    for_each(arr.begin(), arr.end(), [](int n){cout << n << " ";});
}

6. ๋ฒ”์œ„ ๊ธฐ๋ฐ˜ for๋ฌธ

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<int> arr = {1,2,3,4,5};
    // 1. ํ˜• ์ง€์ •
    for(int n : arr)
    {
    	cout << n << " ";
    }
    // 2. auto
    for(auto &n : arr)
    {
    	cout << n << " ";
    }
}

๋ช…ํ’ˆ C++ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ฑ…
https://book.naver.com/bookdb/book_detail.naver?bid=13395206

๐Ÿ“Œ11์žฅ๋ถ€ํ„ฐ 13์žฅ์€ ๋น„๊ณต๊ฐœ๋กœ ์—…๋กœ๋“œ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

โš ๏ธ ์ฃผ์˜ โš ๏ธ
1. ์—ฌ๊ธฐ์„œ๋ถ€ํ„ฐ๋Š” ์‹ค์Šต๋ฌธ์ œ ์ •๋‹ต์ž…๋‹ˆ๋‹ค.
2.์ œ๊ฐ€ ์ž‘์„ฑํ•œ ์ •๋‹ต์ด ํ‹€๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ˜น์‹œ ํ‹€๋ฆฐ๊ฒŒ ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ ๋‚จ๊ฒจ์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํžˆ ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
3. C++๋ฅผ ๊ณต๋ถ€ํ•˜์‹œ๋Š” ํ•™์ƒ์ด์‹œ๋ผ๋ฉด ๋ณด์‹œ๊ธฐ ์ „์— ์ง์ ‘ ๊ผญ ํ•œ ๋ฒˆ ํ’€์–ด๋ณด์„ธ์š”!

_์‹ค์Šต ๋ฌธ์ œ 10 - 1
๋ฌธ์ œ : ์ œ๋„ค๋ฆญ ํ•จ์ˆ˜ biggest() ์ž‘์„ฑํ•˜๊ธฐ

#include <iostream>
using namespace std;

template <class T>
T biggest(T *arr, int size)
{
    T max = arr[0];
    for(int i = 1; i < size; i++)
    {
        if(max < arr[i])
            max = arr[i];
    }
    return max;
}

int main()
{
    int iArr[5] = {1, 2, 3, 4, 5};
    cout << biggest(iArr, 5) << endl;
    double dArr[5] = {1.2, 3.4, 4.5, 5.6, 7.8};
    cout << biggest(dArr, 5) << endl;
    char cArr[5] = {'F', 'Z', 'T', 'O', 'A'};
    cout << biggest(cArr, 5);
}

ย 

_์‹ค์Šต ๋ฌธ์ œ 10 - 2
๋ฌธ์ œ : ์ œ๋„ค๋ฆญ ํ•จ์ˆ˜ equalArrays() ์ž‘์„ฑํ•˜๊ธฐ

#include <iostream>
using namespace std;

template <class T>
bool equalArray(T *arr1, T *arr2, int size)
{
    for(int i = 0; i < size; i++)
    {
        if(arr1[i] != arr2[i]){
            return false;
        }
    }
    return true;
}

int main()
{
    int iArr1[5] = {1, 2, 3, 4, 5};
    int iArr2[5] = {1, 2, 3, 4, 5};
    if(equalArray(iArr1, iArr2, 5))
        cout << "๊ฐ™๋‹ค" << endl;
    else
        cout << "๋‹ค๋ฅด๋‹ค" << endl;
    double dArr1[5] = {1.2, 3.4, 4.5, 5.6, 7.8};
    double dArr2[5] = {1.2, 4.5, 5.6, 9.0, 7.8};
    if(equalArray(dArr1, dArr2, 5))
        cout << "๊ฐ™๋‹ค" << endl;
    else
        cout << "๋‹ค๋ฅด๋‹ค" << endl;
    char cArr1[5] = {'F', 'X', 'T', 'O', 'A'};
    char cArr2[5] = {'F', 'Z', 'T', 'O', 'A'};
    if(equalArray(cArr1, cArr2, 5))
        cout << "๊ฐ™๋‹ค" << endl;
    else
        cout << "๋‹ค๋ฅด๋‹ค" << endl;
}

ย 
_์‹ค์Šต ๋ฌธ์ œ 10 - 3
๋ฌธ์ œ : ์ œ๋„ค๋ฆญ ํ•จ์ˆ˜ reverseArray() ์ž‘์„ฑํ•˜๊ธฐ

#include <iostream>
using namespace std;

template <class T>
void reverseArray(T *arr, int size)
{
    for(int i = 0; i < size / 2; i++)
    {
        swap(arr[i], arr[size - i - 1]);
    }
}

int main()
{
    int iArr[5] = {1, 2, 3, 4, 5};
    reverseArray(iArr, 5);
    for(int i = 0; i < 5; i++)
    {
        cout << iArr[i] << " ";
    }
    cout << endl;

    double dArr[5] = {1.2, 3.4, 4.5, 5.6, 7.8};
    reverseArray(dArr, 5);
    for(int i = 0; i < 5; i++)
    {
        cout << dArr[i] << " ";
    }
    cout << endl;
    char cArr[5] = {'F', 'X', 'T', 'O', 'A'};
    reverseArray(cArr, 5);
    
    for(int i = 0; i < 5; i++)
    {
        cout << cArr[i] << " ";
    }
    cout << endl;

}

ย 
_์‹ค์Šต ๋ฌธ์ œ 10 - 4
๋ฌธ์ œ : ์ œ๋„ค๋ฆญ ํ•จ์ˆ˜ search() ์ž‘์„ฑํ•˜๊ธฐ

#include <iostream>
using namespace std;

template <class T>
bool search(T num, T *arr, int size)
{
    for(int i = 0; i < size; i++)
    {
        if(num == arr[i])
            return true;
    }
    return false;
}

int main()
{
    int iArr[5] = {1, 2, 3, 4, 5};
    if(search(3, iArr, 5))
        cout << 3 << " ์ฐพ์•˜๋‹ค!" << endl;
    else
        cout << 3 << " ๋ชป์ฐพ์•˜๋‹ค.." << endl;

    double dArr[5] = {1.2, 3.4, 4.5, 5.6, 7.8};
    if(search(5.6, dArr, 5))
        cout << 5.6 << " ์ฐพ์•˜๋‹ค!" << endl;
    else
        cout << 5.6 << " ๋ชป์ฐพ์•˜๋‹ค.." << endl;

    char cArr[5] = {'F', 'X', 'T', 'O', 'A'};
    if(search('Z', cArr, 5))
        cout << 'Z' << " ์ฐพ์•˜๋‹ค!" << endl;
    else
        cout << 'Z' << " ๋ชป์ฐพ์•˜๋‹ค.." << endl;

}

ย 
_์‹ค์Šต ๋ฌธ์ œ 10 - 5
๋ฌธ์ œ : ์ œ๋„ค๋ฆญ ํ•จ์ˆ˜ concat() ์ž‘์„ฑํ•˜๊ธฐ

#include <iostream>
using namespace std;

template <class T>
T* concat(T a[], int sizea, T b[], int sizeb){
    T *arr = new T[sizea + sizeb];
    for(int i = 0; i < sizea; i++)
    {
        arr[i] = a[i];
    }
    for(int i = sizea; i < sizeb + sizea; i++)
    {
        arr[i] = b[i - sizea];
    }
    return arr;
}

template <class T>
void print(T *arr, int size)
{
    for(int i = 0; i < size; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}

int main()
{
    int iArr1[5] = {1, 2, 3, 4, 5};
    int iArr2[5] = {6, 7, 8, 9, 10};
    int *iArr = concat(iArr1, 5, iArr2, 5);
    print(iArr, 10);

    double dArr1[5] = {1.2, 3.4, 4.5, 5.6, 7.8};
    double dArr2[5] = {8.9, 0.1, 2.3, 4.5, 6.7};
    double *dArr = concat(dArr1, 5, dArr2, 5);
    print(dArr, 10);

    char cArr1[5] = {'A', 'B', 'C', 'D', 'E'};
    char cArr2[5] = {'F', 'G', 'H', 'I', 'J'};
    char *cArr = concat(cArr1, 5, cArr2, 5);
    print(cArr, 10);

}

ย 
_์‹ค์Šต ๋ฌธ์ œ 10 - 6
๋ฌธ์ œ : ์ œ๋„ค๋ฆญ ํ•จ์ˆ˜ equalArrays() ์ž‘์„ฑํ•˜๊ธฐ

#include <iostream>
using namespace std;

template <class T>
T* remove(T src[], int sizeSrc, T minus[], int sizeMinus, int &retsize)
{
    for(int i = 0; i < sizeMinus; i++)
    {
        for(int j = 0; j < sizeSrc;)
        {
            if(minus[i] == src[j])
            {
                for(int k = j; k < sizeSrc - 1; k++)
                {
                    src[k] = src[k + 1];
                }
                sizeSrc--;
                if(sizeSrc == 0)
                    return src;
                continue;
            }
            j++;
        }
    }
    retsize = sizeSrc;
    T *arr = new T[retsize];
    for(int i = 0; i < retsize; i++)
    {
        arr[i] = src[i];
    }
    return arr;
}

template <class T>
void print(T *arr, int size)
{
    for(int i = 0; i < size; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
}

int main()
{
    int size = 0;
    int iArr1[5] = {1, 2, 3, 4, 5};
    int iArr2[5] = {4, 7, 5, 9, 10};
    int *iArr = remove(iArr1, 5, iArr2, 5, size);
    print(iArr, size);

    double dArr1[5] = {1.2, 3.4, 4.5, 5.6, 7.8};
    double dArr2[5] = {8.9, 3.4, 2.3, 4.5, 1.2};
    double *dArr = remove(dArr1, 5, dArr2, 5, size);
    print(dArr, size);

    char cArr1[5] = {'A', 'B', 'C', 'D', 'E'};
    char cArr2[5] = {'A', 'G', 'H', 'I', 'E'};
    char *cArr = remove(cArr1, 5, cArr2, 5, size);
    print(cArr, size);

}

ย 
_์‹ค์Šต ๋ฌธ์ œ 10 - 7
๋ฌธ์ œ : ์˜ค๋ฅ˜ ์ฐพ๊ธฐ
ํด๋ž˜์Šค์˜ ๊ฐ„์˜ ํฌ๊ธฐ ๋น„๊ต๋ฅผ ํ•  ์ˆ˜ ์—†์–ด์„œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ

#include <iostream>
using namespace std;

class Circle{
    int radius;
public:
    Circle(int radius = 1) {this->radius = radius;}
    int getRadius() {return radius;}
};

template <class T>
T bigger(T a, T b)
{
     return a > b ? a : b;
}

int main()
{
    int a = 20, b = 50, c;
    c = bigger(a, b);
    cout << "20๊ณผ 50์ค‘ ํฐ ๊ฐ’์€ " << c << endl;
    Circle waffle(10), pizza(20), y;
    y = bigger(waffle.getRadius(), pizza.getRadius());
    cout << "waffle๊ณผ pizza ์ค‘ ํฐ ๊ฒƒ์˜ ๋ฐ˜์ง€๋ฆ„์€ " << y.getRadius() << endl;
}

ย 
_์‹ค์Šต ๋ฌธ์ œ 10 - 8
๋ฌธ์ œ : 7๋ฒˆ ๋ฌธ์ œ ์—ฐ์‚ฐ์ž ์ค‘๋ณต์œผ๋กœ ํ•ด๊ฒฐํ•˜๊ธฐ

#include <iostream>
using namespace std;

class Comparable{
public:
    virtual bool operator > (Comparable& op2) = 0;
    virtual bool operator < (Comparable& op2) = 0;
    virtual bool operator == (Comparable& op2) = 0;
};
class Circle : public Comparable{
    int radius;
public:
    Circle(int radius = 1) {this->radius = radius;}
    int getRadius() {return radius;}
    // ๋‹ค์šด์บ์ŠคํŒ…
    bool operator > (Comparable &op2) override {
        Circle *temp = (Circle *)&op2;
        if(this->radius > temp->radius)
            return true;
        return false;
    }
    bool operator < (Comparable &op2) override {
        Circle *temp = (Circle *)&op2;
        if(this->radius < temp->radius)
            return true;
        return false;
    }
    bool operator == (Comparable &op2) override {
        Circle *temp = (Circle *)&op2;
        if(this->radius == temp->radius)
            return true;
        return false;
    }
};

template <class T>
T bigger(T a, T b)
{
     return a > b ? a : b;
}

template <class T>
T smaller(T a, T b)
{
    return a < b ? a : b;
}

template <class T>
bool equal(T a, T b)
{
    return a == b;
}

int main()
{
    int a = 20, b = 50, c;
    c = bigger(a, b);
    cout << "20๊ณผ 50์ค‘ ํฐ ๊ฐ’์€ " << c << endl;
    Circle waffle(10), pizza(20), y;
    y = bigger(waffle, pizza);
    cout << "waffle๊ณผ pizza ์ค‘ ํฐ ๊ฒƒ์˜ ๋ฐ˜์ง€๋ฆ„์€ " << y.getRadius() << endl;

    Circle waffle1(10), pizza1(20), y1;

    y1 = smaller(waffle1, pizza1);
    cout << "waffle๊ณผ pizza ์ค‘ ์ž‘์€ ๊ฒƒ์€ ๋ฐ˜์ง€๋ฆ„์€ " << y1.getRadius() << endl;

    Circle waffle2(20), pizza2(20);
    if(equal(waffle2, pizza2))
    {
        cout << "waffle๊ณผ pizza์˜ ๋ฐ˜์ง€๋ฆ„์€ ์„œ๋กœ ๊ฐ™๋‹ค.";
    }
    else
        cout << "waffle๊ณผ pizza์˜ ๋ฐ˜์ง€๋ฆ„์€ ์„œ๋กœ ๋‹ค๋ฅด๋‹ค.";

}

ย 
_์‹ค์Šต ๋ฌธ์ œ 10 - 9
๋ฌธ์ œ : STL์˜ vectorํด๋ž˜์Šค ์ด์šฉํ•˜๊ธฐ

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    vector<int> v;
    int num = 0, sum = 0;
    while(true)
    {
        cout << "์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”(0์„ ์ž…๋ ฅํ•˜๋ฉด ์ข…๋ฃŒ)" << endl;
        cin >> num;
        if(num == 0)
            break;
        sum += num;
        v.push_back(num);

        for_each(v.begin(), v.end(), [](int n) {cout << n << " ";});
        cout << endl << "ํ‰๊ท  = " << (double)sum / v.size() << endl;
    }
}

ย 
_์‹ค์Šต ๋ฌธ์ œ 10 - 10
๋ฌธ์ œ : vector๋ฅผ ์ด์šฉํ•ด ๋‚˜๋ผ ์ด๋ฆ„ ์ˆ˜๋„ ๋งž์ถ”๊ธฐ ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ

#include <iostream>
#include <string>
#include <vector>
#include <random>
using namespace std;

class Nation{
    string nation;
    string capital;
public:
    Nation(string nation, string capital){
        this->nation = nation;
        this->capital = capital;
    }
    void setInfo(string nation, string capital)
    {
        this->nation = nation;
        this->capital = capital;
    }
    string getNation(){
        return nation;
    }
    string getCapital(){
        return capital;
    }
};

int main()
{
    vector<Nation> v = {Nation("ํ•œ๊ตญ", "์„œ์šธ"), Nation("์ผ๋ณธ", "๋„์ฟ„"),Nation("์˜๊ตญ", "๋Ÿฐ๋˜"),
                        Nation("๋ฏธ๊ตญ", "์™€์‹ฑ"), Nation("๋…์ผ", "๋ฒ ๋ฅผ๋ฆฐ"), Nation("์ค‘๊ตญ", "๋ฒ ์ด์ง•"),
                        Nation("ํ”„๋ž‘์Šค", "ํŒŒ๋ฆฌ"), Nation("ํƒœ๊ตญ", "๋ฐฉ์ฝ•"), Nation("ํ˜ธ์ฃผ", "์บ”๋ฒ„๋ผ")};
    vector<Nation>::iterator it;
    cout << "***** ๋‚˜๋ผ์˜ ์ˆ˜๋„ ๋งž์ถ”๊ธฐ ๊ฒŒ์ž„์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. *****" << endl;
    random_device rd;
    mt19937 gen(rd());
    uniform_int_distribution dist;

    int num = 0;
    string nation, capital;
    while (true)
    {
        cout << "์ •๋ณด ์ž…๋ ฅ: 1, ํ€ด์ฆˆ: 2, ์ข…๋ฃŒ :3 >> ";
        cin >> num;
        if(num == 1)
        {
            cout << "ํ˜„์žฌ " << v.size() << "๊ฐœ์˜ ๋‚˜๋ผ๊ฐ€ ์ž…๋ ฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค." << endl;
            cout << "๋‚˜๋ผ์™€ ์ˆ˜๋„๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”(no no์ด๋ฉด ์ž…๋ ฅ๋)" << endl;
            while(true)
            {
                bool chk = true;
                cout << v.size() + 1 << ">>";
                cin >> nation >> capital;
                if(nation == "no" && nation == "no")
                    break;
                for(it = v.begin(); it != v.end(); it++)
                {
                    if(it->getNation() == nation)
                    {
                        cout << "already exists !!" << endl;
                        chk = false;
                    }
                }
                if(chk)
                {
                    Nation temp(nation, capital);
                    v.push_back(temp);
                }
            }
        }
        if(num == 2)
        {
            while(true)
            {
                int n = dist(gen) % v.size();
                cout << v[n].getNation() << "์˜ ์ˆ˜๋„๋Š”?";
                cin >> capital;
                if(capital == "exit")
                    break;
                else if(capital == v[n].getCapital())
                    cout << "Correct !!" << endl;
                else
                    cout << "NO !!" << endl;
            }
        }
        if(num == 3)
            break;
    }
}

ย 
_์‹ค์Šต ๋ฌธ์ œ 10 - 11
๋ฌธ์ œ : vector์™€ Bookํด๋ž˜์Šค๋กœ ์ €์ž์™€ ๋…„๋„๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑํ•˜๊ธฐ

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Book{
    string name;
    string bookName;
    int year;
public:
    Book(int year, string bookName, string name){
        this->year = year;
        this->bookName = bookName;
        this->name = name;
    }
    string getName() {return name;}
    int getYear() {return year;}

    void show(){
        cout << year << "๋…„๋„, " << bookName << ", " << name << endl;
    }
};

int main()
{
    vector<Book> v;
    string name;
    string bookName;
    int year;
    cout << "์ž…๊ณ ํ•  ์ฑ…์„ ์ž…๋ ฅํ•˜์„ธ์š”. ๋…„๋„์— -1์„ ์ž…๋ ฅํ•˜๋ฉด ์ž…๊ณ ๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค." << endl;
    while(true)
    {
        cout << "๋…„๋„>>";
        cin >> year;
        cin.ignore();
        if(year == -1)
            break;
        cout << "์ฑ…์ด๋ฆ„>>";
        getline(cin, bookName);
        cout << "์ €์ž>>";
        getline(cin, name);
        Book temp(year, bookName, name);
        v.push_back(temp);
    }
    cout << "์ด ์ž…๊ณ ๋œ ์ฑ…์€ " << v.size() <<"๊ถŒ์ž…๋‹ˆ๋‹ค." << endl;
    int num = 0;
    while(true)
    {
        cout << "์–ด๋–ค๊ฑธ๋กœ ๊ฒ€์ƒ‰ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? 1: ์ €์ž์ด๋ฆ„ 2: ๋…„๋„ 3: ์ข…๋ฃŒ>>";
        cin >> num;
        if(num == 1)
        {
            cin.ignore();
            cout << "์ €์ž ์ด๋ฆ„>>";
            getline(cin, name);
            for(auto arr : v){
                if(arr.getName() == name)
                    arr.show();
            }
        }
        if(num == 2)
        {
            cin.ignore();
            cout << "๋…„๋„>>";
            cin >> year;
            for(auto arr : v){
                if(arr.getYear() == year)
                    arr.show();
            }
        }
        if(num == 3)
            break;
    }
}

ย 
_์‹ค์Šต ๋ฌธ์ œ 10 - 12
๋ฌธ์ œ : ๋‹จ์–ด ์‹œํ—˜ ํ…Œ์ŠคํŠธ ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ

#include <iostream>
#include <vector>
#include <random>
using namespace std;

class Word{
    string jpn;
    string kor;
public:
    Word(){
        jpn = "";
        kor = "";
    }
    Word(string eng, string kor) { this->jpn = eng; this->kor = kor;}
    void setJpn(string eng){this->jpn = eng;}
    void setKor(string kor){this->kor = kor;}

    string getJpn(){return jpn;}
    string getKor(){return kor;}
};

int main()
{
    string jpn;
    string kor;
    int num = 0;
    random_device rd;
    mt19937 gen(rd());
    uniform_int_distribution<int> dist;
    Word words[4];
    vector<Word> v = {Word("ใ‚‚ใ‚‚", "๋ณต์ˆญ์•„"), Word("ใ„ใกใ”", "๋”ธ๊ธฐ"),
                      Word("ใฟใ‹ใ‚“", "๊ทค"), Word("ใƒใƒŠใƒŠ", "๋ฐ”๋‚˜๋‚˜")};
    cout << "***** ์ผ์–ด ์–ดํœ˜ ํ…Œ์ŠคํŠธ๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. *****" << endl;
    while(true)
    {
        cout << "์–ดํœ˜ ์‚ฝ์ž…: 1, ์–ดํœ˜ ํ…Œ์ŠคํŠธ: 2, ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ:๊ทธ์™ธํ‚ค >> ";
        cin >> num;
        if(num == 1)
        {
            cout << "์ผ์–ด ๋‹จ์–ด์— ใŠใ‚ใ‚Š๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ž…๋ ฅ ๋" << endl;
            while (true)
            {
                cout << "์ผ์–ด >>";
                cin >> jpn;
                if(jpn == "ใŠใ‚ใ‚Š")
                    break;
                cout << "ํ•œ๊ธ€ >>";
                cin >> kor;
                Word temp(jpn, kor);
                v.push_back(temp);
            }
        }
        else if(num == 2)
        {
            int n, ans, pos;
            cout << "์ผ์–ด ์–ดํœ˜ ํ…Œ์ŠคํŠธ๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. 1 ~ 4 ์™ธ ๋‹ค๋ฅธ ์ž…๋ ฅ์‹œ ์ข…๋ฃŒ.";
            while (true)
            {
                Word temp;
                for(int i = 0; i < 4; i++)
                {
                    words[i] = temp;
                }
                ans = dist(gen) % v.size();
                cout << v[ans].getJpn() << "?" <<endl;
                pos = dist(gen) % 4;
                words[pos] = v[ans];
                int cnt = 1;
                while (cnt < 4)
                {
                    n = dist(gen) % v.size();
                    for(int i = 0; i < 4; i++)
                    {
                        if(v[n].getJpn() == words[i].getJpn())
                            break;
                        if(i == 3)
                        {
                            for(int j = 0; j < 4; j++)
                            {
                                if(words[j].getJpn() == words[j].getKor())
                                {
                                    words[j] = v[n];
                                    cnt++;
                                    break;
                                }
                            }
                        }
                    }
                }
                for(int i = 0; i < 4; i++)
                {
                    cout << "(" << i + 1 << ") " << words[i].getKor() << " ";
                }
                int result = 0;
                cout << ":>>";
                cin >> result;
                if(1 <= result && result <= 4)
                {
                    if(result - 1 == pos)
                        cout << "Excellent !!" << endl;
                    else
                    {
                        cout << "No. !!" << endl;
                    }
                }
                else
                {
                    break;
                }
            }
        }
        else
            break;
        cout << endl;
    }
}

ย 
_์‹ค์Šต ๋ฌธ์ œ 10 - 13
๋ฌธ์ œ : map์ปจํ…Œ์ด๋„ˆ ์ด์šฉํ•ด์„œ ์„ฑ์ ์กฐํšŒ ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main()
{
    map<string, int> student;
    int num;
    int score;
    string name;
    cout << "***** ์ ์ˆ˜๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ HIGH SCORE์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค *****" << endl;
    while(true)
    {
        cout << "์ž…๋ ฅ:1, ์กฐํšŒ:2, ์ข…๋ฃŒ:3 >> ";
        cin >> num;
        if(num == 3)
        {
            cout << "ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค..." << endl;
            break;
        }
        if(num == 1)
        {
            cout << "์ด๋ฆ„๊ณผ ์ ์ˆ˜>> ";
            cin >> name >> score;
            student[name] = score;
        }
        if(num == 2)
        {
            cout << "์ด๋ฆ„>> ";
            cin >> name;
            cout << name << "์˜ ์ ์ˆ˜๋Š” " << student[name] << endl;
        }
    }
}

ย 
_์‹ค์Šต ๋ฌธ์ œ 10 - 14
๋ฌธ์ œ : ์•”ํ˜ธ ๊ด€๋ฆฌ ์‘์šฉํ”„๋กœ๊ทธ๋žจ์„ map์„ ์ด์š”ํ•ด ๋งŒ๋“ค๊ธฐ

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main()
{
    map<string, string> student;
    int num;
    string key;
    string name;
    cout << "***** ์•”ํ˜ธ๊ด€๋ฆฌ ํ”„๋กœ๊ทธ๋žจ WHO๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค *****" << endl;
    while(true)
    {
        cout << "์‚ฝ์ž…:1, ๊ฒ€์‚ฌ:2, ์ข…๋ฃŒ:3 >> ";
        cin >> num;
        if(num == 3)
        {
            cout << "ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค..." << endl;
            break;
        }
        if(num == 1)
        {
            cout << "์ด๋ฆ„ ์•”ํ˜ธ>> ";
            cin >> name >> key;
            student[name] = key;
        }
        if(num == 2)
        {
            cout << "์ด๋ฆ„? ";
            cin >> name;
            while(true){
                cout << "์•”ํ˜ธ? ";
                cin >> key;
                if(student[name] == key){
                    cout << "ํ†ต๊ณผ!!" << endl;
                    break;
                }
                else
                    cout << "์‹คํŒจ~~" << endl;
            }

        }
    }
}

ย 
_์‹ค์Šต ๋ฌธ์ œ 10 - 15
๋ฌธ์ œ : vector๋ฅผ ์ด์šฉํ•ด Circleํด๋ž˜์Šค ์‚ฝ์ž… ์‚ญ์ œํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค๊ธฐ

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Circle{
    string name;
    int radius;
public:
    Circle(int radius, string name)
    {
        this->radius = radius;
        this->name = name;
    }
    double getArea(){ return 3.14 * radius * radius; }
    string getName(){ return name; }
};

int main()
{
    vector<Circle*> v;
    vector<Circle*>::iterator it;
    string name;
    int radius;
    int num;
    cout << "์›์„ ์‚ฝ์ž…ํ•˜๊ณ  ์‚ญ์ œํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ž…๋‹ˆ๋‹ค." << endl;
    while (true){
        cout <<"์‚ฝ์ž…:1, ์‚ญ์ œ:2, ๋ชจ๋‘๋ณด๊ธฐ:3, ์ข…๋ฃŒ:4 >> ";
        cin >> num;
        if(num == 4)
            break;
        if(num == 1)
        {
            cout << "์ƒ์„ฑํ•˜๊ณ ์ž ํ•˜๋Š” ์›์˜ ๋ฐ˜์ง€๋ฆ„๊ณผ ์ด๋ฆ„์€ >> ";
            cin >> radius >> name;
            v.push_back(new Circle(radius, name));
        }
        if(num == 2)
        {
            cout << "์‚ญ์ œํ•˜๊ณ ์ž ํ•˜๋Š” ์›์˜ ์ด๋ฆ„์€ >> ";
            cin >> name;
            it = v.begin();
            while(it != v.end())
            {
                Circle *temp = *it;
                if(temp->getName() == name) {
                    it = v.erase(it);
                }
                else
                    it++;
            }

        }
        if(num == 3)
        {
            for(it = v.begin(); it != v.end(); it++)
            {
                Circle *temp = *it;
                cout << temp->getName() << endl;
            }
            cout << endl;
        }
    }


}

ย 
_์‹ค์Šต ๋ฌธ์ œ 10 - 16
๋ฌธ์ œ : vector<Shape*>v; ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•œ ๊ทธ๋ž˜ํ”ฝ ํŽธ์ง‘๊ธฐ๋ฅผ ์ฝ˜์†” ๋ฐฉํƒ•์œผ๋กœ ๋งŒ๋“ค๊ธฐ
โš ๏ธShape, Circle, Rect, Line ํด๋ž˜์Šค์— ํ•ด๋‹นํ•˜๋Š” cppํŒŒ์ผ๊ณผ ํ—ค๋”ํŒŒ์ผ์€ ์ฑ…์— ๋‚˜์™€์žˆ๋Š”๋Œ€๋กœ ์ž‘์„ฑ

#include <iostream>
#include <string>
#include <vector>
#include "Shape.h"
#include "Circle.h"
#include "Rect.h"
#include "Line.h"
using namespace std;

int main()
{
    vector<Shape*> v;
    vector<Shape*>::iterator it;
    int num;
    int sNum;
    cout << "๊ทธ๋ž˜ํ”ฝ ์—๋””ํ„ฐ์ž…๋‹ˆ๋‹ค." << endl;
    while (true)
    {
        cout << "์‚ฝ์ž…:1, ์‚ญ์ œ:2, ๋ชจ๋‘๋ณด๊ธฐ:3, ์ข…๋ฃŒ:4 >> ";
        cin >> num;
        if(num == 4)
            break;
        if(num == 1)
        {
            cout << "์„ :1, ์›:2, ์‚ฌ๊ฐํ˜•:3 >> ";
            cin >> sNum;
            if(sNum == 1)
            {
                v.push_back(new Line);
            }
            if(sNum == 2)
            {
                v.push_back(new Circle);
            }
            if(sNum == 3)
            {
                v.push_back(new Rect);
            }
        }
        if(num == 2)
        {
            cout << "์‚ญ์ œํ•˜๊ณ ์ž ํ•˜๋Š” ๋„ํ˜•์˜ ์ธ๋ฑ์Šค >> ";
            cin >> sNum;
            it = v.begin();
            int i = 0;
            while(it != v.end())
            {
                if(i == sNum)
                {
                    it = v.erase(it);
                    break;
                }
                else
                {
                    it++;
                    i++;
                }
            }
        }
        if(num == 3)
        {
            int i;
            for(it = v.begin(), i = 0; it != v.end(); it++, i++)
            {
                Shape *temp = *it;
                cout << i << ": ";
                temp->paint();
            }
        }
    }
}

ย 

profile
์น˜ํƒ€๊ฐ€ ๋˜๊ณ  ์‹ถ์€ ์ทจ์ค€์ƒ

0๊ฐœ์˜ ๋Œ“๊ธ€