简体   繁体   English

C ++泛型迭代器

[英]C++ generic iterator

I'm not sure if the answers I was able to find are the easiest way to do what I need. 我不确定我能找到的答案是否是我所需要的最简单方法。 The simple template that I would know how to modify into a full solution to my problem would be code that accomplishes the following: 我知道如何修改为我的问题的完整解决方案的简单模板将是完成以下操作的代码:

  1. Takes as input two iterators pointing to the beginning and end of an iterable container (vector, list...) containing things of value type T. 将两个迭代器作为输入,指向包含值类型为T的东西的可迭代容器(vector,list ...)的开头和结尾。

  2. Returns a std::vector<T> containing an element-by-element copy of the input container in whatever order accomplished by iterating the input container from beginning to end. 返回一个std::vector<T>包含输入容器的逐个元素的副本,无论通过从开始到结束迭代输入容器完成的任何顺序。

Something non-functioning would be like follows: 不起作用的东西如下:

 template<typename Iterator, typename T>
 std::vector<T> dumb_copy(Iterator first, Iterator last) { ... }

Problem is that I would need the compiler to somehow check that I'm given iterators pointing to something of type T. 问题是我需要编译器以某种方式检查我是否给出了指向T类型的迭代器。

I'm currently learning C++ and writing as practice the most generic implementations of certain algorithms that I can think of, so I want to get the best practices right from the start. 我目前正在学习C ++并将实践编写为我能想到的某些算法的最通用实现,因此我想从一开始就获得最佳实践。 If there's an easy way of doing this using C++11 constructs, that's fine with me. 如果使用C ++ 11构造有一种简单的方法,那对我来说没问题。

You can simply use traits to remove the T type completely, allowing it to be determined automatically: 您可以简单地使用特征完全删除T类型,允许自动确定:

template <typename Iterator>
std::vector<typename std::iterator_traits<Iterator>::value_type>
    dumb_copy(Iterator first, Iterator last)
{
    std::vector<typename std::iterator_traits<Iterator>::value_type> copy;

    // Populate the copy vector

    return copy;
}

In particular, note that std::iterator_traits has a specialization when the iterator type is a pointer, so this will allow your function to "just work" even when it is passed pointers instead of "true" iterator objects. 特别要注意的是,当迭代器类型是一个指针时, std::iterator_traits有一个特化,所以这将允许你的函数“正常工作”,即使它是传递指针而不是“true”迭代器对象。

You don't need to do this because the standard library containers already work this way. 您不需要这样做,因为标准库容器已经以这种方式工作。 So you can create a std::vector from two iterators directly: 所以你可以直接从两个迭代器创建一个std::vector

#include <string>
#include <vector>
#include <iostream>

int main()
{
    std::string s = "hello"; // iterable container

    // construct a vector using two iterators
    std::vector<std::string::value_type> v(s.begin(), s.end());

    // check the results
    for(unsigned i = 0; i < v.size(); ++i)
        std::cout << v[i];
    std::cout << '\n';
}

You can just create a std::vector<T> with a type matching the result of *it for one of the iterators: 可以只创建一个std::vector<T>与一种类型的匹配的结果*it的迭代中的一个:

#include <type_traits>
#include <vector>
template <typename Iterator>
auto dump_copy(Iterator begin, Iterator end)
    -> std::vector<typename std::decay<decltype(*begin)>::type> {
    return std::vector<typename std::decay<decltype(*begin)>::type(begin, end);
}

With C++14 you can replace typename std::decay<X>::type by std::decay_t<X> . 使用C ++ 14,您可以用std::decay_t<X>替换typename std::decay<X>::type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM