标准库头文件 <string>

来自cppreference.com
< cpp‎ | header

此头文件是字符串库的一部分。

目录

[编辑] 包含

<initializer_list> (C++11 起)

[编辑]

名称 描述
std::char_traits 描述字符类型属性的类模板(类模板)
std::char_traits<char> (类模板特化)
std::char_traits<wchar_t> (类模板特化)
std::char_traits<char16_t> (C++11) (类模板特化)
std::char_traits<char32_t> (C++11) (类模板特化)
std::basic_string 表示文本字符串对象的类模板(类模板)
std::string std::basic_string<char> (typedef)
std::wstring std::basic_string<wchar_t> (typedef)
std::u16string (C++11) std::basic_string<char16_t> (typedef)
std::u32string (C++11) std::basic_string<char32_t> (typedef)
std::hash<std::string> (C++11) (类模板特化)
std::hash<std::u16string> (C++11) (类模板特化)
std::hash<std::u32string> (C++11) (类模板特化)
std::hash<std::wstring> (C++11) (类模板特化)

[编辑] 函数

连接两个字符串或者一个字符串和一个字符
(函数模板) [编辑]
以字典序比较二个字符串
(函数模板) [编辑]
特化 std::swap 算法
(函数模板) [编辑]
输入/输出
进行 string 上的流输入与输出
(函数模板) [编辑]
从 I/O 流读取数据到字符串
(函数模板) [编辑]
数值转换
(C++11)
(C++11)
(C++11)
转换字符串为有符号整数
(函数) [编辑]
(C++11)
(C++11)
转换字符串为无符号整数
(函数) [编辑]
(C++11)
(C++11)
(C++11)
转换字符串为浮点值
(函数) [编辑]
(C++11)
转换整数或浮点值为 string
(函数) [编辑]
转换整数或浮点值为 wstring
(函数) [编辑]

[编辑] 字面量

定义于内联命名空间 std::literals::string_literals
转换字符数组字面量为 basic_string
(函数) [编辑]

[编辑] 概要

#include <initializer_list>
 
namespace std {
 
    // 字符特性:
    template<class charT> struct char_traits;
    template <> struct char_traits<char>;
    template <> struct char_traits<char16_t>;
    template <> struct char_traits<char32_t>;
    template <> struct char_traits<wchar_t>;
 
    // basic_string:
    template<class charT, class traits = char_traits<charT>,
        class Allocator = allocator<charT> >
            class basic_string;
 
    template<class charT, class traits, class Allocator>
        basic_string<charT,traits,Allocator>
            operator+(const basic_string<charT,traits,Allocator>& lhs,
                    const basic_string<charT,traits,Allocator>& rhs);
    template<class charT, class traits, class Allocator>
        basic_string<charT,traits,Allocator>
            operator+(basic_string<charT,traits,Allocator>&& lhs,
                    const basic_string<charT,traits,Allocator>& rhs);
    template<class charT, class traits, class Allocator>
        basic_string<charT,traits,Allocator>
            operator+(const basic_string<charT,traits,Allocator>& lhs,
                    basic_string<charT,traits,Allocator>&& rhs);
    template<class charT, class traits, class Allocator>
        basic_string<charT,traits,Allocator>
            operator+(basic_string<charT,traits,Allocator>&& lhs,
                    basic_string<charT,traits,Allocator>&& rhs);
    template<class charT, class traits, class Allocator>
        basic_string<charT,traits,Allocator>
            operator+(const charT* lhs,
                    const basic_string<charT,traits,Allocator>& rhs);
    template<class charT, class traits, class Allocator>
        basic_string<charT,traits,Allocator>
            operator+(const charT* lhs,
                    basic_string<charT,traits,Allocator>&& rhs);
    template<class charT, class traits, class Allocator>
        basic_string<charT,traits,Allocator>
            operator+(charT lhs, const basic_string<charT,traits,Allocator>& rhs);
    template<class charT, class traits, class Allocator>
        basic_string<charT,traits,Allocator>
            operator+(charT lhs, basic_string<charT,traits,Allocator>&& rhs);
    template<class charT, class traits, class Allocator>
        basic_string<charT,traits,Allocator>
            operator+(const basic_string<charT,traits,Allocator>& lhs,
                    const charT* rhs);
    template<class charT, class traits, class Allocator>
        basic_string<charT,traits,Allocator>
            operator+(basic_string<charT,traits,Allocator>&& lhs,
                    const charT* rhs);
    template<class charT, class traits, class Allocator>
        basic_string<charT,traits,Allocator>
            operator+(const basic_string<charT,traits,Allocator>& lhs, charT rhs);
    template<class charT, class traits, class Allocator>
        basic_string<charT,traits,Allocator>
            operator+(basic_string<charT,traits,Allocator>&& lhs, charT rhs);
 
    template<class charT, class traits, class Allocator>
        bool operator==(const basic_string<charT,traits,Allocator>& lhs,
            const basic_string<charT,traits,Allocator>& rhs);
    template<class charT, class traits, class Allocator>
        bool operator==(const charT* lhs,
                        const basic_string<charT,traits,Allocator>& rhs);
    template<class charT, class traits, class Allocator>
        bool operator==(const basic_string<charT,traits,Allocator>& lhs
                        const charT* rhs);
    template<class charT, class traits, class Allocator>
        bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
                        const basic_string<charT,traits,Allocator>& rhs);
    template<class charT, class traits, class Allocator>
        bool operator!=(const charT* lhs,
                        const basic_string<charT,traits,Allocator>& rhs);
    template<class charT, class traits, class Allocator>
        bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
                        const charT* rhs);
 
    template<class charT, class traits, class Allocator>
        bool operator< (const basic_string<charT,traits,Allocator>& lhs,
                        const basic_string<charT,traits,Allocator>& rhs);
    template<class charT, class traits, class Allocator>
        bool operator< (const basic_string<charT,traits,Allocator>& lhs,
                        const charT* rhs);
    template<class charT, class traits, class Allocator>
        bool operator< (const charT* lhs,
            const basic_string<charT,traits,Allocator>& rhs);
 
    template<class charT, class traits, class Allocator>
        bool operator> (const basic_string<charT,traits,Allocator>& lhs,
                        const basic_string<charT,traits,Allocator>& rhs);
    template<class charT, class traits, class Allocator>
        bool operator> (const basic_string<charT,traits,Allocator>& lhs,
                        const charT* rhs);
    template<class charT, class traits, class Allocator>
        bool operator> (const charT* lhs,
                        const basic_string<charT,traits,Allocator>& rhs);
 
    template<class charT, class traits, class Allocator>
        bool operator<=(const basic_string<charT,traits,Allocator>& lhs,
                        const basic_string<charT,traits,Allocator>& rhs);
    template<class charT, class traits, class Allocator>
        bool operator<=(const basic_string<charT,traits,Allocator>& lhs,
                        const charT* rhs);
    template<class charT, class traits, class Allocator>
        bool operator<=(const charT* lhs,
                        const basic_string<charT,traits,Allocator>& rhs);
    template<class charT, class traits, class Allocator>
        bool operator>=(const basic_string<charT,traits,Allocator>& lhs,
                        const basic_string<charT,traits,Allocator>& rhs);
    template<class charT, class traits, class Allocator>
        bool operator>=(const basic_string<charT,traits,Allocator>& lhs,
                        const charT* rhs);
    template<class charT, class traits, class Allocator>
        bool operator>=(const charT* lhs,
                        const basic_string<charT,traits,Allocator>& rhs);
 
    // 交换:
    template<class charT, class traits, class Allocator>
        void swap(basic_string<charT,traits,Allocator>& lhs,
                basic_string<charT,traits,Allocator>& rhs);
 
    // 插入器与释出器:
    template<class charT, class traits, class Allocator>
        basic_istream<charT,traits>&
            operator>>(basic_istream<charT,traits>&& is,
                    basic_string<charT,traits,Allocator>& str);
    template<class charT, class traits, class Allocator>
        basic_ostream<charT, traits>&
            operator<<(basic_ostream<charT, traits>&& os,
                    const basic_string<charT,traits,Allocator>& str);
    template<class charT, class traits, class Allocator>
        basic_istream<charT,traits>&
            getline(basic_istream<charT,traits>& is,
                    basic_string<charT,traits,Allocator>& str,
                    charT delim);
    template<class charT, class traits, class Allocator>
        basic_istream<charT,traits>&
            getline(basic_istream<charT,traits>&& is,
                    basic_string<charT,traits,Allocator>& str,
                    charT delim);
    template<class charT, class traits, class Allocator>
        basic_istream<charT,traits>&
            getline(basic_istream<charT,traits>& is,
                    basic_string<charT,traits,Allocator>& str);
    template<class charT, class traits, class Allocator>
        basic_istream<charT,traits>&
            getline(basic_istream<charT,traits>&& is,
                    basic_string<charT,traits,Allocator>& str);
 
    // basic_string typedef名
    typedef basic_string<char> string;
    typedef basic_string<char16_t> u16string;
    typedef basic_string<char32_t> u32string;
    typedef basic_string<wchar_t> wstring;
 
    // 数值转换:
    int stoi(const string& str, size_t *idx = 0, int base = 10);
    long stol(const string& str, size_t *idx = 0, int base = 10);
    unsigned long stoul(const string& str, size_t *idx = 0, int base = 10);
    long long stoll(const string& str, size_t *idx = 0, int base = 10);
    unsigned long long stoull(const string& str, size_t *idx = 0, int base = 10);
    float stof(const string& str, size_t *idx = 0);
    double stod(const string& str, size_t *idx = 0);
    long double stold(const string& str, size_t *idx = 0);
    string to_string(int val);
    string to_string(unsigned val);
    string to_string(long val);
    string to_string(unsigned long val);
    string to_string(long long val);
    string to_string(unsigned long long val);
    string to_string(float val);
    string to_string(double val);
    string to_string(long double val);
 
    int stoi(const wstring& str, size_t *idx = 0, int base = 10);
    long stol(const wstring& str, size_t *idx = 0, int base = 10);
    unsigned long stoul(const wstring& str, size_t *idx = 0, int base = 10);
    long long stoll(const wstring& str, size_t *idx = 0, int base = 10);
    unsigned long long stoull(const wstring& str, size_t *idx = 0, int base = 10);
    float stof(const wstring& str, size_t *idx = 0);
    double stod(const wstring& str, size_t *idx = 0);
    long double stold(const wstring& str, size_t *idx = 0);
    wstring to_wstring(int val);
    wstring to_wstring(unsigned val);
    wstring to_wstring(long val);
    wstring to_wstring(unsigned long val);
    wstring to_wstring(long long val);
    wstring to_wstring(unsigned long long val);
    wstring to_wstring(float val);
    wstring to_wstring(double val);
    wstring to_wstring(long double val);
 
    // 哈希支持:
    template <class T> struct hash;
    template <> struct hash<string>;
    template <> struct hash<u16string>;
    template <> struct hash<u32string>;
    template <> struct hash<wstring>;
 
    inline namespace literals {
    inline namespace string_literals {
 
    string operator "" s(const char* str, size_t len);
    u16string operator "" s(const char16_t* str, size_t len);
    u32string operator "" s(const char32_t* str, size_t len);
    wstring operator "" s(const wchar_t* str, size_t len);
 
   } // inline namespace string_literals
   } // inline namespace literals
} // namespace std