Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用于展平容器的通用函数_C++_Templates_Generics_Containers - Fatal编程技术网

C++ 用于展平容器的通用函数

C++ 用于展平容器的通用函数,c++,templates,generics,containers,C++,Templates,Generics,Containers,我试图更好地掌握迭代器和泛型函数。我认为编写一个将container1转换为container3的函数是一个有用的练习。例如,它应该能够将向量转换为列表 我认为所有容器访问都应该通过迭代器,就像中的函数一样 这是我的密码: #include <iterator> #include <algorithm> // COCiter == Container of Containers Iterator // Oiter == Output Iterator template

我试图更好地掌握迭代器和泛型函数。我认为编写一个将
container1转换为
container3
的函数是一个有用的练习。例如,它应该能够将
向量
转换为
列表

我认为所有容器访问都应该通过迭代器,就像
中的函数一样

这是我的密码:

#include <iterator>
#include <algorithm>

// COCiter == Container of Containers Iterator
// Oiter == Output Iterator
template <class COCiter, class Oiter>
void flatten (COCiter start, COCiter end, Oiter dest)
{
    using namespace std;

    while (start != end) {
        dest = copy(start->begin(), start()->end(), dest);
        ++start;
    }
}
#包括
#包括
//COCiter==容器的容器迭代器
//Oiter==输出迭代器
模板
空隙展平(共沸器开始、共沸器结束、共沸器结束)
{
使用名称空间std;
while(开始!=结束){
dest=复制(开始->开始(),开始()->结束(),dest);
++开始;
}
}
但当我尝试用以下代码调用它时:

int main ()
{
    using namespace std;

    vector< vector<string> > splitlines;
    vector<string> flat;

    /* some code to fill SPLITLINES with vectors of strings */

    flatten(splitlines.begin(), splitlines.end(), back_inserter(flat));
}
int main()
{
使用名称空间std;
向量<向量>分裂线;
向量平坦;
/*一些用字符串向量填充拆分行的代码*/
展平(splitlines.begin()、splitlines.end()、后插入器(展平));
}

我得到一个巨大的C++模板错误消息,<代码>未定义的引用空格扁平化…模板页面…


我觉得我的代码太容易编写了,我必须需要更多的东西来确保内部容器中的数据类型与输出容器中的数据类型匹配。但是我不知道该怎么办。

我发现了这个问题。多亏了SFINAE(替换失败不是错误),您的编译器无法找到正确的模板,因为您正试图通过键入
start()
(可能是打字错误)在
start
上调用
operator()
。试试这个:

#include <iterator>
#include <algorithm>

// COCiter == Container of Containers Iterator
// Oiter == Output Iterator
template <class COCiter, class Oiter>
void flatten (COCiter start, COCiter end, Oiter dest) {
    while (start != end) {
        dest = std::copy(start->begin(), start->end(), dest);
        ++start;
    }
}
#包括
#包括
//COCiter==容器的容器迭代器
//Oiter==输出迭代器
模板
空隙展平(共沸器开始、共沸器结束、共沸器结束){
while(开始!=结束){
dest=std::copy(开始->开始(),开始->结束(),dest);
++开始;
}
}

std::accumulate
可以为您做这件事。您需要将每个内部向量的内容收集到外部向量中

vector<vector<int>> v_of_v;
vector<int> v = std::accumulate(
    v_of_v.begin(), v_of_v.end(),
    vector<int>(),
    [](vector<int> a, vector<int> b) {
        a.insert(a.end(), b.begin(), b.end());
        return a;
    });
向量v;
向量v=std::累加(
v_of_v.begin(),v_of_v.end(),
向量(),
[](向量a,向量b){
a、 插入(a.结束(),b.开始(),b.结束());
返回a;
});

SFINAE不适用于函数体中可能发生的替换故障。它只在函数签名(返回类型和参数)中起作用。这确实是一个输入错误,但事实证明,我在头文件中包含我的通用函数原型也是一个错误。我刚发现你不能那样做;您必须包括整个cpp文件(我认为),注意堆栈没有迭代器,也没有开始/结束函数。如果你想让它和堆栈一起工作,你肯定需要对它进行特殊处理;我将改变问题。我实际上不需要堆栈兼容性;只是一个可拆卸的容器。