• C++字符串


    动态字符串

    C++中定义一些来自c语言的字符串函数,在头文件中。通常,这些函数不直接操作内存分配。

    1. strlen(str)返回字符串长度,不包括

    使用安全C库: strlen_s 也在

    C++的string类

    #include <string>
    
    using namespace std;
    
    const string s1("hello");
    const string s2 = " world";
    string s3 = s1 + s2;
    

    在string类中,运算符 ==, +,>,<,[]等都被重载了

    数值转换

    string to_char(int);
    string to_char(unsigned);
    string to_char(long);
    string to_char(unsigned long);
    string to_char(long long);
    ...
    

    demo

    float f = 3.14;
    string s3 =to_string(f);
    cout << s3 << endl; /// 3.140000
    
    int stoi(const string& str, size_t * idx = 0, int base = 10);
    

    字符串转数值,idx: 未转换字符的索引,base:进制

    demo

    string s = "3.14";
    float f = stof(s);
    cout << f << endl;
    

    原始字符串

    1. 单行
    "hello "world ""
    

    等价于:

    R"(hello "world ")"
    
    1. 跨行
    R"(hello
    world)"
    
    1. 特殊字符 ()

    使用不会出现的字符作为分隔字符,如:

    R"-hello (wolrd) !-"
    

    在C++14中,只能使用()作为开始结束标识符,并且中间也可以输入括号字符

    string s = R"(hello )wolrd) !)"; // C++14中合法
    
  • 相关阅读:
    e.g.-basic-Si
    Telephone interview with Youyou Tu
    Mo2C-tag
    Usage of “symmetrical” and “symmetric”
    Xcrysden-2
    The partial charge density (1)
    利用 AWK 的数值计算功能提升工作效率(转载)
    扩展程序
    选择排序
    装饰递归函数
  • 原文地址:https://www.cnblogs.com/zhuxiang1633/p/13235720.html
Copyright © 2020-2023  润新知