https://velog.io/@kwt0124/int-string-string-int
: c_str๋ก๋ ํ์ด๋ด์ผ ํจ.
// ์ถ๊ฐ์ ์ผ๋ก 2์ค์ ์ฝ๋๊ฐ ํ์ํจ...
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
#include <unordered_map>
#include <map>
int main()
{
map<string, int>m1;
map<int, string>m2;
int n1, n2;
cin >> n1 >> n2;
string word;
for (int i = 0; i < n1; i++)
{
cin >> word;
m1.insert({ word, i + 1 });
m2.insert({ i + 1 , word});
}
cin.ignore();
for (int i = 0; i < n2; i++)
{
cin >> word;
int temp = atoi(word.c_str());
//๋ฌธ์์ด
if (temp == 0 && word.compare("0") != 0)
{
//m1์ ์
๋ ฅ๊ฐ์ด string์ผ ๊ฒฝ์ฐ.
//auto iter = m1.find(word);
cout << m1[word] << '\n';
}
//์ซ์
else
{
//m2์ ์
๋ ฅ๊ฐ์ด int์ผ ๊ฒฝ์ฐ.
//auto iter = m2.find(temp);
cout << m2[temp] << '\n';
}
}
}
: ์ ์์ฒดํฌ ํ์ธํ๋ ํจ์ ๊ธฐ์ต์ด ์๋์,
๊ทธ๋ฅ string์ ์ฒซ๋จ์ด๋ก ์ ์์ธ์ง ๋ฌธ์์ด์ธ์ง ํ์ธํด์ ์งํํจ...
#include <iostream>
using namespace std;
#include <vector>
#include <string>
#include <map>
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n, m;
cin >> n >> m;
map<int, string >m1;
map<string, int>m2;
for (int i = 1; i <= n; ++i)
{
string s;
cin >> s;
m1[i] = s;
m2[s] = i;
}
for (int i = 0; i < m; ++i)
{
// ๋ฌธ์์ด์ด ๋ค์ด์ฌ ์ ์๊ณ .. ์ซ์๊ฐ ๋ค์ด์ฌ ์ ์์.
string s;
cin >> s;
if (s[0] >= 'A' && s[0] <= 'Z')
{
cout << m2[s] << '\n';
}
// ์ซ์์ผ ๊ฒฝ์ฐ.
else
{
cout << m1[stoi(s)] << '\n';
}
}
}