프로그래머스 의상 C++

2023. 10. 1. 19:22알고리즘

 

#include <string>
#include <vector>
#include <unordered_map>

using namespace std;

unordered_map<string, int> m;

int solution(vector<vector<string>> clothes) {
    int answer = 1;
    for (int i=0;i<clothes.size();i++){
        m[clothes[i][1]]++;
    }
    for (auto it : m){
        answer*=it.second+1;
    }
    answer--;
    return answer;
}

 

unordered map의 기본적인 사용예시입니다.

#include <iostream>
#include <unordered_map>
#include <string>

int main() {
    std::unordered_map<std::string, int> myMap;
    myMap["apple"] = 5;
    myMap["banana"] = 2;
    myMap["cherry"] = 8;
    
    // 원하는 값을 가져오기
    std::string key = "apple";
    int value = myMap[key];
    std::cout << key << "의 값은 " << value << "입니다." << std::endl;

    return 0;
}

 

#include <iostream>
#include <unordered_map>
#include <string>

int main() {
    std::unordered_map<std::string, int> myMap;
    myMap["a"] = 1;
    myMap["b"] = 2;
    myMap["c"] = 3;

    // 모든 원소를 순회하면서 값을 가져옴
    for (const auto& pair : myMap) {
        std::string key = pair.first;  // 키
        int value = pair.second;      // 값
        std::cout << "키: " << key << ", 값: " << value << std::endl;
    }

    return 0;
}
std::string key = "apple";
auto it = myMap.find(key);

if (it != myMap.end()) {
    int value = it->second;
    std::cout << key << "의 값은 " << value << "입니다." << std::endl;
} else {
    std::cout << key << "는 맵에 존재하지 않습니다." << std::endl;
}