c++11 标准模板(STL)string(五)

元素访问

访问首字符

std::basic_string::front
CharT& front();                    (C++11 起) 
const CharT& front() const;        (C++11 起) 

实现

      /**
       *  Returns a read/write reference to the data at the first
       *  element of the %string.
       */
      reference
      front() noexcept
      { return operator[](0); }

      /**
       *  Returns a read-only (constant) reference to the data at the first
       *  element of the %string.
       */
      const_reference
      front() const noexcept
      { return operator[](0); }

返回到 string 中首字符的引用。若 empty() == true 则行为未定义。

参数        (无)

返回值        到首字符的引用,等价于 operator[](0) 。

复杂度        常数

访问尾字符

std::basic_string::back
CharT& back();                            (C++11 起) 
const CharT& back() const;                (C++11 起) 

实现

      /**
       *  Returns a read/write reference to the data at the last
       *  element of the %string.
       */
      reference
      back() noexcept
      { return operator[](this->size() - 1); }

      /**
       *  Returns a read-only (constant) reference to the data at the
       *  last element of the %string.
       */
      const_reference
      back() const noexcept
      { return operator[](this->size() - 1); }

返回字符串中的末字符。若 empty() == true 则行为未定义。

参数                         (无)

返回值                        到末字符的引用,等价于 operator[](size() - 1) 。

复杂度                        常数

返回指向字符串首字符的指针

std::basic_string::data
const CharT* data() const;                                (C++11 前) 
const CharT* data() const noexcept;                       (C++11 起) 

返回指向作为字符存储工作的底层数组的指针。此指针满足范围 [data(); data() + size()) 为合法,且其中的值对应存储于字符串的值。

不要求返回的数组是空终止的。

若 empty() 返回 true ,则指针指向不应解引用的非空指针。

(C++11 前)

返回的数组是空终止的,即 data() 与 c_str() 进行同一功能。

若 empty() 返回 true ,则指针指向单个空字符。

(C++11 起)

指向 data() 的指针可能因以下情况非法:

  • 传递非 const 引用给任何标准库函数,或
  • 在 string 上调用非 const 成员函数,除了 operator[]() 、 at() 、 front() 、 back() 、 begin() 、 end() 、 rbegin() 、 rend() 。

1) 通过 data 的 const 重载修改数组有未定义行为。

2) 修改存储于 data() + size() 的尾后空终止符为任何异于 CharT() 的值有未定义行为。

参数  (无)

返回值  指向底层字符存储的指针。

[0, size()) 中每个 i 有 data()[i] == operator[](i) 。

(C++11 前)

[0, size()] 中每个 i 有 data() + i == std::addressof(operator[](i)) 。

(C++11 起)

复杂度   常数。

返回字符串的不可修改的 C 字符数组版本

std::basic_string::c_str
const CharT* c_str() const;                     (C++11 前) 
const CharT* c_str() const noexcept;            (C++11 起) 

返回指向拥有数据等价于存储于字符串中的空终止字符数组的指针。

该指针有范围 [c_str(); c_str() + size()] 为合法,且其中的值对应存储于字符串的值,且在最后位置有个附加的空终止字符。

c_str() 获得的指针可能被下列行为非法化:

  • 传递给任何非标准库函数字符串的非 const 引用,或
  • 在字符串上调用非 const 成员函数,包括 operator[] 、 at() 、 front() 、 back() 、 begin() 、 rbegin() 、 end() 及 rend() 。

通过 c_str() 写入字符数组是未定义行为。

c_str() 与 data() 进行同一功能。

(C++11 起)

参数                        (无)

返回值                指向底层字符存储的指针。

对于每个 [0, size()) 中的 i 有 c_str()[i] == operator[](i) 。

(C++11 前)

对于每个 [0, size()] 中的 i 有 c_str() + i == std::addressof(operator[](i)) 。

(C++11 起)

复杂度        常数。

注意

c_str() 获得的指针可以只当做一个指向空终止字符串的指针,若 string 对象不含其他空字符。

调用示例

    string str = "abcdefg hijk";

    // 访问首字符
    std::cout << "str front: " << str.front() << std::endl;
    // 修改首字符
    str.front() = 'O';
    std::cout << "str front: " << str.front() << std::endl;

    // 访问尾字符
    std::cout << "str back: " << str.back() << std::endl;
    // 修改尾字符
    str.back() = 'O';
    std::cout << "str back: " << str.back() << std::endl;

    // 返回指向字符串首字符的指针
    std::cout << "str data: " << str.data() << std::endl;

    // 返回字符串的不可修改的 C 字符数组版本
    std::cout << "str c_str: " << str.c_str() << std::endl;

c++11 标准模板(STL)string(五)_第1张图片

 

你可能感兴趣的:(#,std::string,c++,STL,string)