hugDog
Android DevLog
hugDog
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๐Ÿ™Œ Hello? (162)
    • ๐Ÿงฉ์•ˆ๋“œ๋กœ์ด๋“œ (12)
      • ๊ฐœ๋… ์ •๋ฆฌ (5)
      • ๋ฒ„๊ทธ ํ•ด๊ฒฐ (4)
      • ๊ธฐํƒ€ (3)
    • ๐Ÿ”์•Œ๊ณ ๋ฆฌ์ฆ˜ (54)
      • ๊ฐœ๋… (0)
      • ๋ฐฑ์ค€ (48)
      • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (6)
    • ๐Ÿ“„๊ฐœ๋ฐœ ์ผ์ง€ (0)
      • FINPO (0)
    • ๐Ÿ”คํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด (71)
      • C++ ์ •๋ฆฌ (49)
      • C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค ์—ฐ์Šต๋ฌธ์ œ (20)
      • Kotlin (2)
    • โญProject (1)
    • ๐ŸšดTIL (13)
      • Clean Code (13)
    • ๐Ÿšฉ๊ธฐํƒ€ (9)
      • ๋ชฉํ‘œ (6)
      • ์ผ์ƒ (3)
      • ๋ฌธ์„œ (0)

์ธ๊ธฐ ๊ธ€

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
hugDog

Android DevLog

๐Ÿ”คํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค ์—ฐ์Šต๋ฌธ์ œ

