public member function
<string>

std::basic_string::begin

      iterator begin();const_iterator begin() const;
      iterator begin() noexcept;const_iterator begin() const noexcept;
Return iterator to beginning
Returns an iterator pointing to the first character of the string.

引數



返回值

An iterator to the beginning of the string.

如果 basic_string 物件是常量限定的,則該函式返回const_iterator。否則,它返回一個iterator.

成員型別iteratorconst_iterator隨機訪問迭代器 型別(分別指向字元和常量字元)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
// string::begin/end
#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  for ( std::string::iterator it=str.begin(); it!=str.end(); ++it)
    std::cout << *it;
  std::cout << '\n';

  return 0;
}

輸出
Test string


複雜度

未指定。

迭代器有效性

通常,沒有變化。
在某些實現中,非 const 版本可能會使物件構造或修改後首次訪問字串字元的所有迭代器、指標和引用都無效。

資料競爭

訪問該物件,並且在某些實現中,非 const 版本會在物件構造或修改後首次訪問字串字元時修改它。
返回的迭代器可用於訪問或修改字元。

複雜度

未指定,但通常是恆定的。

迭代器有效性

沒有變化。

資料競爭

訪問該物件(const 版本和非 const 版本都不會修改它)。
返回的迭代器可用於訪問或修改字元。併發訪問或修改不同的字元是安全的。

異常安全

無異常保證:此成員函式從不丟擲異常。
還可以保證返回的迭代器的複製構造或賦值永遠不會引發異常。

另見