头文件
#include<stdio> // scanf_s/printf_s 头文件
#include<string>
using namespace std;
一 定长字符串
int main(){
const int maxSize = 100;
string str;
str.resize(maxSize);
scanf_s("%s", &str[0]);// 输入,scanf_s只能读取定长的字符串
printf_s(str.c_str()); //输出
}
二 不定长字符串
int main(){
string str;
printf_s("请输入字符串:");
getline(cin, str);// 输入,当字符串不定长时可以使用getline
puts(str.c_str()); //输出
}
三 遍历字符串
int main(){
string s = "sssss1231";
for(char c : s){
printf_s("%c", c);//打印
}
}