C,C++
[map, vector] map 의 value 로 vector 사용하기
달콩쨩
2019. 11. 28. 09:48
* map
- 헤더 : #include <map>
- 선언 : map<key의 자료형, value의 자료형> mapName
- 삽입 : mapName[key] = value
* vector
- 헤더 : #include <vector>
- 선언 : vector<자료형> vectorName
- 전체 삭제 : vectorName.clear( )
- 삽입 : push_back(값)
- 사이즈 : vectorName.size( )
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
map<string, vector<string>> CountryMap;
string FindCountryByFood(string FoodName){ //Food가 일치하는 Country 반환
map<string, vector<string>>::iterator iter;
string CountryName;
for (iter = CountryMap.begin(); iter != CountryMap.end(); ++iter) { //검색
cout << (*iter).first << " : ";
vector<string> inVect = (*iter).second;
cout << endl;
for (unsigned j = 0; j < inVect.size(); j++) {
cout << inVect[j] << endl;
if (inVect[j] == FoodName) { //value 중에 FoodName과 일치하는 값이 있으면,
CountryName = (*iter).first; //그 항목의 key 값을 CountryName 에 넣는다.
}
else {
//일치하는 항목이 없으면,
}
}
cout << endl;
}
return CountryName;
}
int main() {
vector<string> FoodList;
string correctCountryName;
string CountryName = "한국"; //key == 한국
FoodList.push_back("비빔밥"); //FoodList에 추가
FoodList.push_back("김치");
FoodList.push_back("칼국수");
FoodList.push_back("불고기");
CountryMap[CountryName] = FoodList;
FoodList.clear(); //FoodList 비우기
CountryName = "중국"; //key == 중국
FoodList.push_back("짜장면");
FoodList.push_back("짬뽕");
FoodList.push_back("유산슬");
FoodList.push_back("팔보채");
CountryMap[CountryName] = FoodList;
correctCountryName = FindCountryByFood("칼국수");
cout << "칼국수의 국가는 >>>>>" << correctCountryName << endl;
}
