๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

[๊ฐ์ฒด ๋ฐ ํฌ์ธํ„ฐ ํ™œ์šฉ] vector ์— ์žˆ๋Š” string ์„ iterator ๋กœ find ํ•˜๊ธฐ โ— ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป IoT (Embedded)/C++

[๊ฐ์ฒด ๋ฐ ํฌ์ธํ„ฐ ํ™œ์šฉ] vector ์— ์žˆ๋Š” string ์„ iterator ๋กœ find ํ•˜๊ธฐ โ—

์ง•์ง•์•ŒํŒŒ์นด 2023. 11. 27. 15:08
728x90
๋ฐ˜์‘ํ˜•

<๋ณธ ๋ธ”๋กœ๊ทธ๋Š” ํ™ฉ๊ธฐํƒœ ๋‹˜์˜ ๋ช…ํ’ˆ C++ Programming ์„œ์ ์„ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค :-)>

 

 

๋ฌธ์ œ๋Š” ์•„๋ž˜ ์‚ฌ์ง„์„ ์ฐธ๊ณ ํ•˜์„ธ์š”

1) for ๋ฌธ์œผ๋กœ ์ผ์น˜ํ•˜๋Š” string ์ฐพ๊ธฐ
2) find ์™€ iterator ๋กœ ๋”๋ธ” ์ฒดํฌํ•˜๊ธฐ
3) find_if ์™€ ๋žŒ๋‹ค ํ•จ์ˆ˜๋กœ ์ฒดํฌํ•˜๊ธฐ

 

 โญ for ๋ฌธ์œผ๋กœ ์ผ์น˜ํ•˜๋Š” string ์ฐพ๊ธฐ

#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <algorithm>
using namespace std;
// ๋‚˜๋ผ ์ด๋ฆ„๊ณผ ์ˆ˜๋„ ๋ฌธ์ž์—ด๋กœ ๊ตฌ์„ฑ๋จ
class Nation
{
public:
    string nation;
    string capital;
    Nation() {}
    Nation(string nation, string capital)
    {
        this->nation = nation;
        this->capital = capital;
    }
};
int main()
{
    vector<Nation> v;
    v.push_back(Nation("๋ฏธ๊ตญ", "์›Œ์‹ฑํ„ด"));
    v.push_back(Nation("๋…์ผ", "๋ฒ ๋ฅผ๋ฆฐ"));
    v.push_back(Nation("ํฌ๋ฅดํˆฌ๊ฐˆ", "๋ฆฌ์Šค๋ณธ"));
    v.push_back(Nation("์ดํƒˆ๋ฆฌ์•„", "๋กœ๋งˆ"));
    v.push_back(Nation("๋Œ€ํ•œ๋ฏผ๊ตญ", "์„œ์šธ"));
    while (1)
    {
        cout << "***** ๋‚˜๋ผ์˜ ์ˆ˜๋„ ๋งž์ถ”๊ธฐ ๊ฒŒ์ž„์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. *****" << endl;
        cout << "์ •๋ณด ์ž…๋ ฅ: 1, ํ€ด์ฆˆ: 2, ์ข…๋ฃŒ: 3 >> ";
        int info;
        cin >> info;
        string nation;
        string capital;
        if (info == 1)
        {
            int n = v.size();
            cout << "ํ˜„์žฌ " << n << "๊ฐœ์˜ ๋‚˜๋ผ๊ฐ€ ์ž…๋ ฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค." << endl;
            cout << "๋‚˜๋ผ์™€ ์ˆ˜๋„๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”(no no ์ด๋ฉด ์ž…๋ ฅ๋)" << endl;
            while (1)
            {
                cout << n << ">>";
                cin >> nation >> capital;
                if ((nation == "no") && (capital == "no"))
                {
                    break;
                }

                vector<Nation>::iterator it;
                int flag = 0;
                // it = find(v.begin(), v.end(), nation);

                for (it = v.begin(); it < v.end(); it++)
                {
                    // if ((it->nation) == nation)
                    if ((it->nation) == nation)
                    {
                        cout << "already exists !!" << endl;
                        flag = 1;
                        break;
                    }
                }

                if (flag == 0)
                {
                    v.push_back(Nation(nation, capital));
                    n++;
                    int n = v.size();
                }
            }
        }

        else if (info == 2)
        {
            while (1)
            {
                random_device rd;                                    // ์‹œ๋“œ ๋„˜๋ฒ„ ์ƒ์„ฑ. time์ฒ˜๋Ÿผ ์‹œ๋“œ ๋„˜๋ฒ„ ๊ณ„์† ๋ฐ”๋€Œ๊ฒŒ ํ•œ๋‹ค
                mt19937 mersenne(rd());                              // ๋žœ๋ค ๋„˜๋ฒ„ ์ƒ์„ฑ ์•Œ๊ณ ๋ฆฌ์ฆ˜. mersenne๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด๋ฆ„
                uniform_int_distribution<int> dist(0, v.size() - 1); // n~m ์‚ฌ์ด์˜ ์ˆ˜๋ฅผ ๋žœ๋คํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. ๋‹จ, ๊ฐ ์ˆซ์ž๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์€ ์ „๋ถ€ ๋™์ผ

                auto randNum = dist(mersenne); // ๊ท ๋“ฑํ•˜๊ฒŒ ๋žœ๋ค ๋„˜๋ฒ„ ์ƒ์„ฑ
                vector<Nation>::iterator it = v.begin();

                cout << (it[randNum]).nation << "์˜ ์ˆ˜๋„๋Š”?";

                string answer;
                cin >> answer;

                if ((it[randNum]).capital == answer)
                {
                    cout << "Correct !!" << endl;
                }
                else if (answer == "exit")
                {
                    break;
                }
                else
                {
                    cout << "NO !!" << endl;
                }
            }
        }

        else if (info == 3)
        {
            break;
        }
    }
}

 

 โญ find์™€ iterator ๋กœ bool ๋”๋ธ” ์ฒดํฌํ•˜๊ธฐ

