C++ 使用模板向量作为参数定义方法

C++ 使用模板向量作为参数定义方法,c++,c++11,C++,C++11,我的C++11代码中有一个方法,它接受模板作为参数 template<typename type> uint64_t insert(type item) { //code return id; } 我想创建一个类似的,以便插入许多项目。我的尝试是将这些项作为向量传递。但是编译失败,错误为:模板参数1无效 上面的方法签名有什么问题 假设类型是存储在向量中的对象的类型 #include <iostream> #include <vector>

我的C++11代码中有一个方法,它接受模板作为参数

template<typename type> uint64_t insert(type item) {
    //code
    return id; 
 }
我想创建一个类似的,以便插入许多项目。我的尝试是将这些项作为向量传递。但是编译失败,错误为:模板参数1无效

上面的方法签名有什么问题

假设类型是存储在向量中的对象的类型

#include <iostream>
#include <vector>

template<typename type>
typename std::vector<type>::iterator insert(std::vector<type>& v, const std::vector<type>& add) {
    return v.insert(v.end(), add.begin(), add.end());
}


int main() {
    std::vector<int> a{0,1,2,3,4};
    std::vector<int> b{5,6};
    insert(a, b);
    for(const auto val : a) {
        std::cout << val << "\n";
    }
}

什么是insta类型?插入\u manyconst std::vector&items,不要复制你的向量。std::vector是一个语法错误,你的意思是std::vector吗?没有模板向量这样的东西。C++中有函数模板和类模板,也不能是向量的元素。除了ID没有定义外,代码从编辑后就正确编译。如果你仍然有问题,请发一封邮件
#include <iostream>
#include <vector>

template<typename type>
typename std::vector<type>::iterator insert(std::vector<type>& v, const std::vector<type>& add) {
    return v.insert(v.end(), add.begin(), add.end());
}


int main() {
    std::vector<int> a{0,1,2,3,4};
    std::vector<int> b{5,6};
    insert(a, b);
    for(const auto val : a) {
        std::cout << val << "\n";
    }
}
0
1
2
3
4
5
6