快速上手STL(非常有用)

核心组件简要介绍

C++标准模板库STL的核心包括以下三个组件:
(1)容器:容器是用来管理某一类对象的集合。
(2)算法:算法作用于容器。它们提供了执行各种操作的方式,包括对容器内容执行初始化、排序、搜索和转换等操作。
(3)迭代器:迭代器用于遍历对象集合的元素。这些集合可能是容器,也可能是容器的子集。

各个容器使用说明(排序不分先后)

一. Set(集合)

1. 声明:

#include<set>
std::set<int> s;
std::set<char> s;
std::set<pair<int,int> > s; set<node> s;
struct node{...};

2. 构造:

1.使用默认构造函数创建一个空的unordered_set

std::unordered_set<int> mySet;

2.使用初始化列表直接初始化unordered_set:

std::unordered_set<int> mySet = {1, 2, 3, 4, 5};

3.使另一个容器的迭代器范围来初始化unordered_set:

std::vector<int> myVector = {1, 2, 3, 4, 5};
std::unordered_set<int> mySet(myVector.begin(), myVector.end());

4.通过拷贝另一个unordered_set来初始化新的unordered_set:

std::unordered_set<int> originalSet = {1, 2, 3};
std::unordered_set<int> mySet(originalSet);

5.使用移动语义来初始化unordered_set(C++11及以上版本):

std::unordered_set<int> tempSet = {1, 2, 3};
std::unordered_set<int> mySet(std::move(tempSet));

6.使用std::initializer_list构造unordered_set:

std::unordered_set<int> mySet = std::initializer_list<int>({1, 2, 3, 4, 5});

3. 类对象的访问及遍历操作:

在这里插入图片描述

4. 分类

在这里插入图片描述

二. String(字符串)

1. 声明:

#include <string>

std::string str;

2. 构造:

    std::string str1; 								// 默认初始化,结果是空字符串 ""
    std::string str2 = "Hello"; 					// 通过等号初始化
    std::string str3("World"); 						// 直接初始化
    std::string str4 = std::string("Hello World");  // 显示调用构造函数初始化
    std::string str5(str2); 						// 拷贝初始化
    std::string str6(10, 'x');					    // 重复字符初始化,结果是 "xxxxxxxxxx"

3. 类对象的访问及遍历操作:

函数名称功能说明
s[pos]返回pos位置的字符,const string类对象调用
s.begin()获取一个字符的迭代器
s.end()获取最后一个字符下一个位置的迭代器
s.size() / s.length()返回字符串的长度
s.capacity()返回字符串在重新分配内存之前可以容纳的最大字符数
s.empty()返回一个布尔值,指示字符串是否为空
s.front() / s.back()返回第一个字符,返回最后一个字符
s.push_back()/ s.pop_back()在字符串末尾添加一个字符, 删除字符串末尾的一个字符
s.append()在字符串末尾添加字符或子串
s.insert()在指定位置插入字符或子串
s.erase()删除指定位置的字符或子串
s.find()在字符串中查找子串,返回子串的位置。
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值