#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <algorithm>
#include <iterator>

using namespace std;

// ๋‚˜๋ผ ์ด๋ฆ„๊ณผ ์ˆ˜๋„ ๋ฌธ์ž์—ด๋กœ ๊ตฌ์„ฑ๋จ
class Nation
{
	string nation;
	string capital;

public:
	Nation() {}
	Nation(string nation, string capital)
	{
		this->nation = nation;
		this->capital = capital;
	}

	// NOTE (Nation const &n) -> ์œ„์— ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” get_nation ์—๋„ const ๋„ฃ์–ด์•ผ ํ•จ
	string get_nation() const
	{
		return this->nation;
	}
	string get_capital() const
	{
		return this->capital;
	}

	bool operator==(const std::string &rhs)
	{
		return this->capital == rhs;
	}
};

int main()
{
	vector<Nation> v;

	v.push_back(Nation("๋ฏธ๊ตญ", "์›Œ์‹ฑํ„ด"));
	v.push_back(Nation("๋…์ผ", "๋ฒ ๋ฅผ๋ฆฐ"));
	v.push_back(Nation("ํฌ๋ฅดํˆฌ๊ฐˆ", "๋ฆฌ์Šค๋ณธ"));
	v.push_back(Nation("์ดํƒˆ๋ฆฌ์•„", "๋กœ๋งˆ"));
	v.push_back(Nation("๋Œ€ํ•œ๋ฏผ๊ตญ", "์„œ์šธ"));

	while (1)
	{
		cout << "***** ๋‚˜๋ผ์˜ ์ˆ˜๋„ ๋งž์ถ”๊ธฐ ๊ฒŒ์ž„์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. *****" << endl;
		cout << "์ •๋ณด ์ž…๋ ฅ: 1, ํ€ด์ฆˆ: 2, ์ข…๋ฃŒ: 3 >> ";

		int info;
		cin >> info;

		string nation;
		string capital;

		if (info == 1)
		{
			int n = v.size();
			cout << "ํ˜„์žฌ " << n << "๊ฐœ์˜ ๋‚˜๋ผ๊ฐ€ ์ž…๋ ฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค." << endl;
			cout << "๋‚˜๋ผ์™€ ์ˆ˜๋„๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”(no no ์ด๋ฉด ์ž…๋ ฅ๋)" << endl;

			while (1)
			{
				cout << n << ">>";
				cin >> nation >> capital;

				if ((nation == "no") && (capital == "no"))
				{
					break;
				}

				// NOTE (Nation const &n) -> ์œ„์— ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” get_nation ์—๋„ const ๋„ฃ์–ด์•ผ ํ•จ
				// auto it = find_if(v.begin(), v.end(), [&](Nation const &n ) { return n.get_nation() == nation; });

				// CHECKLIST find_if ๊ฐ€ ์•„๋‹Œ find ๋กœ๋Š” ํ•  ์ˆ˜ ์—†๋Š”๊ฐ€?
				auto it = find(v.begin(), v.end(), nation);

				if (it == v.end())
				{
					v.push_back(Nation(nation, capital));
					n++;
					n = v.size();
				}
				else
				{
					cout << "already existed!!" << endl;
				}
			}
		}

		else if (info == 2)
		{
			while (1)
			{
				random_device rd;									 // ์‹œ๋“œ ๋„˜๋ฒ„ ์ƒ์„ฑ. time์ฒ˜๋Ÿผ ์‹œ๋“œ ๋„˜๋ฒ„ ๊ณ„์† ๋ฐ”๋€Œ๊ฒŒ ํ•œ๋‹ค
				mt19937 mersenne(rd());								 // ๋žœ๋ค ๋„˜๋ฒ„ ์ƒ์„ฑ ์•Œ๊ณ ๋ฆฌ์ฆ˜. mersenne๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด๋ฆ„
				uniform_int_distribution<int> dist(0, v.size() - 1); // n~m ์‚ฌ์ด์˜ ์ˆ˜๋ฅผ ๋žœ๋คํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. ๋‹จ, ๊ฐ ์ˆซ์ž๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์€ ์ „๋ถ€ ๋™์ผ

				auto randNum = dist(mersenne); // ๊ท ๋“ฑํ•˜๊ฒŒ ๋žœ๋ค ๋„˜๋ฒ„ ์ƒ์„ฑ
				vector<Nation>::iterator it = v.begin();

				cout << (it[randNum]).get_nation() << "์˜ ์ˆ˜๋„๋Š”?";

				string answer;
				cin >> answer;

				if ((it[randNum]).get_capital() == answer)
				{
					cout << "Correct !!" << endl;
				}
				else if (answer == "exit")
				{
					break;
				}
				else
				{
					cout << "NO !!" << endl;
				}
			}
		}

		else if (info == 3)
		{
			break;
		}
	}
}

 

 

 โญ find_if ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋žŒ๋‹ค ํ•จ์ˆ˜๋ฅผ ๋„ฃ๊ธฐ (with const ๋ฒ„์ „ ํ•˜๋‚˜ ๋” !)

#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <algorithm>
#include <iterator>

using namespace std;

// ๋‚˜๋ผ ์ด๋ฆ„๊ณผ ์ˆ˜๋„ ๋ฌธ์ž์—ด๋กœ ๊ตฌ์„ฑ๋จ
class Nation
{
	string nation;
	string capital;

public:
	Nation() {}
	Nation(string nation, string capital)
	{
		this->nation = nation;
		this->capital = capital;
	}

	// NOTE (Nation const &n) -> ์œ„์— ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” get_nation ์—๋„ const ๋„ฃ์–ด์•ผ ํ•จ
    // string get_nation() const
	string get_nation()
	{
		return this->nation;
	}
	string get_capital()
	{
		return this->capital;
	}

	// bool operator==(const std::string &rhs)
	// {
	// 	return this->capital == rhs;
	// }
};

int main()
{
	vector<Nation> v;

	v.push_back(Nation("๋ฏธ๊ตญ", "์›Œ์‹ฑํ„ด"));
	v.push_back(Nation("๋…์ผ", "๋ฒ ๋ฅผ๋ฆฐ"));
	v.push_back(Nation("ํฌ๋ฅดํˆฌ๊ฐˆ", "๋ฆฌ์Šค๋ณธ"));
	v.push_back(Nation("์ดํƒˆ๋ฆฌ์•„", "๋กœ๋งˆ"));
	v.push_back(Nation("๋Œ€ํ•œ๋ฏผ๊ตญ", "์„œ์šธ"));

	while (1)
	{
		cout << "***** ๋‚˜๋ผ์˜ ์ˆ˜๋„ ๋งž์ถ”๊ธฐ ๊ฒŒ์ž„์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. *****" << endl;
		cout << "์ •๋ณด ์ž…๋ ฅ: 1, ํ€ด์ฆˆ: 2, ์ข…๋ฃŒ: 3 >> ";

		int info;
		cin >> info;

		string nation;
		string capital;

		if (info == 1)
		{
			int n = v.size();
			cout << "ํ˜„์žฌ " << n << "๊ฐœ์˜ ๋‚˜๋ผ๊ฐ€ ์ž…๋ ฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค." << endl;
			cout << "๋‚˜๋ผ์™€ ์ˆ˜๋„๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”(no no ์ด๋ฉด ์ž…๋ ฅ๋)" << endl;

			while (1)
			{
				cout << n << ">>";
				cin >> nation >> capital;

				if ((nation == "no") && (capital == "no"))
				{
					break;
				}

				// NOTE (Nation const &n) -> ์œ„์— ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” get_nation ์—๋„ const ๋„ฃ์–ด์•ผ ํ•จ
				auto it = find_if(v.begin(), v.end(), [&](Nation &n)
									   { return n.get_nation() == nation; });

				// CHECKLIST find_if ๊ฐ€ ์•„๋‹Œ find ๋กœ๋Š” ํ•  ์ˆ˜ ์—†๋Š”๊ฐ€?
				//  Nation na("no", "no");
				//  find(v.begin(), v.end(), na);

				if (it == v.end())
				{
					v.push_back(Nation(nation, capital));
					n++;
					n = v.size();
				}
				else
				{
					cout << "already existed!!" << endl;
				}
			}
		}

		else if (info == 2)
		{
			while (1)
			{
				random_device rd;									 // ์‹œ๋“œ ๋„˜๋ฒ„ ์ƒ์„ฑ. time์ฒ˜๋Ÿผ ์‹œ๋“œ ๋„˜๋ฒ„ ๊ณ„์† ๋ฐ”๋€Œ๊ฒŒ ํ•œ๋‹ค
				mt19937 mersenne(rd());								 // ๋žœ๋ค ๋„˜๋ฒ„ ์ƒ์„ฑ ์•Œ๊ณ ๋ฆฌ์ฆ˜. mersenne๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด๋ฆ„
				uniform_int_distribution<int> dist(0, v.size() - 1); // n~m ์‚ฌ์ด์˜ ์ˆ˜๋ฅผ ๋žœ๋คํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. ๋‹จ, ๊ฐ ์ˆซ์ž๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์€ ์ „๋ถ€ ๋™์ผ

				auto randNum = dist(mersenne); // ๊ท ๋“ฑํ•˜๊ฒŒ ๋žœ๋ค ๋„˜๋ฒ„ ์ƒ์„ฑ
				vector<Nation>::iterator it = v.begin();

				cout << (it[randNum]).get_nation() << "์˜ ์ˆ˜๋„๋Š”?";

				string answer;
				cin >> answer;

				if ((it[randNum]).get_capital() == answer)
				{
					cout << "Correct !!" << endl;
				}
				else if (answer == "exit")
				{
					break;
				}
				else
				{
					cout << "NO !!" << endl;
				}
			}
		}

		else if (info == 3)
		{
			break;
		}
	}
}

 

