Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/reverse iterator/base"

From cppreference.com
m (langlinks)
(Wording update.)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{cpp/iterator/reverse_iterator/title| base}}
+
{{cpp/iterator/reverse_iterator/title|base}}
 
{{cpp/iterator/reverse_iterator/navbar}}
 
{{cpp/iterator/reverse_iterator/navbar}}
{{dcl begin}}
+
{{ddcla|constexpr=c++17|
{{dcl|until=c++17|  
+
 
iterator_type base() const;
 
iterator_type base() const;
 
}}
 
}}
{{dcl|since=c++17|
 
constexpr iterator_type base() const;
 
}}
 
{{dcl end}}
 
 
Returns the underlying base iterator. That is {{c|std::reverse_iterator(it).base() {{==}} it}}.
 
  
The base iterator refers to the element that is next (from the {{c|std::reverse_iterator::iterator_type}} perspective) to the element the {{tt|reverse_iterator}} is currently pointing to. That is {{c|&*(rit.base() - 1) {{==}} &*rit}}.
+
Returns the underlying iterator.
 
+
===Parameters===
+
(none)
+
  
 
===Return value===
 
===Return value===
The underlying iterator.
+
{{box|{{rlpst|/#current}}}}
  
{{cpp/impldef exception}}
+
===Notes===
 +
The base iterator refers to the element that is next (from the {{tt|iterator_type}} perspective) to the element the {{tt|reverse_iterator}} is currently pointing to. That is {{c|1=&*(this->base() - 1) == &*(*this)}}.
  
 
===Example===
 
===Example===
{{example |
+
{{example
| code=
+
|code=
 
#include <iostream>
 
#include <iostream>
 
#include <iterator>
 
#include <iterator>
Line 31: Line 22:
 
int main()
 
int main()
 
{
 
{
  std::vector<int> v = { 0, 1, 2, 3, 4, 5 };
+
    std::vector<int> v = {0, 1, 2, 3, 4, 5};
 
+
   
  using RevIt = std::reverse_iterator<std::vector<int>::iterator>;
+
    using RevIt = std::reverse_iterator<std::vector<int>::iterator>;
  {
+
   
 
     const auto it = v.begin() + 3;
 
     const auto it = v.begin() + 3;
     RevIt r_it(it);
+
     RevIt r_it{it};
+
    std::cout << "*it == " << *it << ", *r_it.base() == " << *r_it.base()
+
    << '\n' << "*r_it == " << *r_it <<", *(r_it.base()-1) == " << *(r_it.base()-1) << "\n";
+
  }
+
  {
+
    RevIt r_end(v.begin());
+
    RevIt r_begin(v.end());
+
 
      
 
      
     for (auto it = r_end.base(); it != r_begin.base(); ++it) {
+
    std::cout << "*it == " << *it << '\n'
      std::cout << *it << " ";
+
              << "*r_it == " << *r_it << '\n'
     }
+
              << "*r_it.base() == " << *r_it.base() << '\n'
     std::cout << "\n";
+
              << "*(r_it.base() - 1) == " << *(r_it.base() - 1) << '\n';
  }
+
   
 +
    RevIt r_end{v.begin()};
 +
    RevIt r_begin{v.end()};
 +
   
 +
     for (auto it = r_end.base(); it != r_begin.base(); ++it)
 +
        std::cout << *it << ' ';
 +
     std::cout << '\n';
 +
      
 +
    for (auto it = r_begin; it != r_end; ++it)
 +
        std::cout << *it << ' ';
 +
    std::cout << '\n';
 
}
 
}
| output=
+
|output=
*it == 3, *r_it.base() == 3
+
*it == 3
*r_it == 2, *(r_it.base()-1) == 2
+
*r_it == 2
 +
*r_it.base() == 3
 +
*(r_it.base() - 1) == 2
 
0 1 2 3 4 5
 
0 1 2 3 4 5
 +
5 4 3 2 1 0
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/iterator/adaptor/dsc operator*|reverse_iterator}}
+
{{dsc inc|cpp/iterator/adaptor/dsc operator*|reverse_iterator}}
 
{{dsc end}}
 
{{dsc end}}
 
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 19:24, 30 October 2024

 
 
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(C++20)
(C++20)
(C++20)
Utilities
(C++20)
Iterator adaptors
Range access
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
 
iterator_type base() const;
(constexpr since C++17)

Returns the underlying iterator.

Contents

[edit] Return value

current

[edit] Notes

The base iterator refers to the element that is next (from the iterator_type perspective) to the element the reverse_iterator is currently pointing to. That is &*(this->base() - 1) == &*(*this).

[edit] Example

#include <iostream>
#include <iterator>
#include <vector>
 
int main()
{
    std::vector<int> v = {0, 1, 2, 3, 4, 5};
 
    using RevIt = std::reverse_iterator<std::vector<int>::iterator>;
 
    const auto it = v.begin() + 3;
    RevIt r_it{it};
 
    std::cout << "*it == " << *it << '\n'
              << "*r_it == " << *r_it << '\n'
              << "*r_it.base() == " << *r_it.base() << '\n'
              << "*(r_it.base() - 1) == " << *(r_it.base() - 1) << '\n';
 
    RevIt r_end{v.begin()};
    RevIt r_begin{v.end()};
 
    for (auto it = r_end.base(); it != r_begin.base(); ++it)
        std::cout << *it << ' ';
    std::cout << '\n';
 
    for (auto it = r_begin; it != r_end; ++it)
        std::cout << *it << ' ';
    std::cout << '\n';
}

Output:

*it == 3
*r_it == 2
*r_it.base() == 3
*(r_it.base() - 1) == 2
0 1 2 3 4 5
5 4 3 2 1 0

[edit] See also

accesses the pointed-to element
(public member function) [edit]