📜  在地图中存储字符串的单词 - 任何代码示例

📅  最后修改于: 2022-03-11 14:55:01.076000             🧑  作者: Mango

代码示例1
int main()
{
    std::string input="Hello My name is OP Hello World";
    std::map myMap;
    std::istringstream iss(input);
    while (iss) {
        std::string substr;
        std::getline(iss,substr,' ');
        int count = 0;
        auto pos = input.find(substr, 0);
        while (pos != std::string::npos) {
            ++count;
            pos = input.find(substr, pos + 1);
        }
        if(substr.size() != 0)
            myMap[substr] = count;
    }
    for (const auto &p : myMap) {
        std::cout << p.first << "=" << p.second << '\n';
    }
    return 0;
}