leetcode 容器使用

本文详细介绍了C++标准库中的多种容器使用方法,包括vector、list、unordered_map、队列、栈、string、priority_queue、unordered_set、map等,涵盖了基本操作如增删改查及一些高级用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 容器使用

参考《C++ 基础教程 - 基础教程在线

1) vector

 

Vector相关操作增:void push_back(const T& x); vec.emplace_back(vector<int>{que.begain(),que.end}); //将队列的值都插入vector删:iterator erase(iterator first, iterator second);    iterator erase(iterator position);改:temp[i] = x; 下标操作即可查:#include <algorithm> ; vector<int>::iterator result = find(begin, end, value);  if(result != end) 代表找到了;找最大值: *max_element(begin(), end());求vector之和:accumulate(begin(), end(), val);  //求val + vector 元素之和, 当val = 0, 就是vector 之和。求vector某个值val出现的次数: count(begin(), end(), val);改变二维vector大小resize(n,vector<int>(m)); 

vector.pop_back();删除最后一个元素

vector<vector<int>> dp(n, vector<int>(m, 1);初始化n*m的dp表为1

2) list

list增: void push_front(const T& x); //头插    void push_back(const T& x); //尾插删: void pop_front();// 头部删除    void pop_back();// 尾部删除改:  xxxx查: front();//返回第一个元素的值    back(); //返回最后一个元素的值

3) unordered_map

定义:unordered_map<string, int> map增: insert(pair(key, value));  map[string] = int;//直接下标操作也可以增加<key,value>;改: tmp_map[key] = value; //下标操作查: 查: auto tmp = tmp_map.find(i); if (tmp != tmp_map.end() ) 代表找到了unordered_map.erase(val);删除val键值对; count(key) 计数;

 

4)队列

1 队列的定义: queue<TreeNode *> nodeQueue;//定义存放数据类型为TreeNode* 的队列;              deque<int> valueDeque;//定义存放数据类型为int的deque;2 增(入队):nodeQueue.push();valueDeque.push_back();//尾插  valueDeque.push_front();//头插3 删(出队):nodeQueue.pop(); valueDeque.pop_back();//尾出队  valueDeque.push_front();//头出队4 查: nodeQueue.front(); 返回队头元素5 判断队列是否为空: nodeQueue.empty();6 得到队列size: nodeQueue.size();             

5) 栈

栈定义:stack <int> Stack;入栈: Stack.push();访问栈头:Stack.top();出栈:StacK.pop();

6) string 

定义: string str;操作符:string str = str1 + str2 + to_string(int);字串: str.substr(pos, len); 长度:str.size();

7) priority_queue

priority_queue1 定义最大堆: priority_queue<int, vector<int>, less<int>>; // 最大堆先出大的值2 定义最小堆: priority_queue<int, vector<int>, greater<int>>;3 定义ListNode 最小堆:       struct Status {        int val;        ListNode* ptr;        bool operator < (const Status &rhs) const {            return val > rhs.val;        }     };    priority_queue<Status> pq; pq.push({val, node}); pq.top()      

8)unordered_set

unordered_set: 关联式容器,它与序列式容器相比存储的值都不是重复的。
增:emplace()/insert(); 删:erase()/clear(); 改:  ; 查:find()/count();

9) 优先级队列(二叉堆)

优先级队列(二叉堆):priority_queue
如何利用优先级队列保证值从小到大出队?
struct Status {
    int val;
    ListNode *ptr;
    bool operator < (const Status &rhs) const {
        return val > rhs.val;
    }
};
priority_queue<Status> q;
入队:q.push(); 取值:q.top(); 出队:q.pop()

10) map

map: 存储排序键值得容器, 增:insert(),[]下标操作; 删:erase(),clear(); 改:[]下标操作, 查:find();

 

 

### LeetCode C++ STL 容器使用教程 #### 常见容器概述 在LeetCode编程挑战中,C++标准模板库(STL)中的容器扮演着至关重要的角色。这些数据结构不仅能够简化代码编写过程,还能提高程序效率。对于求职者而言,在面试撕题阶段掌握STL容器的应用技巧显得尤为重要[^1]。 #### 字符串操作实例 考虑如下一段用于展示`string`类成员函数的简单例子: ```cpp #include <iostream> #include <string> using namespace std; int main(){ string s("hello world"); // 输出字符串长度 cout << "Size of the string is: " << s.size() << endl; // 查找子串位置 size_t found = s.find("world"); if (found != string::npos){ cout << "'world' first occurs at position: " << found << endl; } } ``` 上述代码片段展示了如何获取字符串的实际尺寸以及定位特定模式首次出现的位置[^3]。 #### 向量(Vector)基础用法 向量是最常使用的动态数组形式之一,支持随机访问其内部元素并允许高效地追加新项至末端。下面给出了一段创建、填充并向终端用户打印整型数值序列的方法: ```cpp #include <vector> #include <iostream> using namespace std; void displayVector(const vector<int>& v){ for(auto elem : v){ cout << elem << ' '; } cout << '\n'; } int main(){ vector<int> numbers{1, 2, 3}; displayVector(numbers); // 添加更多元素到现有列表里 numbers.push_back(4); numbers.emplace_back(5); displayVector(numbers); } ``` 这段示范说明了初始化带有初始值集合的方式,并介绍了两种不同的方法来扩展已存在的向量内容——通过调用`push_back()`或者更高效的就地构造版本`emplace_back()`[^2]。 #### 排序与自定义比较器 当涉及到对复杂对象进行排序时,可能需要提供额外的信息给sort算法以便正确处理各个记录之间的相对顺序关系。这里有一个利用lambda表达式实现降序排列整数数组的小案例: ```cpp #include<algorithm> #include<iostream> #include<vector> using namespace std; int main(){ vector<int> nums={6,5,9,1,2,8,7,3,4,0}; // 应用std::sort配合lambda表达式的组合完成逆序整理工作 sort(begin(nums), end(nums), [](const int& a,const int& b){return a>b;}); copy(cbegin(nums), cend(nums), ostream_iterator<int>(cout," ")); } ``` 此部分强调了灵活运用匿名函数定制化逻辑的重要性,使得开发者可以根据实际需求调整默认行为[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值