Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/basic string/rbegin"

From cppreference.com
< cpp‎ | string‎ | basic string
m (+{{image|range-rbegin-rend.svg}}, +{{noexcept}})
(Example two, put by K-FIVE)
Line 53: Line 53:
 
Exemplary
 
Exemplary
 
yralpmexE
 
yralpmexE
 +
}}
 +
 +
 +
===Example 2 ===
 +
{{example
 +
| code=
 +
#include <iostream>
 +
#include <string>
 +
#include <cassert>
 +
#include <stdexcept>
 +
 +
 +
using std::cout;
 +
using std::cin;
 +
using std::endl;
 +
using std::cerr;
 +
 +
/// std::basic_string::rbegin, std::basic_string::crbegin
 +
/*
 +
 +
reverse_iterator rbegin();
 +
 +
const_reverse_iterator rbegin() const;
 +
 +
const_reverse_iterator crbegin() const; (since C++11)
 +
 +
*/
 +
/// Return value : reverse iterator to the first character
 +
 +
 +
int main (int ac,char **av, char **lvs) {
 +
    { /// test reverse_iterator re begin()
 +
        cout<<"test reverse_iterator re begin()"<<endl;
 +
 +
        std::basic_string<char> s ("a b c d e f g h");
 +
 +
        cout<<"Before using iterator : "<<s<<endl;
 +
 +
        *s.rbegin() = 'H'; /// changing 'h' to 'H'
 +
 +
        cout<<"After using iterator : "<<s<<endl;
 +
 +
    }
 +
    { /// continuing test above
 +
 +
        std::basic_string<char> s ("a b c d e f g h");
 +
 +
        std::basic_string<char>::reverse_iterator ris = s.rbegin();
 +
 +
        cout<<*ris<<endl;
 +
 +
        while(*ris){
 +
 +
            cout<<*ris<<' ';
 +
 +
            ++ris;
 +
        }
 +
        ris-=s.size(); /// Okay
 +
 +
        auto bris = (ris); /// Okay
 +
 +
        cout<<endl<<*bris<<endl;
 +
    }
 +
    { /// Test const_reverse_iterator rbegin() const;
 +
 +
        cout<<"Test const_reverse_iterator rbegin() const;"<<endl;
 +
 +
        std::string s ("1 2 3 4 5 6 7 8 9");
 +
 +
        cout<<"Before : "<<s<<endl;
 +
 +
        std::string::const_reverse_iterator cris = s.rbegin();
 +
 +
        cout<<*cris<<endl; /// Output -> 9
 +
 +
        for( ; *cris ; ++cris )
 +
            cout<<*cris<<' '; /// Output -> 9 8 7 6 5 4 3 2 2 1
 +
 +
        cris-= s.size(); /// Reset Address to rbegin
 +
        /*
 +
        *cris = '*' /// error: assignment of read-only location cris...
 +
 +
        for( ; *cris ; ++cris )
 +
            cout<<*cris<<' ';
 +
        */
 +
        cout<<endl;
 +
    }
 +
    { /// Test const_reverse_iterator crbegin() const; (since C++11)
 +
 +
        cout<<"Test const_reverse_iterator crbegin() const; (since C++11)"<<endl;
 +
 +
        std::string s ("A B C D E F G H");
 +
 +
        std::string::const_reverse_iterator cris = s.crbegin();
 +
 +
        cout<<*cris<<endl;
 +
        /*
 +
        *cris = '*' /// error: assignment of read-only location cris...
 +
 +
        for( ; *cris ; ++cris )
 +
            cout<<*cris<<' ';
 +
        */
 +
        while (*cris) {
 +
            cout<<*cris<<' ';
 +
            ++cris;
 +
        }
 +
 +
    }
 +
 +
}
 +
 +
| output=
 +
test reverse_iterator re begin()
 +
Before using iterator : a b c d e f g h
 +
After using iterator : a b c d e f g H
 +
h
 +
h  g  f  e  d  c  b  a
 +
h
 +
 +
Test const_reverse_iterator rbegin() const;
 +
Before : 1 2 3 4 5 6 7 8 9
 +
9
 +
9  8  7  6  5  4  3  2  1
 +
 +
Test const_reverse_iterator crbegin() const; (since C++11)
 +
H
 +
H  G  F  E  D  C  B  A
 +
 +
Process returned 0 (0x0)  execution time : 0.004 s
 +
Press ENTER to continue.
 
}}
 
}}
  

