C++迭代器iterator本质是什么

C++迭代器iterator本质是什么,第1张

1.下载C++标准库源码:博主下载版本为:LLVM 11.0.0
https://github.com/llvm/llvm-project/releases/download/llvmorg-11.0.0/libcxx-11.0.0.src.tar.xz


2.libcxx-11.0.0.src/include/vector
<1>.类vector继承自__vector_base类
template  */>
class _LIBCPP_TEMPLATE_VIS vector
    : private __vector_base<_Tp, _Allocator>
{
private:
    typedef typename __base::pointer                 pointer;
    typedef typename __base::const_pointer           const_pointer;
    typedef __wrap_iter                     iterator;
};
typedef __wrap_iter                     iterator;

这里iterator的定义类型是pointer指针类型。
//1.迭代器iterator访问测试
#include      
#include     
#include        
using namespace std;

int main () {
  int myints[]={10,20,30,40,50,60,70};
  std::vector myvector (7);

  std::copy(myints, myints+7, myvector.begin());

  for(std::vector::iterator it = myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
  cout< vec = {1,2,3,4,5,6,7};
	for(auto i: vec)
		cout << i << " ";
	cout << endl;

	for(vector::iterator it = vec.begin(); it != vec.end(); it++)
		cout << *it << " ";
	cout<

Vector容器类成员函数查看

std::vector(3)                                                                           C++ Programmer´s Manual                                                                          std::vector(3)



NAME
       std::vector - Vector

TYPE
       class template

SYNOPSIS
       #include 

       template < class T, class Alloc = allocator > class vector; // generic template

DESCRIPTION
       Vectors are sequence containers representing arrays that can change in size.
       Just  like  arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just
       as efficiently as in arrays. But unlike arrays, their  size can change dynamically, with their storage being handled automatically by the container.
       Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allo‐
       cating  a new array and moving all elements to it. This is a relatively expensive task in terms of processing time, and thus, vectors do not reallocate each time an element is added to the con‐
       tainer.
       Instead, vector containers may allocate some extra storage to accommodate for possible growth, and thus the container may have an actual capacity greater than the  storage  strictly  needed  to
       contain its elements (i.e., its size). Libraries can implement different strategies for growth to balance between memory usage and reallocations, but in any case, reallocations should only hap‐
       pen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back).
       Therefore, compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.
       Compared to the other dynamic sequence containers (deques, lists and forward_lists), vectors are very efficient accessing its elements (just like arrays)  and  relatively  efficient  adding  or
       removing  elements  from its end. For operations that involve inserting or removing elements at positions other than the end, they perform worse than the others, and have less consistent itera‐
       tors and references than lists and forward_lists.

CONTAINER PROPERTIES
       Sequence
              Elements in sequence containers are ordered in a strict linear sequence. Individual elements are accessed by their position in this sequence.

       Dynamic array
              Allows direct access to any element in the sequence, even through pointer arithmetics, and provides relatively fast addition/removal of elements at the end of the sequence.

       Allocator-aware
              The container uses an allocator object to dynamically handle its storage needs.

TEMPLATE PARAMETERS
       T      Type of the elements.
              Only if T is guaranteed to not throw while moving, implementations can optimize to move elements instead of copying them during reallocations.
              Aliased as member type vector::value_type.

       Alloc  Type of the allocator object used to define the storage allocation model. By default, the allocator class template is used, which defines the simplest  memory  allocation  model  and  is
              value-independent.
              Aliased as member type vector::allocator_type.

MEMBER TYPES
       C++98

       ┌───────────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬──────────────────────────────────────────────┐
       │     member type       │                                                       definition                                                        │                    notes                     │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │value_type             │ The first template parameter (T)                                                                                        │                                              │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │allocator_type         │ The second template parameter (Alloc)                                                                                   │ defaults to: allocator           │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │reference              │ allocator_type::reference                                                                                               │ for the default allocator: value_type&       │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │const_reference        │ allocator_type::const_reference                                                                                         │ for the default allocator: const value_type& │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │pointer                │ allocator_type::pointer                                                                                                 │ for the default allocator: value_type*       │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │const_pointer          │ allocator_type::const_pointer                                                                                           │ for the default allocator: const value_type* │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │iterator               │ a random access iterator to value_type                                                                                  │ convertible to const_iterator                │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │const_iterator         │ a random access iterator to const value_type                                                                            │                                              │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │reverse_iterator       │ reverse_iterator                                                                                              │                                              │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │const_reverse_iterator │ reverse_iterator                                                                                        │                                              │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │difference_type        │ a signed integral type, identical to: iterator_traits::difference_type                                        │ usually the same as ptrdiff_t                │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │size_type              │ an unsigned integral type that can represent any non-negative value of difference_type                                  │ usually the same as size_t                   │
       └───────────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────────────────┘


       C++11

       ┌───────────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┬──────────────────────────────────────────────┐
       │     member type       │                                                       definition                                                        │                    notes                     │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │value_type             │ The first template parameter (T)                                                                                        │                                              │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │allocator_type         │ The second template parameter (Alloc)                                                                                   │ defaults to: allocator           │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │reference              │ value_type&                                                                                                             │                                              │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │const_reference        │ const value_type&                                                                                                       │                                              │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │pointer                │ allocator_traits::pointer                                                                               │ for the default allocator: value_type*       │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │const_pointer          │ allocator_traits::const_pointer                                                                         │ for the default allocator: const value_type* │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │iterator               │ a random access iterator to value_type                                                                                  │ convertible to const_iterator                │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │const_iterator         │ a random access iterator to const value_type                                                                            │                                              │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │reverse_iterator       │ reverse_iterator                                                                                              │                                              │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │const_reverse_iterator │ reverse_iterator                                                                                        │                                              │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │difference_type        │ a signed integral type, identical to:                                                                                   │ usually the same as ptrdiff_t                │
       │                       │ iterator_traits::difference_type                                                                              │                                              │
       ├───────────────────────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┼──────────────────────────────────────────────┤
       │size_type              │ an unsigned integral type that can represent any non-negative value of difference_type                                  │ usually the same as size_t                   │
       └───────────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────────────────┘


MEMBER FUNCTIONS
       vector::vector (3)
              Construct vector  (public member function)

       vector::~vector (3)
              Vector destructor  (public member function)

       vector::operator= (3)
              Assign content  (public member function)

   Iterators
       vector::begin (3)
              Return iterator to beginning  (public member function)

       vector::end (3)
              Return iterator to end  (public member function)

       vector::rbegin (3)
              Return reverse iterator to reverse beginning  (public member function)

       vector::rend (3)
              Return reverse iterator to reverse end  (public member function)

       vector::cbegin (3) [C++11 only]
              Return const_iterator to beginning  (public member function)

       vector::cend (3) [C++11 only]
              Return const_iterator to end  (public member function)

       vector::crbegin (3) [C++11 only]
              Return const_reverse_iterator to reverse beginning  (public member function)

       vector::crend (3) [C++11 only]
              Return const_reverse_iterator to reverse end  (public member function)

   Capacity
       vector::size (3)
              Return size  (public member function)

       vector::max_size (3)
              Return maximum size  (public member function)

       vector::resize (3)
              Change size  (public member function)

       vector::capacity (3)
              Return size of allocated storage capacity  (public member function)

       vector::empty (3)
              Test whether vector is empty  (public member function)

       vector::reserve (3)
              Request a change in capacity  (public member function)

       vector::shrink_to_fit (3) [C++11 only]
              Shrink to fit  (public member function)

   Element access
       vector::operator[] (3)
              Access element  (public member function)

       vector::at (3)
              Access element  (public member function)

       vector::front (3)
              Access first element  (public member function)

       vector::back (3)
              Access last element  (public member function)

       vector::data (3) [C++11 only]
              Access data  (public member function)

   Modifiers
       vector::assign (3)
              Assign vector content  (public member function)

       vector::push_back (3)
              Add element at the end  (public member function)

       vector::pop_back (3)
              Delete last element  (public member function)

       vector::insert (3)
              Insert elements  (public member function)

       vector::erase (3)
              Erase elements  (public member function)

       vector::swap (3)
              Swap content  (public member function)

       vector::clear (3)
              Clear content  (public member function)

       vector::emplace (3) [C++11 only]
              Construct and insert element  (public member function)

       vector::emplace_back (3) [C++11 only]
              Construct and insert element at the end  (public member function)

   Allocator
       vector::get_allocator (3)
              Get allocator  (public member function)

NON-MEMBER FUNCTION OVERLOADS
       relational operators
              Relational operators for vector (function template )

       swap (3)
              Exchange contents of vectors  (function template)

TEMPLATE SPECIALIZATIONS
       vector (3)
              Vector of bool  (class template specialization)

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/915013.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-16
下一篇2022-05-16

发表评论

登录后才能评论

评论列表(0条)