카테고리 없음

[C++개발자되기] 30. map 사용법

jinkwon.kim 2022. 6. 28. 23:45
728x90
반응형

1. map 사용법

    1) 선언

        map<int, int> m1;

 

    2) insert

map1.insert(std::make_pair(1, 10); // insert

    3) find

   auto item = m1.find(key);
    if (item != m1.end()) {
        cout << "Key exists!  -  {" <<
            item->first << ";" << item->second << "}\n";
    } else {
        cout << "Key does not exist!" << endl;
    }

    4) loop 

    for (const auto &item : tempMap) {
        cout << "[" << item.first << "," << item.second << "]\n";
    }

2. string to vector

std::string s = "What is the right way to split a string into a vector of strings";
std::stringstream ss(s);
std::istream_iterator<std::string> begin(ss);
std::istream_iterator<std::string> end;
std::vector<std::string> vstrings(begin, end);
std::copy(vstrings.begin(), vstrings.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
728x90
반응형