#include<iostream>
#include<string>
using namespace std;
int main()
{
//默认初始化,不含任何元素
string s1;
//拷贝初始化
string s2 = s1;
string s3="Hello world";
//直接初始化
string s4("hello world");
string s5(8, 'h');
cout << s5 << endl;
//访问字符
cout << "s4[2]=" << s4[2] << endl; \
s4[0] = 'H';
s4[s4.size()-1] = '\?';
cout << s4<< endl;
string str1, str2;
string str4 = str1 + "," + str2;
cin.get();
}
字符串的使用
于 2024-02-18 21:29:33 首次发布
本文通过C++代码展示了如何使用iostream和string库进行字符串的默认初始化、拷贝初始化、直接初始化以及字符访问和修改操作,包括连接两个字符串和改变特定位置的字符。
摘要由CSDN通过智能技术生成