简体   繁体   English

如何定义一个模板化的 function ,它需要一个 map 和一个 lambda 来按值搜索映射对?

[英]How can I define a templated function which takes a map and a lambda to search map-pair by value?

What I currently have is this:我目前拥有的是这样的:

std::find_if(mmap.begin(), mmap.end(),
                                    [streamShm](const std::pair<int, StreamMapContainer> pair) {
                                        return pair.second.pcktRefShm->id() == streamShm->id();
                                    });

and I want to refactor this into a templated function, something like this (.cpp file):我想将其重构为模板化的 function,如下所示(.cpp 文件):

template<template <typename...> class Map, typename K, typename T, typename Lambda>
typename Map<K, T>::const_iterator findMapPairByValue(typename const Map<K, T> &map, const Lambda &lambda) {
    return std::find_if(map.begin(), map.end(), lambda);
}

by this I am then able to call这样我就可以打电话了

findMapPairByValue(mmap, [streamShm](const std::pair<int, StreamMapContainer> pair) {
    return pair.second.pcktRefShm->id() == streamShm->id();
});

I declared the function as extern in my header file:我在 header 文件中将 function 声明为外部:

template<template <typename...> class Map, typename K, typename T, typename Lambda>
extern typename Map<K, T>::const_iterator findMapPairByValue(typename const Map<K, T> &map, const Lambda &lambda);

But the function above doesn't compile:但是上面的 function 不能编译:

warning: 'findMapPairByValue' initialized and declared 'extern'
   31 | extern typename Map<K, T>::const_iterator findMapPairByValue(typename const Map<K, T> &map, const Lambda &lambda);
      |                                           ^~~~~~~~~~~~~~~~~~
/.hpp:31:71: error: expected nested-name-specifier before 'const'
   31 | extern typename Map<K, T>::const_iterator findMapPairByValue(typename const Map<K, T> &map, const Lambda &lambda);
      |                                                                       ^~~~~
/.hpp:31:71: error: expected '(' before 'const'
   31 | extern typename Map<K, T>::const_iterator findMapPairByValue(typename const Map<K, T> &map, const Lambda &lambda);
      |                                                                       ^~~~~
      |                                                                       (
/.hpp:31:85: error: expected primary-expression before '>' token
   31 | extern typename Map<K, T>::const_iterator findMapPairByValue(typename const Map<K, T> &map, const Lambda &lambda);
      |                                                                                     ^
/.hpp:31:88: error: 'map' was not declared in this scope; did you mean 'std::map'?
   31 | extern typename Map<K, T>::const_iterator findMapPairByValue(typename const Map<K, T> &map, const Lambda &lambda);
      |                                                                                        ^~~
      |                                                                                        std::map

Most of the body of the function is right. function的主体大部分是对的。 You just incorrectly used typename inside parameter list:您只是在参数列表中错误地使用了typename

template<template <typename...> class Map, typename K, typename T, typename Lambda>
typename Map<K, T>::const_iterator findMapPairByValue(const Map<K, T> &map, const Lambda &lambda) {
    return std::find_if(map.begin(), map.end(), lambda);
}

Additionally it is worth noting that template function definition should be placed in header file, not inside *.cpp file另外值得注意的是模板 function 定义应该放在 header 文件中,而不是放在 *.cpp 文件中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何定义一个 function 以捕获作为参数的 lambda? - How can I define a function that takes a lambda with capture as parameter? 如何在地图中搜索本身就是一对的钥匙? - How to search for a key which is a pair itself, in a map? 如何为 unordered_map 编写一个哈希函数,它以一对为键,但如果我切换对成员的顺序,则返回相同的值? - How do I write a hash function for an unordered_map that takes a pair as key, but return the same value if I switch the order of the pairs members? 如何在C ++映射中擦除键值对? - How can I erase a key value pair in c++ map? 如何在 map 中找到配对的价值? - How do I find value as pair in a map? 如何创建一个带有map或unordered_map的函数? - How can I make a function that takes either a map or an unordered_map? 如何在模板化的类中使用std :: map迭代器? - How can I use a std::map iterator inside a templated class? 如何创建一个模板化函数可以对任何具有字符串键的 std::map 进行操作? - How do I create a templated function can that operate on any std::map that has a string key? 是否有可能专门设计一个模板化函数,该函数根据lambda接受的参数数量接受lambda表达式? - Is it possible to specialize a templated function which takes a lambda expression based on the number of arguments the lambda takes? 根据作为参数接收的函数,定义具有模板化键类型的std :: map - Define a std::map with a templated Key type based on a function that is received as a parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM