C++ 使用不同类型的参数调用相同的函数

C++ 使用不同类型的参数调用相同的函数,c++,C++,我想调用一个可以接受向量的函数,但我想把不同类型的向量传递给那个函数 我遇到过这样的函数调用 print<int>(int_vector); print<string>(string_vector); 打印(整数向量); 打印(字符串_向量); 这里提到的是和 还有一个疑问是,如果我传递不同类型的向量,我如何在函数调用中获取它。我应该使用void*?然后键入cast it代码正在使用模板编程来生成通用函数: template <typename T> vo

我想调用一个可以接受向量的函数,但我想把不同类型的向量传递给那个函数

我遇到过这样的函数调用

print<int>(int_vector);
print<string>(string_vector);
打印(整数向量);
打印(字符串_向量);
这里提到的是


还有一个疑问是,如果我传递不同类型的向量,我如何在函数调用中获取它。我应该使用void*?然后键入cast it

代码正在使用模板编程来生成通用函数:

template <typename T>
void foo(T item){
    // do something to item of type T
}
void foo(int str); // Declare a certain type of template
int x = 1;
foo<int>(x);
模板
无效foo(T项){
//对类型为T的项目执行某些操作
}
void foo(int str);//声明某种类型的模板
稍后,您可以使用以下功能:

template <typename T>
void foo(T item){
    // do something to item of type T
}
void foo(int str); // Declare a certain type of template
int x = 1;
foo<int>(x);
intx=1;
foo(x);
但在这种情况下,由于printf对不同类型使用不同的格式,因此最好是重载函数。重载是类似地命名函数的实践,但给出不同的参数:

void foo(std::vector<int> v);
void foo(std::vector<string> v);
void foo(标准::向量v);
void-foo(标准:向量v);

函数模板示例

#include <iostream>
#include <vector>

using namespace std;

template<typename T>
void foo(std::vector<T> vec)
{
   // do stuff with vector
}

int main() {
    std::vector<int> iv = {42};
    foo(iv);
    std::vector<string> sv = {"hello"};
    foo(sv);
    return 0;
}
#包括
#包括
使用名称空间std;
模板
void foo(标准::向量向量向量)
{
//用向量做东西
}
int main(){
std::向量iv={42};
foo(iv);
std::vector sv={“hello”};
foo(sv);
返回0;
}
如果您知道确切的类型,还有一种选择:

void foo(std::vector<int> v)
{}

void foo(std::vector<string> v)
{}
void foo(标准::向量v)
{}
void foo(标准::向量v)
{}

这是普通的函数重载。

这里要使用的是模板函数。与您的问题相关的简单示例如下:

// This line says that the function accepts one generic type
// which you will refer to as T
template <typename T>
// vector<T> means that the referenced type T will be of the type the vector, 
// you call this function with, is templated with
void print(const std::vector<T>& data) {
    // Here calling the operator[] will return the generic type T
    const T& element = data[0];
    for (unsigned i = 0; i < data.size(); ++i)
        std::cout << data[i] << std::endl;
}

这个概念被称为泛型编程。在C++中,你使用这个来实现,具体地说是。如果希望自动推断不同类型的列表的不同类型,或者需要高级模板,也可以使用模板

例如:

#include <iostream>
#include <vector>
#include <list>

template < template < class, class > class V, class T, class A >
void erase_value(V<T, A>& v, const T& t)
{
    typename V<T,A>::iterator s = v.begin();
    while (s != v.end()) {
        if ((*s) == t) { v.erase(s); break; }
        ++s;
    }
}

template < typename T >
void print_all(T begin, T end)
{
    for (; begin != end; ++begin) {
        std::cout << *begin << " ";
    }
    std::cout << std::endl;
}

template < typename T >
void print_all(const T& array)
{
    for (auto i : array) {
        std::cout << i << " ";
    }
    std::cout << std::endl;
}

int main(int argc, char** argv)
{

    std::vector<std::string> strings {"123","321","ABC"};
    std::list<int> ints {123,321,5332};
    print_all(strings);
    print_all(ints);
    erase_value(strings, std::string("123"));
    erase_value(ints, 123);
    print_all(strings.begin(), strings.end());
    print_all(ints.begin(), ints.end());
    return 0;
}
#包括
#包括
#包括
模板V类,T类,A类>
无效擦除_值(V&V、常数T&T)
{
typename V::迭代器s=V.begin();
而(s!=v.end()){
如果(*s)==t{v.erase(s);break;}
++s;
}
}
模板
全部无效打印(T开始,T结束)
{
for(;begin!=end;++begin){

我相信你在寻找:

模板
无效打印(矢量arg){
副本(cbegin(arg)、cend(arg)、ostream_迭代器(cout)(“”);
}

你需要的函数模板,你在C++中绝不会使用空洞*。我应该使用空虚*吗?然后,类型转换它,不,C++。如果你想要两个不同版本的相同函数,依赖于参数类型,过载可能是你需要的。甚至不考虑(例如,先延迟容器本身并选择模板迭代器)。取决于您的实际用例是什么。这个问题需要更多的上下文,包括意图。@drescherjm,除非您可能发现自己能够实现一个需要进行类型擦除的库…:)但这完全是另一回事。您的建议很有力。@bolov我的答案的要点并没有真正改变,但是的,它们是非常非常非常重要的y危险。@tobi303已修复。输出将仅为
1
,因为您只打印第一个元素。已修复。是的,对此很抱歉:d函数应使用对常量参数的引用,而不是对其参数进行无用的复制。我做了完全相同的操作,但其给出的错误如下"!expected identifier before'类和templatenone之间的区别是什么?它只是约定使用T作为typename。如果您想问一个新问题,您可以说
template
如果您想问的话,您应该问一个新问题,发布您的示例中的确切错误消息,我不明白您为什么需要模板参数。您的
erase_value()
当我将其更改为simple template parameter时。@zett42这只是为了表明您可以利用它进行更高级的模板编程,因为OP在模板需求方面所指的内容并不十分清楚。
template <typename T>
void print(vector<T> arg){
    copy(cbegin(arg), cend(arg), ostream_iterator<T>(cout, " "));
}