【问题标题】:Function reads input and runs function twice函数读取输入并运行函数两次
【发布时间】:2015-02-13 02:43:35
【问题描述】:

我正在处理程序中的一个函数以读取用户的输入,然后该函数将检查该函数是否存在然后运行它,但它非常有问题,并且这些函数意味着运行,运行两次。

            //functions with a int and a string
     std::map<std::string, std::function<void(int, string)>> functionsIS = { 
             {"printWordWithNumber", numberPlusWord}, 
     };
     //functions with no parameters
     std::map<std::string, std::function<void()>> functionsNI = {
             {"Help", userHelp},
     }; 
void CommandCheck(std::string command){
int paramInt;
string paramString;
for (int i = 0; i < functionsIS.size(); i = i++){
    if (functionsIS[command]){
        std::cout << "Accessed '" << command << "' reading requirements..." << std::endl;
        std::cout << "Enter paramater one (integer) : ";
        std::cin >> paramInt;
        std::cout << std::endl<<"Enter paramater two (string)" << std::endl;
        std::cin.ignore();
        std::getline(std::cin,paramString);
        std::cout << "running..." << std::endl;
        functionsIS[command](paramInt,paramString);
    }
}
for (int i = 0; i < functionsNI.size(); i = i++){
    if (functionsNI[command]){
        std::cout << "Accessed '" << command << "' running..." << std::endl;
        functionsNI[command]();
    }
}
}

这是你可以运行的版本:

来源:

#include <iostream>
#include <map>
#include <windows.h>
#include <string>
#include <vector>
#include <map>
#include <functional>

#include "userFunctions.h"//header file for functions

using namespace std;

std::string input;
//functions with a int and a string
std::map<std::string, std::function<void(int, string)>> functionsIS = {
    { "printWordWithNumber", numberPlusWord },
};
//functions with no parameters
std::map<std::string, std::function<void()>> functionsNI = {
    { "Help", userHelp },
};

void CommandCheck(std::string command){
int paramInt;
string paramString;
for (int i = 0; i < functionsIS.size(); i = i++){
    if (functionsIS[command]){
        std::cout << "Accessed '" << command << "' reading requirements..." << std::endl;
        std::cout << "Enter paramater one (integer) : ";
        std::cin >> paramInt;
        std::cout << std::endl << "Enter paramater two (string)" << std::endl;
        std::cin.ignore();
        std::getline(std::cin, paramString);
        std::cout << "running..." << std::endl;
        functionsIS[command](paramInt, paramString);
    }
}
for (int i = 0; i < functionsNI.size(); i = i++){
    if (functionsNI[command]){
        std::cout << "Accessed '" << command << "' running..." << std::endl;
        functionsNI[command]();
    }
}
}

int main(){
do{
    std::cout << "Waiting For Command..." << std::endl;
    cin >> input;
    CommandCheck(input);
} while (input != "end");


return 0;
}

创建一个名为“functions”的头文件并粘贴:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void numberPlusWord(int number, std::string word){
std::cout << word << std::endl;
std::cout << number << std::endl;
}

void userHelp(){
std::cout << "I can help!" << std::endl;
}

【问题讨论】:

  • 您的代码不完整。请编辑您的帖子并添加MVCE
  • 永远不要i = i++。必须是i++

标签: c++ for-loop map


【解决方案1】:

您的代码有几个问题会导致问题。第一个是您正在迭代函数映射,如果命令存在,您正在调用它。问题是您在每次迭代时都检查相同的命令,因此如果地图包含多个元素,将为每个元素调用该命令。您可以通过删除for 循环并使用映射的find 函数来确定该命令是否存在来解决此问题。

第二个问题是,如果该命令不存在,则会在地图中为其创建一个元素。下面的if 语句会自动插入一个使用command 作为键的元素。

 if(functionsIS[command]) { /*...*/}

对您的代码的以下更新将纠正该问题。

void CommandCheck(std::string command)
{
    int paramInt;
    string paramString;

    if (functionsIS.find(command) != functionsIS.end())
    {
        std::cout << "Accessed '" << command << "' reading requirements..." << std::endl;
        std::cout << "Enter paramater one (integer) : ";
        std::cin >> paramInt;
        std::cout << std::endl << "Enter paramater two (string)" << std::endl;
        std::cin.ignore();
        std::getline(std::cin, paramString);
        std::cout << "running..." << std::endl;
        functionsIS[command](paramInt, paramString);
    }
    else if (functionsNI.find(command) != functionsNI.end())
    {
        std::cout << "Accessed '" << command << "' running..." << std::endl;
        functionsNI[command]();
    }
}

【讨论】:

  • 谢谢!现在很好用;我对地图及其功能不是很熟悉,所以我不太了解'.find()'
猜你喜欢
  • 2020-11-29
  • 2021-06-29
  • 1970-01-01
  • 2022-01-26
  • 2015-07-06
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
相关资源
最近更新 更多