压平容器容器的通用函数

发布于 2024-11-16 13:19:03 字数 1337 浏览 1 评论 0原文

我正在尝试更好地掌握迭代器和泛型函数。我认为编写一个转换 container1 container1 container1 container1 container1 container1 container1 < 的函数将是一个有用的练习。容器2 <类型> >container3。例如,它应该能够将 vector<双端队列>list

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

这是我的代码:

#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++ 模板错误消息,undefined reference to void flatten<; ... 模板页面 ...

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

I am trying to get a better hold on iterators and generic functions. I thought it would be a useful exercise to write a function that converts container1 < container2 <type> > to container3 <type>. For example, it should be able to convert vector< deque<int> > to list<int>.

I figured all the container access should be through iterators, like the functions in <algorithm>.

Here is my code:

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

But when I try to call it in the following code:

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

I get a huge C++ template error message, undefined reference to void flatten< ... pages of templates ...

I feel like my code was too easy to write, and I must need some more stuff to ensure that the data type in the inner containers matches the data type in the output container. But I don't know what to do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧梦荧光笔 2024-11-23 13:19:03

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

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

I found the issue. Thanks to SFINAE (Substitution failure is not an error) your compiler couldn't find the correct template because you are trying to call operator() on start by typing start() (probably a typo). Try this:

#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;
    }
}
云归处 2024-11-23 13:19:03

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

std::accumulate can do it for you. You need to gather up the contents of each of the inside vectors into the outside vector.

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;
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文