C++ String类使用文档

元素访问

  • [i]: 访问下标为i的元素
  • at(i): 与[i]相同
  • front(): 访问首元素
  • back(): 访问尾元素
  • c_str(): 返回不可修改的字符数组

示例代码:

#include <iostream>
#include <string>

using namespace std;

int main() {
    string str = "hello world";
    cout << str[0] << endl; //输出 'h'
    cout << str.at(1) << endl; //输出 'e'
    cout << str.front() << endl; //输出 'h'
    cout << str.back() << endl; //输出 'd'
    cout << str.c_str() << endl; //输出 "hello world"
    return 0;
}

容量

  • empty(): 检测是否为空
  • size(): 返回字符数
  • length(): 返回字符数

示例代码:

#include <iostream>
#include <string>

using namespace std;

int main() {
    string str = "hello world";
    cout << str.empty() << endl; //输出 0
    cout << str.size() << endl; //输出 11
    cout << str.length() << endl; //输出 11
    return 0;
}

操作

  • clear(): 清除内容
  • insert(): 插入字符
  • erase(): 删除字符
  • push_back(): 添加字符到末尾
  • pop_back(): 移除末尾字符
  • append(): 后附字符到结尾
  • +=str2: 后附字符串到结尾
  • compare(): 比较两个字符串
  • replace(): 替换字符串的指定部分
  • substr(): 返回子串
  • copy(): 复制字符

示例代码:

#include <iostream>
#include <string>

using namespace std;

int main() {
    string str = "hello world";
    str.clear(); //清除内容
    cout << str << endl; //输出 ""

    string str1 = "world";
    string str2 = "hello";
    str1.insert(0, str2); //在下标为0处插入字符串str2
    cout << str1 << endl; //输出 "helloworld"

    string str3 = "hello world";
    str3.erase(0, 6); //删除下标0-6之间的字符
    cout << str3 << endl; //输出 "world"

    string str4 = "hello";
    str4.push_back('w'); //在末尾添加字符'w'
    cout << str4 << endl; //输出 "hellow"

    string str5 = "helloworld";
    str5.pop_back(); //移除末尾字符
    cout << str5 << endl; //输出 "helloworl"

    string str6 = "hello";
    str6.append("world"); //在末尾附加字符串
    cout << str6 << endl; //输出 "helloworld"

    string str7 = "hello";
    string str8 = "world";
    str7 += str8; //在末尾附加字符串
    cout << str7 << endl; //输出 "helloworld"

    string str9 = "hello";
    string str10 = "world";
    int result = str9.compare(str10); //比较两个字符串
    if(result == 0) {
        cout << "两个字符串相等" << endl;
    } else if(result < 0) {
        cout << "str9 < str10" << endl;
    } else {
        cout << "str9 > str10" << endl;
    }

    string str11 = "hello world";
    str11.replace(0, 5, "hi"); //替换下标0-5之间的字符为"hi"
    cout << str11 << endl; //输出 "hi world"

    string str12 = "hello world";
    string sub_str = str12.substr(0, 5); //获取下标0-5之间的子串
    cout << sub_str << endl; //输出 "hello"

    string str13 = "hello world";
    char str14[20];
    str13.copy(str14, 5, 0); //将下标0-5之间的字符复制到str14中
    cout << str14 << endl; //输出 "hello"
    return 0;
}

查找

  • find(): 于字符串中寻找字符
  • rfind(): 寻找子串中最后一次出现

示例代码:

#include <iostream>
#include <string>

using namespace std;

int main() {
    string str = "hello world";
    size_t index = str.find('o'); //查找'o'字符在字符串中的位置
    cout << index << endl; //输出 4

    string str1 = "hello world";
    size_t index1 = str1.rfind('o'); //查找'o'字符在字符串中最后一次出现的位置
    cout << index1 << endl; //输出 7
    return 0;
}

数制转换

  • stoi: 转换字符串为有符号整数
  • stod: 转换字符串为浮点值
  • to_string: 转换整数或浮点值为string

示例代码:

#include <iostream>
#include <string>

using namespace std;

int main() {
    string str = "123";
    int num = stoi(str); //将字符串转换为整数
    cout << num << endl; //输出 123

    string str1 = "3.14";
    double num1 = stod(str1); //将字符串转换为浮点值
    cout << num1 << endl; //输出 3.14

    int num2 = 123;
    string str2 = to_string(num2); //将整数转换为字符串
    cout << str2 << endl; //输出 "123"
    return 0;
}

0 条评论

目前还没有评论...

信息

ID
2057
时间
1000ms
内存
256MiB
难度
10
标签
(无)
递交数
3
已通过
0
上传者