前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【C++】基础:STL字符串库string

【C++】基础:STL字符串库string

作者头像
DevFrank
发布2024-07-24 15:59:45
1140
发布2024-07-24 15:59:45
举报
文章被收录于专栏:C++开发学习交流

😏1. 字符串库string介绍

在C++中,std::string是一个表示字符串的类,它是C++标准库中的一部分。std::string提供了许多功能和操作,使得字符串的处理更加方便和高效。

参考:https://zh.cppreference.com/w/cpp/string/basic_string

std::string的使用使得字符串的处理更加方便和安全,它提供了许多成员函数和操作符重载来简化字符串的操作。在C++中,std::string通常是首选的字符串表示方式,而不是C风格的字符数组。

😊2. 字符串构造

代码语言:javascript
复制
#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << __cplusplus << endl;  // -std=c++17
    
    // 1.无参构造
    string str;

    // 2.用=赋值
    string str2 = "hello";

    // 3.用char*传参
    string str3("ABC");

    // 4.重复字符构造
    string str4(10, 'A');

    // 5.拷贝构造
    string str5(str2);

    // 6.移动构造
    string str6(move(str5));

    // 7.构造指定范围内的字符
    string str7(str2, 3, 2);

    // 8.字符串拼接
    string str8 = str2 + " " + str3;
    cout << str8 << endl;
    
    return 0;
}

😆3. 元素访问

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << __cplusplus << endl;  // -std=c++17
    
    // 元素访问有以下几种方式
    // 1.at()
    // 2.[]
    // 3.front()
    // 4.back()
    // 5.c_str()
    // 6.data()

    string str = "hello world";
    str.at(2) = 'a';
    cout << str.at(2) << endl;
    cout << str[1] << endl;
    cout << str.front() << endl;
    cout << str.back() << endl;
    cout << str.c_str() << endl;
    cout << str.data() << endl;
    
    return 0;
}

😆4. 容量操作

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << __cplusplus << endl;  // -std=c++17
    
    // 容量操作
    // 1.empty()
    // 2.size() length() 字节
    // 3.max_size()
    // 4.resize(size_type cnt) resize(size_type cnt, CharT ch)
    // 5.reserve()
    // 6.capacity()
    // 7.shrink_to_fit()
    string str = "hello world";
    string str2 = "你好";
    cout << boolalpha << str.empty() << endl;
    cout << str2.size() << endl; // 一个汉字3字节
    cout << str.length() << endl;
    cout << str.max_size() << endl;
    // str.resize(10);
    str.resize(20, 'A');
    cout << str << endl;
    cout << str.capacity() << endl; // 分配的容量

    str.reserve(100); // 分配100个字符的容量
    cout << str.capacity() << endl;
    str.shrink_to_fit(); // 减少容量
    cout << str.capacity() << endl;
    
    return 0;
}

😆5. 迭代器

在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << __cplusplus << endl;  // -std=c++17
    
    // 迭代器 - 为容器类提供统一的遍历接口
    // 1.正向迭代器 iterator
    // 2.正向只读迭代器 const_iterator
    // 3.反向迭代器 reverse_iterator
    // 4.反向只读迭代器 const_reverse_iterator
    string str = "hello world";
    string::iterator it = str.begin(); // auto string::iterator(不可修改)
    cout << *it << endl; // h
    it++; // it += 2
    cout << *it << endl; // e
    *it = 'E';
    cout << *it << endl; // E
    cout << str << endl;

    for (; it != str.end(); it++)
    {
        cout << *it << endl; // print
    }

    string::reverse_iterator rit = str.rbegin();
    for (; rit != str.rend(); rit++)
    {
        cout << *rit << endl; // reverse print
    }
    
    return 0;
}

😆6. 字符串相关操作

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码语言:javascript
复制
#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << __cplusplus << endl;  // -std=c++17
    
    // 字符串相关操作
    // 1.==
    // 2.compare()
    // 3.starts_with() ends_with() // std20
    // 4.contains() // std23
    // 5.append()
    // 6.insert()
    // 7.push_back()
    // 8.pop_back()
    // 9.clear()
    // 10.erase()
    // 11.replace()
    // 12.substr()
    // 13.find()
    // 14.rfind()
    // 15.to_string()
    // 16.stoi() stof()
    // 17.hash
    string str = "hello world";
    string str2 = "Hello World";

    cout << boolalpha << (str == str2) << endl;
    cout << (str2.compare(str) == 0) << endl;

    cout << str.append("!!!").append(" C++") << endl;
    cout << str.insert(5, ",") << endl;
    str.push_back('!');
    str.pop_back();
    cout << str << endl;
    // cout << str.erase(5, 6) << endl;
    // str.clear();

    str.replace(str.begin(), str.end(), str2);
    cout << str << endl;
    cout << str.substr(2) << endl;

    cout << str.find("lo") << endl; // 用 npos 判断索引
    cout << str.rfind("l") << endl;

    cout << stoi("123") << endl;
    cout << stof("123.45") << endl;
    cout << to_string(123) << endl;

    hash<string> hs;
    cout << hs(str) << endl; // 计算哈希值
    
    return 0;
}

😆7. string_view

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <string_view>

using namespace std;

int main()
{
    cout << __cplusplus << endl;  // -std=c++17
    
    // string_view 字符串共享内存,避免重新分配
    const char *s = "hello, c++ stl";
    cout << uintptr_t(s) << endl;

    string_view sv = s;
    cout << uintptr_t(sv.data()) << endl;
    cout << sv << endl;

    // 移除字节
    sv.remove_prefix(7);
    cout << sv << endl;
    
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-01-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 😏1. 字符串库string介绍
  • 😊2. 字符串构造
  • 😆3. 元素访问
  • 😆4. 容量操作
  • 😆5. 迭代器
  • 😆6. 字符串相关操作
  • 😆7. string_view
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档