秒懂C++的字符串类型

字符串类型
作用:用于表示一串字符
两种风格
1.C风格字符串:char 变量名[] = "字符串值"

示例:
int main()
{
    char str1[] = "Hello World";
    cout << str1 << endl;     //str1 就是字符串的变量
    
    system("pause");
    return 0;
}
//注意 C风格的字符串要用双引号括起来

2.C++风格字符串: string 变量名 = "字符串值"
示例:

#include<iostream>
using namespace std;
#include<string>   //用C++风格字符串时候,要包含这个头文件
int main()
{
    string str1 = "Hello World";
    cout << str1 << endl;system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45488131/article/details/105927483