• 字符与字符串


    字符型

    作用:字符型变量用于显示单个字符

    语法:char ch = 'a';

    注意1:在显示字符型变量时,用单引号将字符括起来,不要用双引号

    注意2:单引号内只能有一个字符,不可以是字符串

    • C和C++中字符型变量只占用==1个字节==。
    • 字符型变量并不是把字符本身放到内存中存储,而是将对应的ASCII编码放入到存储单元

    示例:

    1. int main() {
    2. char ch = 'a';
    3. cout << ch << endl;
    4. cout << sizeof(char) << endl;
    5. //ch = "abcde"; //错误,不可以用双引号
    6. //ch = 'abcde'; //错误,单引号内只能引用一个字符
    7. cout << (int)ch << endl; //查看字符a对应的ASCII码
    8. ch = 97; //可以直接用ASCII给字符型变量赋值
    9. cout << ch << endl;
    10. system("pause");
    11. return 0;
    12. }

     字符串型:

    作用:用于表示一串字符

    两种风格

    1. C风格字符串: char 变量名[] = "字符串值"

            示例:

    1. int main() {
    2. char str1[] = "hello world";
    3. cout << str1 << endl;
    4. system("pause");
    5. return 0;
    6. }

    注意:C风格的字符串要用双引号括起来

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

    示例:

    1. int main() {
    2. string str = "hello world";
    3. cout << str << endl;
    4. system("pause");
    5. return 0;
    6. }

    值得注意的是,string字符串数组中的每个字符都是char类型的:
     

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. string str = "abc";
    6. cout << typeid(str[0]).name() << endl;
    7. }

     

    并且不像一般的数组,数组名指向数组首个元素的地址,string数组的数组名指向一整个string

    1. int main()
    2. {
    3. string str = "abc";
    4. cout << *(&str) << endl;
    5. system("pause");
    6. return 0;
    7. }

    1. int main()
    2. {
    3. string str = "abc";
    4. cout << str << endl;
    5. system("pause");
    6. return 0;
    7. }

    原因参考:

    字符指针:

    char *str = "hello";

     在类似这样定义的char的字符串指针中指针变量ps指向的是字符串的首个字符的地址!而直接输出str的话会输出整个字符串,所以我们如果想输出字符串指针的某个字符的话需要用下面这种方式!

    1. int main()
    2. {
    3. char *str = "hello";
    4. cout << str << endl;
    5. cout << *str << endl;
    6. cout << *(str + 1) << endl;
    7. system("pause");
    8. return 0;
    9. }

     

     上述声明方式相当于:

    1. int main()
    2. {
    3. char *str;
    4. str = "hello";
    5. cout << str << endl;
    6. cout << *str << endl;
    7. cout << *(str + 1) << endl;
    8. system("pause");
    9. return 0;
    10. }

    指针数组

    1. int main()
    2. {
    3. char *str[] = {"Hello", "C++", "World"}; // char (*str)[] = ...
    4. int i;
    5. for (i = 0; i < 3; i++)
    6. cout << str[i] << endl;
    7. system("pause");
    8. return 0;
    9. }
    10. // str[0]字符串"hello"的首地址,str[0]+1:字符串"hello"第二个字符'e'的地址,str[2]=str+2:第三个字符串"world"的首地址
    11. // str[1]字符串"C++"的首地址
    12. // str[2]字符串"world"的首地址

     

     或

    1. #include
    2. #include
    3. int main()
    4. {
    5. char *str[] = {"Hello", "C++", "World"};
    6. char **p; //指向指针的指针
    7. for(p=str; p3; p++)
    8. puts(*p); #*p为字符串首地址,*p[0]为字符串的第一个字符地址
    9. }

     将string赋给char

    如果试图把string类型的对象直接赋给C风格的字符串的话,编译器会报错的。

    1. int main()
    2. {
    3. string var = "abc";
    4. char *ptr = var;
    5. cout << *ptr << endl; // correct
    6. system("pause");
    7. return 0;
    8. }

     但是实际应用中这个问题也难以避免,很多时候我们还是需要将string类型的转化为char*来实现自定义的操作,C++标准库也为了和之前用C写的程序兼容,于是可以用string的c_str()函数(c_str():生成一个const char*指针,指向以空字符终止的数组。),更多有关c_str函数的优缺点以及替换方案查看这篇博客:c++中c_str()的用法详解_hold_on2014的博客-CSDN博客

    1. int main()
    2. {
    3. string var = "abc";
    4. char *ptr = var.c_str(); //错误!"const char *" 类型的值不能用于初始化 "char *" 类型的实体
    5. cout << *ptr << endl; // correct
    6. system("pause");
    7. return 0;
    8. }

    或者通过const_cast去掉其const属性亦可

    1. int main()
    2. {
    3. string var = "abc";
    4. char *ptr = const_cast<char *>(var.c_str());// correct
    5. cout << *ptr << endl; // correct
    6. system("pause");
    7. return 0;
    8. }

    但是c_str()为了防止意外地修改string对象,返回的是const指针,所以上面这段代码是不能被编译的。正确的应该是用const指针。

    1. int main()
    2. {
    3. string var = "abc";
    4. const char *ptr = var.c_str();
    5. cout << *ptr << endl; // correct
    6. system("pause");
    7. return 0;
    8. }

     

  • 相关阅读:
    【面试】软件自动化测试岗位面试题和答案
    (59、60、61、62)CPU
    【HCIA】生成树
    基于springboot漫画管理系统springboot001
    JVM相关知识
    Linux:gcc编译器
    【Apollo】感知工程安装测试
    【Python】创建一个简易服务器并实现移动端快速访问电脑文件
    Linux之Shell(二)
    多功能电力仪表在物联网的应用
  • 原文地址:https://blog.csdn.net/qq_55621259/article/details/126275523