加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 用于展平容器容器的通用功能

发布时间:2020-12-16 03:35:44 所属栏目:百科 来源:网络整理
导读:我试图更好地掌握迭代器和泛型函数.我认为编写一个转换容器1的函数是一个很有用的练习. container2 type 到container3 type.例如,它应该能够转换向量双端队列 INT 列出 int. 我认为所有容器访问都应该通过迭代器,就像 algorithm中的函数一样. 这是我的代码:
我试图更好地掌握迭代器和泛型函数.我认为编写一个转换容器1<的函数是一个很有用的练习. container2< type> >到container3< type>.例如,它应该能够转换向量<双端队列< INT> >列出< int>.

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

这是我的代码:

#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;
    }
}

但是当我尝试在以下代码中调用它时:

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));
}

我得到一个巨大的C模板错误消息,未定义引用void flatten< ......模板页面...... 我觉得我的代码太容易编写了,我还需要更多的东西来确保内部容器中的数据类型与输出容器中的数据类型相匹配.但我不知道该怎么办.

解决方法

我发现了这个问题.感谢SFINAE(替换失败不是错误),您的编译器找不到正确的模板,因为您尝试通过键入start()(可能是拼写错误)来调用operator().试试这个:
#include <iterator>
#include <algorithm>

// COCiter == Container of Containers Iterator
// Oiter == Output Iterator
template <class COCiter,Oiter dest) {
    while (start != end) {
        dest = std::copy(start->begin(),start->end(),dest);
        ++start;
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读