C ++ STL:可以阵列透明与STL函数使用吗? [英] C++ STL: Can arrays be used transparently with STL functions?

查看:181
本文介绍了C ++ STL:可以阵列透明与STL函数使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的假设下STL功能可以只用STL的数据容器,直到我看到这片code的使用(如矢量):

I was under the assumption that STL functions could be used only with STL data containers (like vector) until I saw this piece of code:

#include <functional>
#include <iostream>
#include <numeric>
using namespace std;

int main()
{
    int a[] = {9, 8, 7};
    cerr << "Sum: " << accumulate(&a[0], &a[3], 0, plus<int>()) << endl;
    return 0;
}

它编译,没有任何警告或错误使用g ++运行,给24个正确的输出总和。

It compiles and runs without any warnings or errors with g++, giving the correct output sum of 24.

是否允许由C ++ / STL 标准与STL功能的阵列的这种用法?如果是的话,怎么样阵列古老的结构融入模板迭代器,容器和功能的宏伟计划STL?此外,是否有在这种用法任何警告或细节程序员应该的小心关于

Is such usage of arrays with STL functions allowed by the C++/STL standard? If yes, how do archaic structures like arrays fit into the grand STL plan of templated iterators, containers and functions? Also, are there any caveats or details in such usage that the programmer should be careful about?

推荐答案

好了,你问的数组。你可以很容易地获得一个指向它的元素,所以它基本上可以归结为是否指针可以透明地与STL函数使用的问题。指针实际上是最强大的一种迭代器。有各种不同的

Well, you ask about an array. You can just easily get a pointer to its elements, so it basically boils down to the question whether pointers can be used transparently with STL functions. A pointer actually is the most powerful kind of an iterator. There are different kinds


  • 输入迭代器:只有前进档和一个通和只读

  • 输出迭代器:只有前进档和一个通和只写

  • Input iterator: Only forward and one-pass, and only read
  • Output iterator: Only forward and one-pass, and only write



  • 转发迭代器:只有前进,和读/写

  • 双向迭代:前进,后退,和读/写

  • 随机访问迭代器:任意步向前,一口气落后,读/写

  • Forward iterator: Only forward, and read/write
  • Bidirectional iterator: Forward and backward, and read/write
  • Random access iterator: Arbitrary steps forward and backward in one breath, and read/write

现在第二组中的每个迭代器支持之前提到的所有迭代的所有的事情。指针模式后一种迭代器 - 一个随机访问迭代。你可以加/减的任意整数,你可以读写。和所有除输出迭代器有一个操作符&GT; 可用于访问我们遍历元素类型的成员。

Now each iterator in the second group supports all the things of all iterators mentioned before it. A pointer models the last kind of iterators - a random access iterator. You may add/subtract an arbitrary integer and you may read and write. And all except the output iterator has a operator-> that can be used to access a member of the element type we iterate over.

通常情况下,迭代器有几种类型定义为成员

Normally, iterators have several typedefs as members


  • VALUE_TYPE - 什么迭代器遍历(整型,布尔,字符串,...)

  • 参考 - 参考VALUE_TYPE

  • 指针 - 指向VALUE_TYPE

  • difference_type - 什么类型的两个迭代器之间的距离(按返回的std ::距离)。

  • 的iterator_category - 这是一个标签式:它是typedef为一个类型,再presents那种迭代器。无论是的std :: input_iterator_tag ... 的std :: random_access_iterator_tag 。算法可以用它来对超载不同类型的迭代器(如的std ::距离是更快随机访问迭代器,因为它可以只返回 A - b

  • value_type - what the iterator iterates over (int, bool, string, ...)
  • reference - reference to the value_type
  • pointer - pointer to the value_type
  • difference_type - what type the distance between two iterators has (returned by std::distance).
  • iterator_category - this is a tag-type: it is typedefed to a type that represents the kind of the iterator. either std::input_iterator_tag, ..., std::random_access_iterator_tag. Algorithms can use it to overload on different kinds of iterators (like std::distance is faster for random access iterators, because it can just return a - b)

现在,当然是一个指针不具有的成员。 C ++有一个 iterator_traits 模板,专门它的指针。所以,如果你想获得任何迭代器的值类型,您

Now, a pointer of course does not have those members. C++ has an iterator_traits template and specializes it for pointers. So if you want to get the value type of any iterator, you do

iterator_traits<T>::value_type

和它是否是一个指针或其他一些迭代器,它会给你的迭代器的VALUE_TYPE。

And whether it is a pointer or some other iterator, it will give you the value_type of that iterator.

所以 - 是的,一个指针可以很好地与STL算法中使用。正如别人所说,即使的std ::矢量&lt; T&GT; ::迭代器可以是一个 T * 。指针是一个迭代器,甚至一个很好的例子。因为它是如此简单非常,但在同一时间如此强大,它可以遍历的范围。

So - yes, a pointer can very well be used with STL algorithms. As someone else mentioned, even std::vector<T>::iterator can be a T*. A pointer is a very good example of an iterator even. Because it is so exceedingly simple but at the same time so powerful that it can iterate over a range.

这篇关于C ++ STL:可以阵列透明与STL函数使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