๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[๊ฐ์ฒด ๋ฐ ํฌ์ธํฐ ํ์ฉ] vector ์ ์๋ string ์ iterator ๋ก find ํ๊ธฐ โ ๋ณธ๋ฌธ
๐ฉ๐ป IoT (Embedded)/C++
[๊ฐ์ฒด ๋ฐ ํฌ์ธํฐ ํ์ฉ] vector ์ ์๋ string ์ iterator ๋ก find ํ๊ธฐ โ
์ง์ง์ํ์นด 2023. 11. 27. 15:08728x90
๋ฐ์ํ
<๋ณธ ๋ธ๋ก๊ทธ๋ ํฉ๊ธฐํ ๋์ ๋ช ํ 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
๋ฐ์ํ
'๐ฉโ๐ป IoT (Embedded) > C++' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Comments