โž• ์ฃผ์†Œ์— const ์ถ”๊ฐ€

#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <algorithm>
#include <iterator>

using namespace std;

// ๋‚˜๋ผ ์ด๋ฆ„๊ณผ ์ˆ˜๋„ ๋ฌธ์ž์—ด๋กœ ๊ตฌ์„ฑ๋จ
class Nation
{
	string nation;
	string capital;

public:
	Nation() {}
	Nation(string nation, string capital)
	{
		this->nation = nation;
		this->capital = capital;
	}

	// NOTE (Nation const &n) -> ์œ„์— ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” get_nation ์—๋„ const ๋„ฃ์–ด์•ผ ํ•จ
    // string get_nation() const
	string get_nation() const
	{
		return this->nation;
	}
	string get_capital() const
	{
		return this->capital;
	}

	// bool operator==(const std::string &rhs)
	// {
	// 	return this->capital == rhs;
	// }
};

int main()
{
	vector<Nation> v;

	v.push_back(Nation("๋ฏธ๊ตญ", "์›Œ์‹ฑํ„ด"));
	v.push_back(Nation("๋…์ผ", "๋ฒ ๋ฅผ๋ฆฐ"));
	v.push_back(Nation("ํฌ๋ฅดํˆฌ๊ฐˆ", "๋ฆฌ์Šค๋ณธ"));
	v.push_back(Nation("์ดํƒˆ๋ฆฌ์•„", "๋กœ๋งˆ"));
	v.push_back(Nation("๋Œ€ํ•œ๋ฏผ๊ตญ", "์„œ์šธ"));

	while (1)
	{
		cout << "***** ๋‚˜๋ผ์˜ ์ˆ˜๋„ ๋งž์ถ”๊ธฐ ๊ฒŒ์ž„์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. *****" << endl;
		cout << "์ •๋ณด ์ž…๋ ฅ: 1, ํ€ด์ฆˆ: 2, ์ข…๋ฃŒ: 3 >> ";

		int info;
		cin >> info;

		string nation;
		string capital;

		if (info == 1)
		{
			int n = v.size();
			cout << "ํ˜„์žฌ " << n << "๊ฐœ์˜ ๋‚˜๋ผ๊ฐ€ ์ž…๋ ฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค." << endl;
			cout << "๋‚˜๋ผ์™€ ์ˆ˜๋„๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”(no no ์ด๋ฉด ์ž…๋ ฅ๋)" << endl;

			while (1)
			{
				cout << n << ">>";
				cin >> nation >> capital;

				if ((nation == "no") && (capital == "no"))
				{
					break;
				}

				// NOTE (Nation const &n) -> ์œ„์— ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” get_nation ์—๋„ const ๋„ฃ์–ด์•ผ ํ•จ
				auto it = find_if(v.begin(), v.end(), [&](Nation const &n )
									   { return n.get_nation() == nation; });

				// CHECKLIST find_if ๊ฐ€ ์•„๋‹Œ find ๋กœ๋Š” ํ•  ์ˆ˜ ์—†๋Š”๊ฐ€?
				//  Nation na("no", "no");
				//  find(v.begin(), v.end(), na);

				if (it == v.end())
				{
					v.push_back(Nation(nation, capital));
					n++;
					n = v.size();
				}
				else
				{
					cout << "already existed!!" << endl;
				}
			}
		}

		else if (info == 2)
		{
			while (1)
			{
				random_device rd;									 // ์‹œ๋“œ ๋„˜๋ฒ„ ์ƒ์„ฑ. time์ฒ˜๋Ÿผ ์‹œ๋“œ ๋„˜๋ฒ„ ๊ณ„์† ๋ฐ”๋€Œ๊ฒŒ ํ•œ๋‹ค
				mt19937 mersenne(rd());								 // ๋žœ๋ค ๋„˜๋ฒ„ ์ƒ์„ฑ ์•Œ๊ณ ๋ฆฌ์ฆ˜. mersenne๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ด๋ฆ„
				uniform_int_distribution<int> dist(0, v.size() - 1); // n~m ์‚ฌ์ด์˜ ์ˆ˜๋ฅผ ๋žœ๋คํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค. ๋‹จ, ๊ฐ ์ˆซ์ž๊ฐ€ ๋‚˜์˜ฌ ํ™•๋ฅ ์€ ์ „๋ถ€ ๋™์ผ

				auto randNum = dist(mersenne); // ๊ท ๋“ฑํ•˜๊ฒŒ ๋žœ๋ค ๋„˜๋ฒ„ ์ƒ์„ฑ
				vector<Nation>::iterator it = v.begin();

				cout << (it[randNum]).get_nation() << "์˜ ์ˆ˜๋„๋Š”?";

				string answer;
				cin >> answer;

				if ((it[randNum]).get_capital() == answer)
				{
					cout << "Correct !!" << endl;
				}
				else if (answer == "exit")
				{
					break;
				}
				else
				{
					cout << "NO !!" << endl;
				}
			}
		}

		else if (info == 3)
		{
			break;
		}
	}
}
728x90
๋ฐ˜์‘ํ˜•
Comments