C++ 字串庫 - operator[]



描述

它返回字串中位置 pos 處的字元的引用。

宣告

以下是 std::string::operator[] 的宣告

 char& operator[] (size_t pos);

C++11

const char& operator[] (size_t pos) const;

引數

pos - 字串中字元的位置值。

返回值

它返回字串中位置 pos 處的字元的引用。

異常

如果丟擲異常,字串不會發生任何更改。

示例

以下示例演示了 std::string::operator[]。

#include <iostream>
#include <string>

int main () {
   std::string str ("Sairamkrishna Mammahe");
   for (int i=0; i<str.length(); ++i) {
      std::cout << str[i];
   }
   return 0;
}
Sairamkrishna Mammahe 
string.htm
廣告