[C++] c++ 访问字符的元素的STL字符串 →→→→→进入此内容的聊天室

来自 , 2020-03-23, 写在 C++, 查看 103 次.
URL http://www.code666.cn/view/8c3039bd
  1. // 访问字符的元素的STL字符串
  2. #include <string>
  3. #include <iostream>
  4.  
  5. int main() {
  6.         using namespace std;
  7.  
  8.         // The sample string
  9.         string strSTLString("Hello String");
  10.  
  11.         // Access the contents of the string using array syntax
  12.         cout << "Displaying characters using array-syntax: " << endl;
  13.         for (size_t nCharCounter = 0; nCharCounter < strSTLString.length();
  14.                         ++nCharCounter) {
  15.                 cout << "Character [" << nCharCounter << "] is: ";
  16.                 cout << strSTLString[nCharCounter] << endl;
  17.         }
  18.         cout << endl;
  19.  
  20.         // Access the contents of a string using iterators
  21.         cout << "Displaying characters using iterators: " << endl;
  22.         int nCharOffset = 0;
  23.         string::const_iterator iCharacterLocator;
  24.         for (iCharacterLocator = strSTLString.begin()
  25.         ; iCharacterLocator != strSTLString.end(); ++iCharacterLocator) {
  26.                 cout << "Character [" << nCharOffset++ << "] is: ";
  27.                 cout << *iCharacterLocator << endl;
  28.         }
  29.         cout << endl;
  30.  
  31.         // Access the contents of a string as a C-style string
  32.         cout << "The char* representation of the string is: ";
  33.         cout << strSTLString.c_str() << endl;
  34.  
  35.         return 0;
  36. }
  37.  

回复 "c++ 访问字符的元素的STL字符串"

这儿你可以回复上面这条便签

captcha