Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 计算每个不同单词在其输入C+中出现的次数+; #包括 #包括 #包括 #包括 int main() { std::字符串输入; std::cout>单词; it=m.find(单词); 如果(it!=m.end()) { m、 插入(it,std::pair(字,m[字]+1)); } 其他的 { m、 插入(标准::对(字,1)); } }while(iss); for(std::map::iterator it=m.begin();it!=m.end();++it) { std::cout first_C++ - Fatal编程技术网

C++ 计算每个不同单词在其输入C+中出现的次数+; #包括 #包括 #包括 #包括 int main() { std::字符串输入; std::cout>单词; it=m.find(单词); 如果(it!=m.end()) { m、 插入(it,std::pair(字,m[字]+1)); } 其他的 { m、 插入(标准::对(字,1)); } }while(iss); for(std::map::iterator it=m.begin();it!=m.end();++it) { std::cout first

C++ 计算每个不同单词在其输入C+中出现的次数+; #包括 #包括 #包括 #包括 int main() { std::字符串输入; std::cout>单词; it=m.find(单词); 如果(it!=m.end()) { m、 插入(it,std::pair(字,m[字]+1)); } 其他的 { m、 插入(标准::对(字,1)); } }while(iss); for(std::map::iterator it=m.begin();it!=m.end();++it) { std::cout first,c++,C++,因为map自动使用默认构造函数构造一个项,当您访问一个还不存在的键时,您可以简单地说: #include <iostream> #include <string> #include <sstream> #include <map> int main() { std::string input; std::cout << "Enter input: "; std::getline(std::cin, input)

因为
map
自动使用默认构造函数构造一个项,当您访问一个还不存在的键时,您可以简单地说:

#include <iostream>
#include <string>
#include <sstream>
#include <map>

int main()
{
    std::string input;
    std::cout << "Enter input: ";
    std::getline(std::cin, input);

    std::map<std::string, int> m;
    std::map<std::string, int>::iterator it;
    std::istringstream iss(input);
    std::string words;

    do {
        iss >> words;
        it = m.find(words);
        if(it != m.end())
        {
            m.insert(it, std::pair<std::string, int>(words, m[words] + 1));
        }
        else
        {
            m.insert(std::pair<std::string, int>(words, 1));
        }
    }while(iss);

    for(std::map<std::string, int>::iterator it = m.begin(); it != m.end(); ++it)
    {
        std::cout << it->first << " - " << it->second << std::endl;
    }
    return 0;
}
新项目的默认值将为0(请参阅)

您以前的映射逻辑很好,只是条件颠倒了;它应该是
if(it==m.end())
而不是
!=
,因为
find()
返回
end())
当元素找不到时。正如GWW在他的回答中指出的那样,
insert
在项目已经在地图中时无效,这是您唯一一次使用它

此外,您的循环没有正确处理输入;您需要在读取值后但在使用它之前检查流的状态是否无效(因为如果流在其末尾,则该值是垃圾)。处理输入的惯用方法是使用
while(is>>值)
construct;如果您想自己动手,那么这相当于:

while (iss >> words) {
    ++m[words];
}

最后,当变量一次只包含一个单词时,将其命名为“words”有点误导;-)

因为
map
自动使用默认构造函数构造一个项,当您访问一个尚不存在的键时,您可以简单地说:

#include <iostream>
#include <string>
#include <sstream>
#include <map>

int main()
{
    std::string input;
    std::cout << "Enter input: ";
    std::getline(std::cin, input);

    std::map<std::string, int> m;
    std::map<std::string, int>::iterator it;
    std::istringstream iss(input);
    std::string words;

    do {
        iss >> words;
        it = m.find(words);
        if(it != m.end())
        {
            m.insert(it, std::pair<std::string, int>(words, m[words] + 1));
        }
        else
        {
            m.insert(std::pair<std::string, int>(words, 1));
        }
    }while(iss);

    for(std::map<std::string, int>::iterator it = m.begin(); it != m.end(); ++it)
    {
        std::cout << it->first << " - " << it->second << std::endl;
    }
    return 0;
}
新项目的默认值将为0(请参阅)

您以前的映射逻辑很好,只是条件颠倒了;它应该是
if(it==m.end())
而不是
!=
,因为
find()
返回
end())
当元素找不到时。正如GWW在他的回答中指出的那样,
insert
在项目已经在地图中时无效,这是您唯一一次使用它

此外,您的循环没有正确处理输入;您需要在读取值后但在使用它之前检查流的状态是否无效(因为如果流在其末尾,则该值是垃圾)。处理输入的惯用方法是使用
while(is>>值)
construct;如果您想自己动手,那么这相当于:

while (iss >> words) {
    ++m[words];
}
最后,当变量一次只包含一个单词时,将其命名为“words”有点误导;-)

我将引用

因为地图容器不允许 对于重复的键值 插入操作检查每个 元素是否插入另一个 元素已存在于 具有相同键值的容器,如果 因此,该元素不会被插入和删除 其映射值在任何情况下都不会更改 对

在您的代码中,您试图在映射中已存在的元素的顶部插入。您应该更改

while (true) {
   iss >> words;
   if (!iss) {
       break;
   }

   // Process words...
}
我将引用

因为地图容器不允许 对于重复的键值 插入操作检查每个 元素是否插入另一个 元素已存在于 具有相同键值的容器,如果 因此,该元素不会被插入和删除 其映射值在任何情况下都不会更改 对

在您的代码中,您试图在映射中已存在的元素的顶部插入。您应该更改

while (true) {
   iss >> words;
   if (!iss) {
       break;
   }

   // Process words...
}

您还可以在此处看到其他解决方案:您也可以在此处看到其他解决方案: