C++ 中的 string at() 函式\n


在本節中,我們將瞭解 C++ 中的 at() 函式。at() 函式用於訪問指定位置的字元。

在此程式中,我們將使用 at() 遍歷每個字元,並將它們列印到不同的行中。

示例程式碼

 線上演示

#include<iostream>
using namespace std;

main() {
   string my_str = "Hello World";

   for(int i = 0; i<my_str.length(); i++) {
      cout << my_str.at(i) << endl; //get character at position i
   }
}

輸出

H
e
l
l
o

W
o
r
l
d

更新於: 30-Jul-2019

96 次瀏覽

開啟您的 職業

完成課程,獲得認證

開始
廣告
© . All rights reserved.