接受任何 STL 容器的函数

Function which accept any STL container

本文关键字:函数 STL 任何      更新时间:2023-10-16

我需要实现一个接受任何STL容器的模板函数。并基于执行某些操作的容器类型。

例:

template <class Container, class T>
void func(Container<T> container) {
if (container == std::map) {
...
} else {
... 
}
}

int main() {
std::vector<int> v1; 
func(v1); // ok
std::vector<double> v2;
func(v2); // ok
std::map<int, double> m1;
func(m1); // ok
std::list<int> l1;
func(l1); // ok
}

您可以应用Constexpr If(自 C++17 起((使用std::is_same(在编译时检查类型,并应用参数包(自 C++11 起(,因为这些容器采用多个模板参数。

例如
template <template <typename...> class Container, class... T>
void func(const Container<T...>& container) {
if constexpr (std::is_same_v<Container<T...>, std::map<T...>>) {
...
} else if constexpr (std::is_same_v<Container<T...>, std::vector<T...>>) {
...
} else if constexpr (std::is_same_v<Container<T...>, std::list<T...>>) {
...
} else {
...
}
}

PS:这取决于您的意图,但是将参数更改为通过引用传递到常量以避免不必要的复制可能是个好主意。

由于无论如何都要为每个容器进行一个实现,因此可以直接进行重载。它将在 C++11 中工作,并且具有在每个重载中轻松提供模板参数的好处。

template <class T, size_t N>
void func(const std::array<T,N>& c) {
std::cout << "array " << c.size() << 'n';
}
template <class T, class Alloc>
void func(const std::vector<T,Alloc>& c) {
std::cout << "vector " << c.size() << 'n';
}
template <class T, class Alloc>
void func(const std::list<T,Alloc>& c) {
std::cout << "list " << c.size() << 'n';
}
template <class Key, class T, class Comp, class Alloc>
void func(const std::map<Key,T,Comp,Alloc>& c) {
std::cout << "map " << c.size() << 'n';
}
template <class CharT, class Traits, class Alloc>
void func(const std::basic_string<CharT,Traits,Alloc>& c) {
std::cout << "basic_string " << c.size() << 'n';
}
// add more of the containers you aim to support here