zoukankan      html  css  js  c++  java
  • C++:string类的使用

    <string>

    std::string

    String类的定义 其也是个模板类

    typedef basic_string<char> string;

    String class

    Strings是代表一串字符的对象

    标准string类提供了类似于标准容器的接口,但是增加处理字符串所需的特殊的特征

    string是basic_string 类模板使用char类型作为字符类型实现的类。使用默认的字符特征和分配器类型。
    成员类型

    成员类型

    定义

    value_type

    char

    traits_type

    char_traits<char>

    allocator_type

    allocator<char>

    reference

    char&

    const_reference

    const char&

    pointer

    char*

    const_pointer

    const char*

    iterator

     一种可以随机访问的遍历器 (可转换为const_iterator)

    const_iterator

     随机访问 const char类型的遍历器

    reverse_iterator

     倒序访问遍历器

    const_reverse_iterator

     倒序访问const 遍历器

    difference_type

    ptrdiff_t 保存指针相减后的值得类型  与机器相关  通常定义为 long int

    size_type

    size_t


    Member functions

    (constructor)构造函数

    default (1)

    string();构造一个空串

    copy (2)

    string (const string& str);拷贝构造

    substring (3)

    string (const string& str, size_t pos, size_t len = npos);

    //使用 string 的 从 pos 位置开始的 len长的字符串构造,默认到串尾

    from c-string (4)

    string (const char* s);//使用 c风格字符串构造

    from buffer (5)

    string (const char* s, size_t n);使用 风格字符串构造

    fill (6)

    string (size_t n, char c); //使用字符 构造一个长 的串

    range (7)

    template <class InputIterator>

      string  (InputIterator first, InputIterator last); 使用遍历器构造

     

    Initializer list (8)

    string (initializer_list<char> il); 

    move (9)

    string (string&& str) noexcept;

    Construct string object (public member function )

     

    (destructor)

    String destructor (public member function )

    operator=

    重载 操作符

    String assignment (public member function )


    Iterators: 遍历器

    begin  返回指向串首的遍历器

    Return iterator to beginning (public member function )

    end    返回指向串尾的遍历器(指向最后一个字符的下一个位置)

    Return iterator to end (public member function )

    rbegin 倒序遍历 指向串最后一个字符 ,向前遍历

    Return reverse iterator to reverse beginning (public member function )

    rend  倒序遍历 指向第一个字符的下一个位置

    Return reverse iterator to reverse end (public member function )

    cbegin 常遍历器 指向串首

    Return const_iterator to beginning (public member function )

    cend  常遍历器 指向串尾的下一个字符

    Return const_iterator to end (public member function )

    crbegin 常倒序遍历 指向串首

    Return const_reverse_iterator to reverse beginning (public  member function )

    crend  长倒序遍历 指向串尾

    Return const_reverse_iterator to reverse end (public member function )

       
    Capacity: 容量

    size  返回字节数的字节数

    Return length of string (public member function )

    length 返字符数

    Return length of string (public member function )

    max_size 返回最大能

    Return maximum size of string (public member function )

    resize 调整string 大小

    Resize string (public member function )

    capacity  实际容量

    Return size of allocated storage (public member function )

    reserve  字符串倒序

    Request a change in capacity (public member function )

    clear  字符串清空

    Clear string (public member function )

    empty  测试字符串是否为空

    Test if string is empty (public member function )

    shrink_to_fit   收缩string当前容量 

    Shrink to fit (public member function )


    Element access:  

    operator[] 通过索引访问string中的字符

    Get character of string (public member function )

    at  获取 string 中的字符

    Get character in string (public member function )

    back  获取string 总的最后一个字符

    Access last character (public member function )

    front  访问string 的第一个字符

    Access first character (public member function )


    Modifiers:

    operator+= 在string 末尾连接一个字符串

    Append to string (public member function )

    append 在string 末尾连接一个字符串

    Append to string (public member function )

    push_back 在string 末尾连接一个字符

    Append character to string (public member function )

    assign 为string  分配容量

    Assign content to string (public member function )

    insert 在string 中插入 一个 字符

    Insert into string (public member function )

    erase  在string 中删除一个字符

    Erase characters from string (public member function )

    replace 替换某位置的的字符

    Replace portion of string (public member function )

    swap 交换俩string 的数据

    Swap string values (public member function )

    pop_back string 末尾删除一个字符

    Delete last character (public member function )


    String operations: string 操作

    c_str 返回与 string 中存放的字符串等价的 c string

    Get C string equivalent (public member function )

    data 获取 string 中存放的data

    Get string data (public member function )

    get_allocator 获取分配器

    Get allocator (public member function )

    copy 从string 中拷贝字符串

    Copy sequence of characters from string (public member function )

    find 查找

    Find content in string (public member function )

    rfind 逆序查找

    Find last occurrence of content in string (public member function )

    find_first_of 查找

    Find character in string (public member function )

    find_last_of

    Find character in string from the end (public member function )

    find_first_not_of

    Find absence of character in string (public member function )

    find_last_not_of

    Find non-matching character in string from the end (public member function )

    substr  生成子串

    Generate substring (public member function )

    compare 比较字符串

    Compare strings (public member function )


    Member constants

    npos 

    Maximum value for size_t (public static member constant )

     

    Non-member function overloads

    operator+

    Concatenate strings (function )

    relational operators

    Relational operators for string (function )

    swap

    Exchanges the values of two strings (function )

    operator>>

    Extract string from stream (function )

    operator<<

    Insert string into stream (function )

    getline

    Get line from stream into string (function )

     1 #include <iostream>
     2 #include <string>
     3 using    namespace    std;
     4 int main()
     5 {
     6     cout << "构造函数的测试:" <<endl;
     7     string    sa = "hello world";
     8     string    sb(sa);
     9     string    sc("hello world");
    10     string    sd(10,'@');
    11     string    se(sa.begin(),sa.end());
    12 
    13     cout << sa <<endl;
    14     cout << sb <<endl;
    15     cout << sc <<endl;
    16     cout << sd <<endl;
    17     cout << se <<endl;
    18 
    19     //只有 C++11 标准才支持 cbegin()  cend() crbegin() 和 crend() 函数
    20     cout << "遍历器的使用:" << endl;
    21     /*正序遍历*/
    22     for( string::iterator p = sa.begin() ; p!= sa.end() ; p++ )
    23         cout << *p ;
    24     cout << endl;
    25     //常量正序遍历
    26     for( string::const_iterator p = sa.begin() ; p!= sa.end() ; p++ )
    27         cout << *p ;
    28     cout << endl;
    29     //倒序遍历
    30     for( string::reverse_iterator p = sa.rbegin() ; p!= sa.rend() ; p++ )
    31         cout << *p ;
    32     cout << endl;
    33     //倒序常量遍历
    34     for( string::const_reverse_iterator p = sa.rbegin() ; p!=sa.rend() ; p++ )
    35         cout << *p;
    36     cout << endl;
    37 
    38     cout << "与容量相关的函数的使用:" << endl;
    39     cout << "size     " << sa.size() << endl;
    40     cout << "len      "    << sa.length() << endl;
    41     cout << "capacity " << sa.capacity() << endl;
    42     cout << "max_size " << sa.max_size() << endl;
    43 
    44     cout << "元素的访问" << endl;
    45     for(string::size_type i = 0 ; i < sa.length() ; i++ )
    46         cout << sa.at(i) << " " ;
    47     cout << endl;
    48 
    49     cout << sa.c_str() <<endl;
    50     cout <<sa.data() << endl;
    51 
    52     return 0;
    53 }
  • 相关阅读:
    探索式测试实践之路
    管理是什么?真正的管理者是,“管”+“理”!
    JavaScript中的函数式编程
    node js的终端中 console.log 嵌套对象会被折叠的问题
    apt-get install的默认安装路径
    nodejs 事件循环 试题思考
    仅20行的JavaScript模板引擎
    js 驼峰命名转烤串
    git reset 进阶
    linux 拷贝文本到剪切板
  • 原文地址:https://www.cnblogs.com/wowk/p/3219504.html
Copyright © 2011-2022 走看看