C++-Referenz/ Standardbibliothek/ Zeichenketten/ basic string

Alte Seite

Die Arbeit am Buch »C++-Referenz« wurde vom Hauptautor eingestellt. Ein Lehrbuch zum Thema C++ ist unter »C++-Programmierung« zu finden. Eine sehr umfangreiche und gute Referenz gibt es unter cppreference.com.

Diese Seite beschreibt C++98, einen stark veralteten Standard. Aktuelle Referenz.

// Header: string
template<typename charT, typename traits = char_traits<charT>, typename Allocator = allocator<charT> >
class basic_string {
public:
    typedef          traits                       traits_type;
    typedef typename traits::char_type            value_type;
    typedef          Allocator                    allocator_type;
    typedef typename Allocator::size_type         size_type;
    typedef typename Allocator::difference_type   difference_type;
    typedef typename Allocator::reference         reference;
    typedef typename Allocator::const_reference   const_reference;
    typedef typename Allocator::pointer           pointer;
    typedef typename Allocator::const_pointer     const_pointer;
    typedef implementation defined                iterator;
    typedef implementation defined                const_iterator;
    typedef std::reverse_iterator<iterator>       reverse_iterator;
    typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
    static size_type const npos = -1;

// Konstruktoren, Destruktor, Kopieren
    explicit basic_string(Allocator const& a = Allocator());

    basic_string(
        basic_string const& str,
        size_type pos = 0,
        size_type n = npos,
        Allocator const& a = Allocator()
    );

    basic_string(charT const* s, size_type n, Allocator const& a = Allocator());

    basic_string(charT const* s, Allocator const& a = Allocator());

    basic_string(size_type n, charT c, Allocator const& a = Allocator());

    template<typename InputIterator>
    basic_string(InputIterator begin, InputIterator end, Allocator const& a = Allocator());

    ~basic_string();

    basic_string& operator=(basic_string const& str);
    basic_string& operator=(charT const* s);
    basic_string& operator=(charT c);

// Iteratoren
    iterator       begin();
    const_iterator begin()const;
    iterator       end();
    const_iterator end()const;

    reverse_iterator       rbegin();
    const_reverse_iterator rbegin()const;
    reverse_iterator       rend();
    const_reverse_iterator rend()const;

// Kapazität
    size_type size()const;
    size_type length()const;
    size_type max_size()const;
    size_type capacity()const;
    bool      empty()const;

    void resize(size_type n, charT c);
    void resize(size_type n);

    void reserve(size_type res_arg = 0);

    void clear();

// Elementzugriff
    const_reference operator[](size_type pos)const;
    reference       operator[](size_type pos);
    const_reference at(size_type n)const;
    reference       at(size_type n);

// Modifizierer
    basic_string& operator+=(basic_string const& str);
    basic_string& operator+=(charT const* s);
    basic_string& operator+=(charT c);

    basic_string& append(basic_string const& str);
    basic_string& append(basic_string const& str, size_type pos, size_type n);
    basic_string& append(charT const* s, size_type n);
    basic_string& append(charT const* s);
    basic_string& append(size_type n, charT c);
    template<typename InputIterator>
    basic_string& append(InputIterator first, InputIterator last);

    basic_string& assign(basic_string const&);
    basic_string& assign(basic_string const& str, size_type pos, size_type n);
    basic_string& assign(charT const* s, size_type n);
    basic_string& assign(charT const* s);
    basic_string& assign(size_type n, charT c);
    template<typename InputIterator>
    basic_string& assign(InputIterator first, InputIterator last);

    basic_string& insert(size_type pos1, basic_string const& str);
    basic_string& insert(size_type pos1, basic_string const& str, size_type pos2, size_type n);
    basic_string& insert(size_type pos, charT const* s, size_type n);
    basic_string& insert(size_type pos, charT const* s);
    basic_string& insert(size_type pos, size_type n, charT c);
    iterator      insert(iterator p, charT c = charT());
    void          insert(iterator p, size_type n, charT c);
    template<typename InputIterator>
    void          insert(iterator p, InputIterator first, InputIterator last);

    basic_string& erase(size_type pos = 0, size_type n = npos);
    iterator      erase(iterator position);
    iterator      erase(iterator first, iterator last);

    basic_string& replace(size_type pos1, size_type n1, basic_string const& str);
    basic_string& replace(
        size_type pos1,
        size_type n1,
        basic_string const& str,
        size_type pos2,
        size_type n2
    );
    basic_string& replace(size_type pos, size_type n1, charT const* s, size_type n2);
    basic_string& replace(size_type pos, size_type n1, charT const* s);
    basic_string& replace(size_type pos, size_type n1, size_type n2, charT c);
    basic_string& replace(iterator i1, iterator i2, basic_string const& str);
    basic_string& replace(iterator i1, iterator i2, charT const* s, size_type n);
    basic_string& replace(iterator i1, iterator i2, charT const* s);
    basic_string& replace(iterator i1, iterator i2, size_type n, charT c);
    template<typename InputIterator>
    basic_string& replace(iterator i1, iterator i2, InputIterator j1, InputIterator j2);

    size_type copy(charT* s, size_type n, size_type pos = 0) const;

    void swap(basic_string<charT,traits,Allocator>&);

// Stringoperationen:
    const charT* c_str()const;
    const charT* data()const;

    allocator_type get_allocator()const;

    size_type find(basic_string const& str, size_type pos = 0)const;
    size_type find(charT const* s, size_type pos, size_type n)const;
    size_type find(charT const* s, size_type pos = 0)const;
    size_type find(charT c, size_type pos = 0)const;

    size_type rfind(basic_string const& str, size_type pos = npos)const;
    size_type rfind(charT const* s, size_type pos, size_type n)const;
    size_type rfind(charT const* s, size_type pos = npos)const;
    size_type rfind(charT c, size_type pos = npos)const;

    size_type find_first_of(basic_string const& str, size_type pos = 0)const;
    size_type find_first_of(charT const* s, size_type pos, size_type n)const;
    size_type find_first_of(charT const* s, size_type pos = 0)const;
    size_type find_first_of(charT c, size_type pos = 0)const;

    size_type find_last_of(basic_string const& str, size_type pos = npos)const;
    size_type find_last_of(charT const* s, size_type pos, size_type n)const;
    size_type find_last_of(charT const* s, size_type pos = npos)const;
    size_type find_last_of(charT c, size_type pos = npos)const;

    size_type find_first_not_of(basic_string const& str, size_type pos = 0)const;
    size_type find_first_not_of(charT const* s, size_type pos, size_type n)const;
    size_type find_first_not_of(charT const* s, size_type pos = 0)const;
    size_type find_first_not_of(charT c, size_type pos = 0)const;

    size_type find_last_not_of(basic_string const& str, size_type pos = npos)const;
    size_type find_last_not_of(charT const* s, size_type pos, size_type n)const;
    size_type find_last_not_of(charT const* s, size_type pos = npos)const;
    size_type find_last_not_of(charT c, size_type pos = npos)const;

    basic_string substr(size_type pos = 0, size_type n = npos)const;

    int compare(basic_string const& str)const;
    int compare(size_type pos1, size_type n1, basic_string const& str)const;
    int compare(size_type pos1, size_type n1, basic_string const& str, size_type pos2, size_type n2)const;
    int compare(charT const* s)const;
    int compare(size_type pos1, size_type n1, charT const* s, size_type n2 = npos)const;
};

// trait-Klassendeklarationen
template<typename charT> struct char_traits;
template<> struct char_traits<char>;
template<> struct char_traits<wchar_t>;

// Verknüpfung
template<typename charT, typename traits, typename Allocator> basic_string<charT,traits,Allocator>
operator+(
    basic_string<charT,traits,Allocator> const& lhs,
    basic_string<charT,traits,Allocator> const& rhs
);

template<typename charT, typename traits, typename Allocator> basic_string<charT,traits,Allocator>
operator+(charT const* lhs, basic_string<charT,traits,Allocator> const& rhs);

template<typename charT, typename traits, typename Allocator> basic_string<charT,traits,Allocator>
operator+(charT lhs, basic_string<charT,traits,Allocator> const& rhs);

template<typename charT, typename traits, typename Allocator> basic_string<charT,traits,Allocator>
operator+(basic_string<charT,traits,Allocator> const& lhs, charT const* rhs);

template<typename charT, typename traits, typename Allocator> basic_string<charT,traits,Allocator>
operator+(basic_string<charT,traits,Allocator> const& lhs, charT rhs);

// Vergleiche
template<typename charT, typename traits, typename Allocator>
bool operator==(
    basic_string<charT,traits,Allocator> const& lhs,
    basic_string<charT,traits,Allocator> const& rhs
);

template<typename charT, typename traits, typename Allocator>
bool operator==(charT const* lhs, basic_string<charT,traits,Allocator> const& rhs);

template<typename charT, typename traits, typename Allocator>
bool operator==(basic_string<charT,traits,Allocator> const& lhs, charT const* rhs);

template<typename charT, typename traits, typename Allocator>
bool operator!=(
    basic_string<charT,traits,Allocator> const& lhs,
    basic_string<charT,traits,Allocator> const& rhs
);

template<typename charT, typename traits, typename Allocator>
bool operator!=(charT const* lhs, basic_string<charT,traits,Allocator> const& rhs);

template<typename charT, typename traits, typename Allocator>
bool operator!=(basic_string<charT,traits,Allocator> const&  lhs, charT const* rhs);

template<typename charT, typename traits, typename Allocator>
bool operator<(
    basic_string<charT,traits,Allocator> const& lhs,
    basic_string<charT,traits,Allocator> const& rhs);

template<typename charT, typename traits, typename Allocator>
bool operator<(basic_string<charT,traits,Allocator> const&  lhs, charT const* rhs);

template<typename charT, typename traits, typename Allocator>
bool operator<(charT const* lhs, basic_string<charT,traits,Allocator> const& rhs);

template<typename charT, typename traits, typename Allocator>
bool operator>(
    basic_string<charT,traits,Allocator> const& lhs,
    basic_string<charT,traits,Allocator> const& rhs
);

template<typename charT, typename traits, typename Allocator>
bool operator>(basic_string<charT,traits,Allocator> const& lhs, charT const* rhs);

template<typename charT, typename traits, typename Allocator>
bool operator>(charT const* lhs, basic_string<charT,traits,Allocator> const& rhs);

template<typename charT, typename traits, typename Allocator>
bool operator<=(
    basic_string<charT,traits,Allocator> const& lhs,
    basic_string<charT,traits,Allocator> const& rhs
);

template<typename charT, typename traits, typename Allocator>
bool operator<=(basic_string<charT,traits,Allocator> const&  lhs, charT const* rhs);

template<typename charT, typename traits, typename Allocator>
bool operator<=(charT const* lhs, basic_string<charT,traits,Allocator> const& rhs);

template<typename charT, typename traits, typename Allocator>
bool operator>=(
    basic_string<charT,traits,Allocator> const& lhs,
    basic_string<charT,traits,Allocator> const& rhs
);

template<typename charT, typename traits, typename Allocator>
bool operator>=(basic_string<charT,traits,Allocator> const& lhs, charT const* rhs);

template<typename charT, typename traits, typename Allocator>
bool operator>=(charT const* lhs, basic_string<charT,traits,Allocator> const& rhs);

// Spezialisierte Algorithmen
template<typename charT, typename traits, typename Allocator>
void swap(basic_string<charT,traits,Allocator>& lhs, basic_string<charT,traits,Allocator>& rhs);

// Stream-Operationen
template<typename charT, typename traits, typename Allocator>
basic_istream<charT,traits>& operator>>(
    basic_istream<charT,traits>& is,
    basic_string<charT,traits,Allocator>& str
);

template<typename charT, typename traits, typename Allocator>
basic_ostream<charT, traits>& operator<<(
    basic_ostream<charT, traits>& os,
    basic_string<charT,traits,Allocator> const& str
);

template<typename charT, typename traits, typename Allocator>
basic_istream<charT,traits>& getline(
    basic_istream<charT,traits>& is,
    basic_string<charT,traits,Allocator>& str,
    charT delim
);

template<typename charT, typename traits, typename Allocator>
basic_istream<charT,traits>& getline(
    basic_istream<charT,traits>& is,
    basic_string<charT,traits,Allocator>& str
);

// Typdefinitionen
typedef basic_string<char> string;
typedef basic_string<wchar_t> wstring;