C ++将任何容器传递给函数 [英] C++ Pass any container to function

查看:113
本文介绍了C ++将任何容器传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种遍历任何STL容器的方法。目前我有这个:

I'm trying to find a way to iterate through any STL container. Currently I have this:

std::string range(std::vector<int>& args)
{
    for (auto it : args)
        // do something
}

我正在寻找一种方法,可以将具有任何类型的任何类型的STL容器传递给函数,而不是 std :: vector< int> args 。我该怎么做?

I'm looking for a way to be able to pass any type of STL container with any type to the function instead of std::vector<int>& args. How can I do this?

推荐答案

使用模板。

template<typename Container>
std::string range(Container& args)
{
   for (auto it : args)
      // do something
}

可能对特殊类型进行重载(例如, std :: map )。

Probably with overloading for special types (std::map for example).

这篇关于C ++将任何容器传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