gpt4 book ai didi

c++ - 不能在 ‘input’ 使用过时的绑定(bind),因为它有一个析构函数

转载 作者:行者123 更新时间:2023-11-30 00:39:43 26 4
gpt4 key购买 nike

这是我写的字数。我得到了这个错误:不能在'input'处使用过时的绑定(bind),因为它有一个析构函数错误:'input'的名称查找更改为ISO'for'范围什么是他们应该是什么意思?提前致谢。

   //rewrite the word-count program using insert instead of subscripting
#include <iostream>
#include <utility>
#include <map>
#include <string>
using namespace std;
int main ()
{
cout<<"please enter some words"<<endl;
map<string,int> word_count;
for(string input; cin>>input; )
if(!word_count.insert(make_pair(input,1)).second);
++word_count[input];
for(map<string,int>::iterator iter=word_count.begin(); iter!=word_count.end(); ++iter)
cout<<iter->first<<": "<<iter->second<<endl;
return 0;
}

最佳答案

    for(string input; cin>>input; )
if(!word_count.insert(make_pair(input,1)).second);
++word_count[input];

if 之后的分号结束了 if block 。您的代码等同于:

    for(string input; cin>>input; )
{
if(!word_count.insert(make_pair(input,1)).second)
{}
}
++word_count[input];

input 的范围是 for 循环的范围,而您正试图在它之外使用它。

关于c++ - 不能在 ‘input’ 使用过时的绑定(bind),因为它有一个析构函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8106313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com