C++string容器-字元存取
阿新 • • 發佈:2021-02-01
技術標籤:C++基礎學習c++字串leetcode正則表示式
string中單個字元存取方式有兩種
程式碼如下:
#include <iostream>
using namespace std;
#include <cstring>
//string 字元存取
void test01() {
string str = "hello";
cout << "str = " << str << endl;
for (int i = 0; i < str.size(); i++) {
cout << str[i];
}
cout << endl;
for (int i = 0; i < str.size(); i++) {
cout << str.at(i);
}
cout << endl;
str[0] = 'x';
cout << "str = " << str << endl;
str.at(1) = 'x';
cout << "str = " << str << endl;
}
int main() {
test01();
return 0;
}