针对下面这段代码,怎么优化map表和funAll函数const string FUNC1 = "func1"; const string FUNC2 = "func2"; const string FUNC3 = "func3"; int func1(int a, int b) { return a + b; } float func2(int a, float b, float c) { return float(a) + b + c; } int func3(int a, const char *b) { int result = a; auto length = strlen(b); for (size_t i = 0; i < length; i++) { result += b[i] - '0'; } return result; } map<string, function<any(vector<any>)>> funcMap = { {FUNC1, [](vector<any> args) { return func1(any_cast<int>(args[0]), any_cast<int>(args[1])); }}, {FUNC2, [](vector<any> args) { return func2(any_cast<int>(args[0]), any_cast<float>(args[1]), any_cast<float>(args[2])); }}, {FUNC3, [](vector<any> args) { return func3(any_cast<int>(args[0]), any_cast<char *>(args[1])); }} }; int funAll(const string &key, const vector<any> args) { if (funcMap.find(key) == funcMap.end()) { return -1; } auto func = funcMap[key]; auto result = func(args); cout << any_cast<float>(result) << endl; return 0; }
时间: 2023-06-18 09:06:38 浏览: 55
函数名, vector<any> args)
{
auto func = funcMap.find(funcName);
if (func != funcMap.end())
{
return any_cast<int>((func->second)(args));
}
return 0;
}
针对这个问题,可以进行以下优化:
1.修改map表:可以使用unordered_map代替map,因为unordered_map使用哈希表,可以快速查找元素,而map使用红黑树,查找元素比较慢。
2.修改函数定义:可以将函数返回值类型设置为auto,让编译器自动推导返回值类型。
3.修改funAll函数:可以使用auto关键字来自动推导func的类型,以及使用const引用参数来避免不必要的复制。同时可以将if语句中的find()和end()合并为一个语句,提高代码的可读性和效率。
优化后的代码如下:
const string FUNC1 = "func1";
const string FUNC2 = "func2";
const string FUNC3 = "func3";
auto func1(int a, int b) {
return a + b;
}
auto func2(int a, float b, float c) {
return float(a) + b + c;
}
auto func3(int a, const char* b) {
int result = a;
auto length = strlen(b);
for (size_t i = 0; i < length; i++) {
result += b[i] - '0';
}
return result;
}
unordered_map<string, function<any(vector<any>&)>> funcMap = {
{FUNC1, [](vector<any>& args) { return func1(any_cast<int>(args[0]), any_cast<int>(args[1])); }},
{FUNC2, [](vector<any>& args) { return func2(any_cast<int>(args[0]), any_cast<float>(args[1]), any_cast<float>(args[2])); }},
{FUNC3, [](vector<any>& args) { return func3(any_cast<int>(args[0]), any_cast<const char*>(args[1])); }}
};
int funAll(const string& funcName, vector<any>& args) {
auto func = funcMap.find(funcName);
if (func != funcMap.end()) {
return any_cast<int>((func->second)(args));
}
return 0;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)