java - C++ STL迭代器声明的iterator和reverse_iterator实质区别?是不同数值吗?
PHP中文网
PHP中文网 2017-04-18 10:47:50
0
2
795

问1:Vector<int>::reverse_iterator r1和Vector<int>::iterator r2。
r1和r2作用是截然不同的,这个:reverse_iterator和iterator实质是不同数值吗,为什么会有这个作用?

问2:ostream_iterator<int,char>out_iter(cout," ");括号(cout," ")是什么意思,这是个构造函数?为什么cout能给int赋值?

PHP中文网
PHP中文网

认证0级讲师

reply all(2)
PHPzhong

Question 1
reverse_iterator and iterator are two iterator types defined in the vector class template. The implementation may be different classes, such as:


template </* ... */>
class vector {
 public:
  class ReverseIterator {...};
  class Iterator {...};

  using reverse_iterator = ReverseIterator;
  using iterator = Iterator;
};

reverse_iterator的迭代方向和iterator相反。即若r1和r2指向同一个元素,r1+1r2-1指向同一个元素(若有效,且它们分别重载了operator+operator-).

The opposite direction of iteration is achieved through different operator implementations. If the iterator is internally implemented by a pointer, the operator++的实现可自减该指针,而iterator的operator++ implementation of reverse_iterator can increment the pointer.

Question 2
ostream_iterator<int, char> out_iter(cout, " ");是声明并定义一个变量,且直接初始化(direct initialize)该变量。(cout, " ") is the actual parameter list. The compiler will try to choose a constructor based on this parameter list and "pass" the parameters to the constructor.

According to the standard, cout cannot assign a value to int. Please provide the relevant code. But cout can be converted to bool (after c++11) or void * (before c++11) type. This is because the type of cout inherits an instance of basic_ios, and the basic_ios class template used to instantiate the instance defines the relevant conversion operator. That’s it bool x = std::cout;

洪涛
  1. What are two numerical values? . reverse_iteratoriteratorIt is an iterator, essentially two class templates
    Reimplement different details of the function inside the class to achieve different functions, but the external interface is the same

  2. Instantiate an objectout_iter,类型是ostream_iterator<int,char>,传入构造函数的参数是cout" "

The meaning of the sentence "cout can assign a value to int" is unclear

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!