Revision as of 06:22, 30 April 2016

 
 
 
std::basic_string
Member functions
Element access
Iterators
basic_string::rbeginbasic_string::crbegin
(C++11)
Capacity
Modifiers
Search
Operations
Constants
Non-member functions
I/O
Comparison
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Numeric conversions
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
Literals
Helper classes
Deduction guides (C++17)

 
reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
const_reverse_iterator crbegin() const;
(since C++11)

Returns a reverse iterator to the first character of the reversed string. It corresponds to the last character of the non-reversed string.

range-rbegin-rend.svg

Contents

Parameters

(none)

Return value

reverse iterator to the first character

Exceptions

(none) (until C++11)
noexcept specification:  
noexcept
  
(since C++11)

Complexity

Constant

Example

#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
 
int main()
{
    std::string s("Exemplar!");
    *s.rbegin() = 'y';
    std::cout << s << '\n'; // "Exemplary"
 
    std::string c;
    std::copy(s.crbegin(), s.crend(), std::back_inserter(c));
    std::cout << c << '\n'; // "yralpmexE"
}

Output:

Exemplary
yralpmexE


Example 2

#include <iostream>
#include <string>
#include <cassert>
#include <stdexcept>
 
 
using std::cout;
using std::cin;
using std::endl;
using std::cerr;
 
/// std::basic_string::rbegin, std::basic_string::crbegin
/*
 
reverse_iterator rbegin();
 
const_reverse_iterator rbegin() const;
 
const_reverse_iterator crbegin() const; (since C++11)
 
*/
 /// Return value : reverse iterator to the first character
 
 
int main (int ac,char **av, char **lvs) {
    { /// test reverse_iterator re begin()
        cout<<"test reverse_iterator re begin()"<<endl;
 
        std::basic_string<char> s ("a b c d e f g h");
 
        cout<<"Before using iterator : "<<s<<endl;
 
        *s.rbegin() = 'H'; /// changing 'h' to 'H'
 
        cout<<"After using iterator : "<<s<<endl;
 
    }
    { /// continuing test above
 
        std::basic_string<char> s ("a b c d e f g h");
 
        std::basic_string<char>::reverse_iterator ris = s.rbegin();
 
        cout<<*ris<<endl;
 
        while(*ris){
 
            cout<<*ris<<' ';
 
            ++ris;
        }
        ris-=s.size(); /// Okay
 
        auto bris = (ris); /// Okay
 
        cout<<endl<<*bris<<endl;
    }
    { /// Test const_reverse_iterator rbegin() const;
 
        cout<<"Test const_reverse_iterator rbegin() const;"<<endl;
 
        std::string s ("1 2 3 4 5 6 7 8 9");
 
        cout<<"Before : "<<s<<endl;
 
        std::string::const_reverse_iterator cris = s.rbegin();
 
        cout<<*cris<<endl; /// Output -> 9
 
        for( ; *cris ; ++cris )
            cout<<*cris<<' '; /// Output -> 9 8 7 6 5 4 3 2 2 1
 
        cris-= s.size(); /// Reset Address to rbegin
        /*
        *cris = '*' /// error: assignment of read-only location cris...
 
        for( ; *cris ; ++cris )
            cout<<*cris<<' ';
        */
        cout<<endl;
    }
    { /// Test const_reverse_iterator crbegin() const; (since C++11)
 
        cout<<"Test const_reverse_iterator crbegin() const; (since C++11)"<<endl;
 
        std::string s ("A B C D E F G H");
 
        std::string::const_reverse_iterator cris = s.crbegin();
 
        cout<<*cris<<endl;
        /*
        *cris = '*' /// error: assignment of read-only location cris...
 
        for( ; *cris ; ++cris )
            cout<<*cris<<' ';
        */
        while (*cris) {
            cout<<*cris<<' ';
            ++cris;
        }
 
    }
 
}

Output:

test reverse_iterator re begin()
Before using iterator : a b c d e f g h
After using iterator : a b c d e f g H
h
h   g   f   e   d   c   b   a 
h
 
Test const_reverse_iterator rbegin() const;
Before : 1 2 3 4 5 6 7 8 9
9
9   8   7   6   5   4   3   2   1 
 
Test const_reverse_iterator crbegin() const; (since C++11)
H
H   G   F   E   D   C   B   A 
 
Process returned 0 (0x0)   execution time : 0.004 s
Press ENTER to continue.

See also

(C++11)
returns a reverse iterator to the end
(public member function) [edit]