我如何调用这些函数?
假设我有3个显示功能
void disp1()
{
std::cout<<"\n Disp1 ";
}
void disp2()
{
std::cout<<"\n Disp2 ";
}
void disp3()
{
std::cout<<"\n Disp3 ";
}
我想创建这些函数的映射,并根据键调用它们。
fMap["d1"]
应运行该disp1函数。我将如何解决这个问题;
如果函数的返回类型不是void,我可以获取值但是我需要打印的语句怎么样。像上面那些;
答案 0 :(得分:3)
您可以使用std::map和std::function执行此操作:
#include <map>
#include <string>
#include <iostream>
#include <functional>
void disp1()
{
std::cout<<"\n Disp1 ";
}
void disp2()
{
std::cout<<"\n Disp2 ";
}
void disp3()
{
std::cout<<"\n Disp3 ";
}
int main()
{
// add the functions as an initializer-list
std::map<std::string, std::function<void()>> fMap
{
{"d1", disp1}
, {"d2", disp2}
, {"d3", disp3}
};
// or add them one at a time
fMap["d1"] = disp1;
fMap["d2"] = disp2;
fMap["d3"] = disp3;
fMap["d1"](); // call them using ()
fMap["d2"]();
fMap["d3"]();
}
答案 1 :(得分:1)
要存储查找索引,您可以使用std::map
。这包括键值对(mymap[ key ] = value
)。 C ++允许您存储指向函数的指针,在这里我使用using
关键字创建一个类型名称func_t,它的函数将具有指纹类型:void (*)(void)
(这就是你说的&#34;指向一个取消void并返回void的函数的指针。
#include <iostream>
#include <string>
#include <map>
// the functions we intend to map
void disp1()
{
std::cout<<"Disp1\n";
}
void disp2()
{
std::cout<<"Disp2\n";
}
void disp3()
{
std::cout<<"Disp3\n";
}
int main() {
// create a new type, func_t, which describes a pointer
// to a void function that takes no parameters (void).
using func_t = void (*)(void);
// declare a map from a string key to a func_t value,
// and initialize it with a mapping of f1->disp1, f2->disp2
// and f3->disp3
std::map<std::string, func_t> functionMap = {
{ "f1", disp1 }, { "f2", disp2 }, { "f3", disp3 }
};
// declare a string for reading input
std::string input;
// loop until there is no more input on std::cin.
while (std::cin.good()) {
// prompt
std::cout << "Which disp (f1, f2, f3)? ";
// fetch the next line of text from cin, without the \n
std::getline(std::cin, input);
// if the input is empty we ran out of input or the user
// input a blank line. either way, stop.
if (input.empty())
break;
std::cout << "You chose " << input << "\n";
// look for the key in the map. if the key is not found,
// it will equal the special iterator functionMap.end()
auto it = functionMap.find(input);
// If it's not functionMap.end then we have a valid iterator.
// it->first is the key, it->second is the function pointer.
if (it != functionMap.end()) {
// to use a function pointer, just add the () with any
// arguments after the variable name.
// remember, it->second is the function pointer.
it->second();
} else {
std::cout << "Invalid entry.\n";
}
}
}
答案 2 :(得分:0)
第一个问题的答案是在地图中使用std::function
。
std::map<std::string, std::function<void>> fMap {
{"d1", disp1},
{"d2", disp2},
{"d3", disp3}
};
关于我需要考虑的返回值的第二个问题。
答案 3 :(得分:0)
在@ Galik的评论
的帮助下解决 #include <iostream>
#include <functional>
#include <algorithm>
#include <map>
typedef std::function<void()> func_t;
typedef std::map<std::string, func_t> func_t_map;
void disp1()
{
std::cout<<"\n Display 1 "<<std::endl;
return;
}
void disp2()
{
std::cout<<"\n Display 2 "<<std::endl;
return;
}
void disp3()
{
std::cout<<"\n Display 3 "<<std::endl;
return;
}
void disp4()
{
std::cout<<"\n Display 4 "<<std::endl;
return;
}
int main()
{
func_t_map fMap;
fMap["d1"] = disp1;
fMap["d2"] = disp2;
fMap["d3"] = disp3;
fMap["d4"] = disp4;
fMap["d2"]();
return 0;
}