(C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 16 string ํด๋ž˜์Šค์™€ ํ‘œ์ค€ ํ…œํ”Œ๋ฆฟ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ p.1342~ 1๋ฒˆ~10๋ฒˆ

2020. 10. 19. 14:45
728x90

1๋ฒˆ

#include<iostream>
#include<string>

bool palindrome(const std::string& s);

int main()
{
	std::string temp;
	std::cout << "๋ฌธ์ž์—ด ์ž…๋ ฅ: ";
	getline(std::cin, temp);
	if (palindrome(temp))
		std::cout <<temp <<"๋Š” ํšŒ๋ฌธ์ž…๋‹ˆ๋‹ค.\n";
	else
		std::cout <<temp<< "๋Š” ํšŒ๋ฌธ์ด ์•„๋‹™๋‹ˆ๋‹ค.\n";

	return 0;
}

bool palindrome(const std::string& s)
{
	std::string t(s.rbegin(), s.rend());
	return t == s;
}

 

2๋ฒˆ

#include<iostream>
#include<string>
#include<cctype>
#include<new>
bool palindrome(const std::string& s);
void change(std::string& s);

int main()
{
	std::string temp;
	std::cout << "๋ฌธ์ž์—ด ์ž…๋ ฅ: ";
	getline(std::cin, temp);
	change(temp);
	std::cout << temp << '\n';
	if (palindrome(temp))
		std::cout <<temp <<"๋Š” ํšŒ๋ฌธ์ž…๋‹ˆ๋‹ค.\n";
	else
		std::cout <<temp<< "๋Š” ํšŒ๋ฌธ์ด ์•„๋‹™๋‹ˆ๋‹ค.\n";

	return 0;
}

bool palindrome(const std::string& s)
{
	std::string t(s.rbegin(), s.rend());
	return t == s;
}

void change(std::string& s)
{
	std::string t;
	for (int i=0; i <s.size(); ++i)
	{
		if (isalpha(s[i]))
		{
			s[i] = tolower(s[i]);
			t += s[i];
		}
	}
	s = t;
}

 

 

3๋ฒˆ

// hangman.cpp -- string ๋ฉ”์„œ๋“œ๋“ค
#include <iostream>
#include<fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include<vector>
using std::string;
using std::vector;
vector<string> wordlist;

int main()
{
    using std::cout;
    using std::cin;
    using std::tolower;
    using std::endl;
    std::ifstream inFile;
    inFile.open("wordlist.txt");
    while (inFile.good())
    {
        std::string temp;
        inFile >> temp;
        wordlist.push_back(temp);
    }
    inFile.close();
    const int NUM = wordlist.size();
    std::srand(std::time(0));
    char play;
    cout << "์˜์–ด ๋‹จ์–ด ๊ฒŒ์ž„์„ ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? <y/n> ";
    cin >> play;
    play = tolower(play);
    while (play == 'y')
    {
        string target = wordlist[std::rand() % NUM];
        int length = target.length();
        string attempt(length, '-');
        string badchars;
        int guesses = 6;
        cout << "์ˆ˜์ˆ˜๊ป˜๋ผ ๋‹จ์–ด๋ฅผ ์ถ”์ธกํ•ด ๋ณด์‹ญ์‹œ์˜ค.\n"
            << length << "๊ฐœ์˜ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.\n"
            << "ํ•œ ๋ฒˆ์— ํ•œ ๋ฌธ์ž์”ฉ ์ถ”์ธกํ•˜์‹ญ์‹œ์˜ค.\n"
            << "ํ‹€๋ฆด ์ˆ˜ ์žˆ๋Š” ๊ธฐํšŒ: " << guesses << "๋ฒˆ\n";
        cout << "์ถ”์ธกํ•˜๋Š” ๋‹จ์–ด: " << attempt << endl;
        while (guesses > 0 && attempt != target)
        {
            char letter;
            cout << "๋ฌธ์ž๋ฅผ ์ถ”์ธกํ•˜์‹ญ์‹œ์˜ค: ";
            cin >> letter;
            if (badchars.find(letter) != string::npos
                || attempt.find(letter) != string::npos)
            {
                cout << "์ด๋ฏธ ์ถ”์ธกํ•œ ๋ฌธ์ž์ž…๋‹ˆ๋‹ค. ๋‹ค์‹œ ํ•˜์‹ญ์‹œ์˜ค.\n";
                continue;
            }
            int loc = target.find(letter);
            if (loc == string::npos)
            {
                cout << "๋•ก! ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค.\n";
                --guesses;
                badchars += letter; // ํ‹€๋ฆฐ ์ถ”์ธก๋“ค์„ ๋ฌธ์ž์—ด์— ์ถ”๊ฐ€ํ•œ๋‹ค
            }
            else
            {
                cout << "๋”ฉ๋™๋Œ•! ๋งž์•˜์Šต๋‹ˆ๋‹ค.\n";
                attempt[loc] = letter;
                // ๊ฐ™์€ ๋ฌธ์ž๊ฐ€ ๋˜ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค
                loc = target.find(letter, loc + 1);
                while (loc != string::npos)
                {
                    attempt[loc] = letter;
                    loc = target.find(letter, loc + 1);
                }
            }
            cout << "์ถ”์ธกํ•˜๋Š” ๋‹จ์–ด: " << attempt << endl;
            if (attempt != target)
            {
                if (badchars.length() > 0)
                    cout << "ํ‹€๋ฆฌ๊ฒŒ ์ถ”์ธกํ•œ ๋ฌธ์ž๋“ค: " << badchars << endl;
                cout << "ํ‹€๋ฆด ์ˆ˜ ์žˆ๋Š” ๊ธฐํšŒ: " << guesses << "๋ฒˆ\n";
            }
        }
        if (guesses > 0)
            cout << "๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ์ด ์ˆ˜์ˆ˜๊ป˜๋ผ ๋‹จ์–ด์ž…๋‹ˆ๋‹ค.\n";
        else
            cout << "์•ˆํƒ€๊น์Šต๋‹ˆ๋‹ค. ์ˆ˜์ˆ˜๊ป˜๋ผ ๋‹จ์–ด๋Š” " << target << "์ž…๋‹ˆ๋‹ค.\n";

        cout << "๊ฒŒ์ž„์„ ๋‹ค์‹œ ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? <y/n> ";
        cin >> play;
        play = tolower(play);
    }

    cout << "ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.\n";

    return 0;
}

 

 

4๋ฒˆ

#include<iostream>
#include<algorithm>
int reduce(long ar[], int n);
int main()
{
	long a[5] = { 8,2,4,5,2 };
	int n = reduce(a, 5);
	std::cout << n;
	return 0;
}

int reduce(long ar[], int n)
{
	std::sort(ar, ar + n);
	long* pastend = std::unique(ar, ar + n);
	return pastend - ar;
}

 

 

5๋ฒˆ

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

template<class T>
int reduce(T ar[], int n);

template<class T>
void Show(const T ar[], int n);

int main()
{
	long a[5] = { 8,2,4,5,2 };
	string b[4] = { "abc","fdd","eer","abc" };
	int n=reduce(a, 5);
	Show(a, n);
	n = reduce(b, 4);
	Show(b, n);
	return 0;
}

template<class T>
int reduce(T ar[], int n)
{
	sort(ar, ar + n);
	T* pastend=unique(ar, ar + n);
	return pastend - ar;
}

template<class T>
void Show(const T ar[], int n)
{
	for (int i = 0; i < n; i++)
		cout << ar[i] << " ";
	cout<<endl;
}

 

 

6๋ฒˆ

// queue.h -- ํ๋ฅผ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค
#ifndef QUEUE_H_
#define QUEUE_H_
// ์ด ํ๋Š” Customer ํ•ญ๋ชฉ๋“ค์„ ํฌํ•จํ•˜๊ฒŒ ๋œ๋‹ค
class Customer
{
private:
    long arrive;        // ๊ณ ๊ฐ์ด ํ์— ๋„์ฐฉํ•œ ์‹œ๊ฐ„
    int processtime;    // ๊ณ ๊ฐ์˜ ๊ฑฐ๋ž˜๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์‹œ๊ฐ„
public:
    Customer() { arrive = processtime = 0; }
    void set(long when);
    long when() const { return arrive; }
    int ptime() const { return processtime; }
};
typedef Customer Item;

#endif


// queue.cpp -- Queue์™€ Customer ๋ฉ”์„œ๋“œ๋“ค
#include "queue.h"
#include <cstdlib>         // (๋˜๋Š” stdlib.h) rand()๋ฅผ ์œ„ํ•ด
// customer ๋ฉ”์„œ๋“œ

// when์€ ๊ณ ๊ฐ์ด ๋„์ฐฉํ•œ ์‹œ๊ฐ„์ด๋‹ค
// ๋„์ฐฉ ์‹œ๊ฐ„์€ when์œผ๋กœ ์„ค์ •๋œ๋‹ค
// ์ฒ˜๋ฆฌ ์‹œ๊ฐ„์€ 1, 2, 3 ์ค‘์—์„œ ๋ฌด์ž‘์œ„๋กœ ํ•œ ๊ฐ’์ด ์„ค์ •๋œ๋‹ค
void Customer::set(long when)
{
    processtime = std::rand() % 3 + 1;
    arrive = when;
}



// bank.cpp -- Queue ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค
// queue.cpp์™€ ํ•จ๊ป˜ ์ปดํŒŒ์ผํ•œ๋‹ค
#include <iostream>
#include <cstdlib> // rand()์™€ srand()๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด
#include <ctime>   // time()์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด
#include "queue.h"
#include<queue>
const int MIN_PER_HR = 60;

bool newcustomer(double x); // ์ƒˆ ๊ณ ๊ฐ์ด ๋„์ฐฉํ–ˆ๋Š”๊ฐ€?

int main()
{
    using std::cin;
    using std::cout;
    using std::endl;
    using std::queue;
    using std::ios_base;
    // ํ•„์š”ํ•œ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋ฅผ ์ค€๋น„ํ•œ๋‹ค
    std::srand(std::time(0));    //  rand()์˜ ๋ฌด์ž‘์œ„ ์ดˆ๊ธฐํ™”

    cout << "์‚ฌ๋ก€ ์—ฐ๊ตฌ: ํžˆ์„œ ์€ํ–‰์˜ ATM\n";
    cout << "ํ์˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค: ";
    int qs;
    cin >> qs;
    queue<Item> line;         // line ํ์—๋Š” ์ตœ๋Œ€ qs๋ช…๊นŒ์ง€ ๋Œ€๊ธฐํ•  ์ˆ˜ ์žˆ๋‹ค

    cout << "์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์‹œ๊ฐ„ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค: ";
    int hours;              // ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์‹œ๊ฐ„ ์ˆ˜
    cin >> hours;
    // ์‹œ๋ฎฌ๋ ˆ์ด์…˜์€ 1๋ถ„์— 1์ฃผ๊ธฐ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค
    long cyclelimit = MIN_PER_HR * hours; // ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ์ฃผ๊ธฐ ์ˆ˜

    cout << "์‹œ๊ฐ„๋‹น ํ‰๊ท  ๊ณ ๊ฐ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค: ";
    double perhour;         //  ์‹œ๊ฐ„๋‹น ํ‰๊ท  ๊ณ ๊ฐ ์ˆ˜
    cin >> perhour;
    double min_per_cust;    //  ํ‰๊ท  ๊ณ ๊ฐ ๋„์ฐฉ ๊ฐ„๊ฒฉ(๋ถ„ ๋‹จ์œ„)
    min_per_cust = MIN_PER_HR / perhour;

    Item temp;              //  ์ƒˆ ๊ณ ๊ฐ ๋ฐ์ดํ„ฐ
    long turnaways = 0;     //  ํ๊ฐ€ ๊ฐ€๋“ ์ฐจ์„œ ๋ฐœ๊ธธ์„ ๋Œ๋ฆฐ ๊ณ ๊ฐ ์ˆ˜
    long customers = 0;     //  ํ์— ์ค„์„ ์„  ๊ณ ๊ฐ ์ˆ˜
    long served = 0;        //  ์‹œ๋ฎฌ๋ ˆ์ด์…˜์—์„œ ๊ฑฐ๋ž˜๋ฅผ ์ฒ˜๋ฆฌํ•œ ๊ณ ๊ฐ ์ˆ˜
    long sum_line = 0;      //  ํ์˜ ๋ˆ„์  ๊ธธ์ด
    int wait_time = 0;      //  ATM์ด ๋นŒ ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐํ•˜๋Š” ์‹œ๊ฐ„
    long line_wait = 0;     //  ๊ณ ๊ฐ๋“ค์ด ์ค„์„ ์„œ์„œ ๋Œ€๊ธฐํ•œ ๋ˆ„์  ์‹œ๊ฐ„


// ์‹œ๋ฎฌ๋ ˆ์ด์…˜์„ ์‹คํ–‰ํ•œ๋‹ค
    for (int cycle = 0; cycle < cyclelimit; cycle++)
    {
        if (newcustomer(min_per_cust))  // ์ƒˆ ๊ณ ๊ฐ์ด ๋„์ฐฉํ–ˆ๋‹ค
        {
            if (qs==line.size())
                turnaways++;
            else
            {
                customers++;
                temp.set(cycle);    // cycle์ด ๋„์ฐฉ ์‹œ๊ฐ„์ด ๋œ๋‹ค
                line.push(temp); // ํ์— ์ƒˆ ๊ณ ๊ฐ์„ ์ถ”๊ฐ€ํ•œ๋‹ค
 
            }
        }
        if (wait_time <= 0 && line.size()!=0)
        {
            temp = line.front();
            line.pop();      // ๋‹ค์Œ ๊ณ ๊ฐ์„ ๋ฐ›๋Š”๋‹ค
            wait_time = temp.ptime(); // wait_time์„ ์„ค์ •ํ•œ๋‹ค
            line_wait += cycle - temp.when();
            served++;
        }
        if (wait_time > 0)
            wait_time--;
        sum_line += line.size();
    }

    // ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค
    if (customers > 0)
    {
        cout << " ํ์— ์ค„์„ ์„  ๊ณ ๊ฐ ์ˆ˜: " << customers << endl;
        cout << "๊ฑฐ๋ž˜๋ฅผ ์ฒ˜๋ฆฌํ•œ ๊ณ ๊ฐ ์ˆ˜: " << served << endl;
        cout << " ๋ฐœ๊ธธ์„ ๋Œ๋ฆฐ ๊ณ ๊ฐ ์ˆ˜: " << turnaways << endl;
        cout << "          ํ‰๊ท  ํ์˜ ๊ธธ์ด: ";
        cout.precision(2);
        cout.setf(ios_base::fixed, ios_base::floatfield);
        cout.setf(ios_base::showpoint);
        cout << (double)sum_line / cyclelimit << endl;
        cout << "       ํ‰๊ท  ๋Œ€๊ธฐ ์‹œ๊ฐ„: "
            << (double)line_wait / served << "๋ถ„\n";
    }
    else
        cout << "๊ณ ๊ฐ์ด ํ•œ ๋ช…๋„ ์—†์Šต๋‹ˆ๋‹ค!\n";
    cout << "์™„๋ฃŒ!\n";
    return 0;
}

//  x๋Š” ๊ณ ๊ฐ ๊ฐ„์˜ ํ‰๊ท  ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์ด๋‹ค(๋ถ„ ๋‹จ์œ„)
//  ์ด ์‹œ๊ฐ„ ๋‚ด์— ๊ณ ๊ฐ์ด ๋„์ฐฉํ•˜๋ฉด ๋ฆฌํ„ด๊ฐ’์€ true์ด๋‹ค
bool newcustomer(double x)
{
    return (std::rand() * x / RAND_MAX < 1);
}

 

 

7๋ฒˆ

#include<iostream>
#include<vector>
#include<algorithm>
#include<time.h>
using namespace std;
vector<int> Lotto(int total_n, int sel_n);

int main()
{
	srand(time(NULL));
	vector<int> winners = Lotto(51, 6);
	for (int i = 0; i < 6; i++)
		cout << winners[i] << ' ';
	return 0;
}

vector<int> Lotto(int total_n, int sel_n)
{
	vector<int> temp(total_n);
	for (int i = 0; i < total_n; i++)
		temp[i] = i + 1;
	random_shuffle(temp.begin(), temp.end());
	temp.resize(sel_n);
	return temp;
}

 

 

8๋ฒˆ

#include<iostream>
#include<set>
#include<vector>
#include<string>
#include<iterator>
#include<algorithm>
using namespace std;

void input(set<string>& s);

int main()
{
	set<string> Mat,Pat;
	cout << "Mat์˜ ์นœ๊ตฌ ๋ชฉ๋ก ์ž…๋ ฅ (๋๋‚ด๋ ค๋ฉด quit ์ž…๋ ฅ) : ";
	input(Mat);
	cout << "Pat์˜ ์นœ๊ตฌ ๋ชฉ๋ก ์ž…๋ ฅ (๋๋‚ด๋ ค๋ฉด quit ์ž…๋ ฅ): ";
	input(Pat);
	ostream_iterator<string, char> out(cout, " ");

	cout << "Mat๊ฐ€ ์ž…๋ ฅํ•œ ์นœ๊ตฌ๋ชฉ๋ก\n";
	copy(Mat.begin(), Mat.end(), out);
	cout << endl;

	cout << "Pat๊ฐ€ ์ž…๋ ฅํ•œ ์นœ๊ตฌ๋ชฉ๋ก\n";
	copy(Pat.begin(), Pat.end(), out);
	cout << endl;

	set<string> MatPat;
	set_union(Mat.begin(), Mat.end(), Pat.begin(), Pat.end(),
		insert_iterator<set<string>>(MatPat, MatPat.begin()));

	cout << "Mat์™€ Pat์˜ ์นœ๊ตฌ ๋ชฉ๋ก\n";
	copy(MatPat.begin(), MatPat.end(), out);
	cout << endl;

	return 0;
}

void input(set<string>& s)
{
	string temp;
	getline(cin, temp);
	while (temp != "quit")
	{
		s.insert(temp);
		getline(cin, temp);
	}
}

 

 

9๋ฒˆ

#include<iostream>
#include<ctime>
#include<vector>
#include<algorithm>
#include<list>
using namespace std;
const int SIZE = 100000;
int main()
{
	vector<int> vi0(SIZE);
	int i;
	for (i = 0; i < SIZE; i++)
		vi0[i] = std::rand() % 100;
	vector<int> vi;
	list<int> li;
	vi.insert(vi.begin(), vi0.begin(), vi0.end());
	li.insert(li.begin(), vi0.begin(), vi0.end());

	clock_t start = clock();
	sort(vi.begin(), vi.end());
	clock_t end = clock();
	cout << "vi ์ •๋ ฌํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„ : " << (double)(end - start) / CLOCKS_PER_SEC << endl;

	start = clock();
	li.sort();
	end = clock();
	cout << "li ์ •๋ ฌํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„ : " << (double)(end - start) / CLOCKS_PER_SEC << endl;

	li.insert(li.begin(), vi0.begin(), vi0.end());

	//li๋ฅผ vi์— ๋ณต์‚ฌ
	start = clock();
	vi.insert(vi.begin(), li.begin(), li.end());
	sort(vi.begin(), vi.end());
	li.insert(li.begin(), vi.begin(), vi.end());
	end = clock();
	cout << "li๋ฅผ vi์— ๋ณต์‚ฌํ•˜๊ณ  vi๋ฅผ ์ •๋ ฌํ•˜๊ณ  ์ •๋ ฌ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ li๋กœ ๋ณต์‚ฌํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„  : " << (double)(end - start) / CLOCKS_PER_SEC << endl;

	return 0;
}

 

 

10๋ฒˆ

// vect3.cpp -- STL ํ•จ์ˆ˜๋“ค์„ ์‚ฌ์šฉํ•œ๋‹ค
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include<memory>
struct Review {
    std::string title;
    int rating;
    int price;
};

bool operator<(const Review& r1, const Review& r2);
bool worseThan(const Review& r1, const Review& r2);
bool FillReview(Review& rr);
void ShowReview(const Review& rr);
bool worseThan2(const Review& r1, const Review& r2);

int main()
{
    using namespace std;

    shared_ptr<vector<Review>> books  (new vector<Review>);
    Review temp;
    while (FillReview(temp))
        books->push_back(temp);

    shared_ptr<vector<Review>> rem(new vector<Review>);
    *rem = *books;
    char choice;
    cout << "๋‹ค์Œ์˜ ์„ ํƒ ์‚ฌํ•ญ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค.\n"
        << "a. ์›๋ž˜ ์ˆœ์„œ\tb. ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ\tc. ์ฆ๊ฐ€์œจ ์ˆœ์„œ\n"
        << "d. ๊ฐ์†Œ์œจ ์ˆœ์„œ\te. ๊ฐ€๊ฒฉ ์ฆ๊ฐ€ ์ˆœ์„œ\tf. ๊ฐ€๊ฒฉ ๊ฐ์†Œ ์ˆœ์„œ\ng. ์ž‘์—… ๋๋‚ด๊ธฐ\n";
    cin >> choice;
    while (choice !='q')
    {
        if (choice == 'g')
            break;
        switch (choice)
        {
        case 'a':
            cout << "์›๋ž˜ ์ˆœ์„œ\n๋“ฑ๊ธ‰\t์ œ๋ชฉ\t๊ฐ€๊ฒฉ\n";
            for_each(rem->begin(), rem->end(), ShowReview);
            break;
        case 'b':
            sort(books->begin(), books->end());
            cout << "์•ŒํŒŒ๋ฒณ ์ˆœ์„œ\n๋“ฑ๊ธ‰\t์ œ๋ชฉ\t๊ฐ€๊ฒฉ\n";
            for_each(books->begin(), books->end(), ShowReview);
            break;
        case 'c':
            sort(books->begin(), books->end(), worseThan);
            cout << "์ฆ๊ฐ€์œจ ์ˆœ์„œ\n๋“ฑ๊ธ‰\t์ œ๋ชฉ\t๊ฐ€๊ฒฉ\n";
            for_each(books->begin(), books->end(), ShowReview);
            break;
        case 'd':
            sort(books->begin(), books->end(), worseThan);
            cout << "๊ฐ์†Œ์œจ ์ˆœ์„œ\n๋“ฑ๊ธ‰\t์ œ๋ชฉ\t๊ฐ€๊ฒฉ\n";
            for_each(books->rbegin(), books->rend(), ShowReview);
            break;
        case 'e':
            sort(books->begin(), books->end(), worseThan2);
            cout << "๊ฐ€๊ฒฉ ์ฆ๊ฐ€์œจ ์ˆœ์„œ\n๋“ฑ๊ธ‰\t์ œ๋ชฉ\t๊ฐ€๊ฒฉ\n";
            for_each(books->begin(), books->end(), ShowReview);
            break;
        case 'f':
            sort(books->begin(), books->end(), worseThan2);
            cout << "๊ฐ€๊ฒฉ ๊ฐ์†Œ์œจ ์ˆœ์„œ\n๋“ฑ๊ธ‰\t์ œ๋ชฉ\t๊ฐ€๊ฒฉ\n";
            for_each(books->rbegin(), books->rend(), ShowReview);
            break;
        }
        cout << "๋‹ค์Œ์˜ ์„ ํƒ ์‚ฌํ•ญ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค.\n"
            << "a. ์›๋ž˜ ์ˆœ์„œ\tb. ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ\tc. ์ฆ๊ฐ€์œจ ์ˆœ์„œ\n"
            << "d. ๊ฐ์†Œ์œจ ์ˆœ์„œ\te. ๊ฐ€๊ฒฉ ์ฆ๊ฐ€ ์ˆœ์„œ\tf. ๊ฐ€๊ฒฉ ๊ฐ์†Œ ์ˆœ์„œ\ng. ์ž‘์—… ๋๋‚ด๊ธฐ\n";
        cin >> choice;
    }
   
    return 0;
}

bool operator<(const Review& r1, const Review& r2)
{
    if (r1.title < r2.title)
        return true;
    else if (r1.title == r2.title && r1.rating < r2.rating)
        return true;
    else
        return false;
}

bool worseThan(const Review& r1, const Review& r2)
{
    if (r1.rating < r2.rating)
        return true;
    else
        return false;
}

bool worseThan2(const Review& r1, const Review& r2)
{
    if (r1.price < r2.price)
        return true;
    else
        return false;
}


bool FillReview(Review& rr)
{
    std::cout << "์ฑ… ์ œ๋ชฉ์„ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค(๋๋‚ด๋ ค๋ฉด quit): ";
    std::getline(std::cin, rr.title);
    if (rr.title == "quit")
        return false;
    std::cout << "์ฑ… ๋“ฑ๊ธ‰(0-10)์„ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค: ";
    std::cin >> rr.rating;
    if (!std::cin)
        return false;
    std::cout << "์ฑ… ๊ฐ€๊ฒฉ์„ ์ž…๋ ฅํ•˜์‹ญ์‹œ์˜ค: ";
    std::cin >> rr.price;
    if (!std::cin)
        return false;
    // ๋‚˜๋จธ์ง€ ์ž…๋ ฅ ์ค„์„ ์‚ญ์ œํ•œ๋‹ค
    while (std::cin.get() != '\n')
        continue;
    return true;
}

void ShowReview(const Review& rr)
{
    std::cout << rr.rating << "\t" << rr.title <<"\t"<<rr.price <<std::endl;
}

728x90
์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ (์ƒˆ์ฐฝ์—ด๋ฆผ)
    '๐Ÿ”คํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค ์—ฐ์Šต๋ฌธ์ œ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 18 ์ƒˆ๋กœ์šด C++ ํ‘œ์ค€๊ณผ์˜ ๋งŒ๋‚จ p.1540~ 1๋ฒˆ~4๋ฒˆ
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 17 ์ž…๋ ฅ, ์ถœ๋ ฅ, ํŒŒ์ผ p.1458~ 1๋ฒˆ~7๋ฒˆ
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 15 ํ”„๋ Œ๋“œ,์˜ˆ์™ธ,๊ธฐํƒ€ ์‚ฌํ•ญ p.1206~ 1๋ฒˆ~4๋ฒˆ
    • (C++๊ธฐ์ดˆํ”Œ๋Ÿฌ์Šค 6ํŒ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ฐ์Šต ์ •๋‹ต,์†”๋ฃจ์…˜) CHAPTER 14 C++ ์ฝ”๋“œ์˜ ์žฌํ™œ์šฉ p.1103~ 1๋ฒˆ~5๋ฒˆ
    hugDog
    hugDog
    ์•ˆ๋“œ๋กœ์ด๋“œ ๊ณต๋ถ€ ์ค‘์ธ ํ•™์ƒ์ž…๋‹ˆ๋‹ค!

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”