stl

stl内存管理

心已入冬 提交于 2020-03-21 18:50:23
STL提供了很多泛型容器,如vector,list和map。程序员在使用这些容器时只需关心何时往容器内塞对象,而不用关心如何管理内存,需要用多少内存,这些STL容器极大地方便了C++程序的编写。例如可以通过以下语句创建一个vector,它实际上是一个按需增长的动态数组,其每个元素的类型为int整型: stl::vector<int> array; 拥有这样一个动态数组后,用户只需要调用push_back方法往里面添加对象,而不需要考虑需要多少内存: array.push_back(10); array.push_back(2); vector会根据需要自动增长内存,在array退出其作用域时也会自动销毁占有的内存,这些对于用户来说是透明的,stl容器巧妙的避开了繁琐且易出错的内存管理工作。 隐藏在这些容器后的内存管理工作是通过STL提供的一个默认的allocator实现的。当然,用户也可以定制自己的allocator,只要实现allocator模板所定义的接口方法即可,然后通过将自定义的allocator作为模板参数传递给STL容器,创建一个使用自定义allocator的STL容器对象,如: stl::vector<int, UserDefinedAllocator> array; 大多数情况下,STL默认的allocator就已经足够了

操作序列(STL map)

与世无争的帅哥 提交于 2020-03-21 05:59:47
链接: https://ac.nowcoder.com/acm/contest/4462/A 来源:牛客网 题目描述 给出一个长度无限的数列,初始全部为零,有三种操作: 增加操作:给下标为 t 的数加 c 。特别注意,如果在下标 [t−30,t+30] 内有不为零的数,增加操作无效。 削减操作:让数列中下标最小的不为零数变为零。 查询操作:查询数列中下标为 t 的数字是多少。 输入描述: 第一行包含一个整数 N,1≤N≤10 6 ,表示操作总数。 随后 N 行,每行由两个数字或一个数字组成。 若一行中有两个数字,分别代表增加操作的 t,c 。 若一行中只有数字-1,执行削减操作。 若一行中只有一个不为 -1的数字,则代表查询操作的数字 t。 保证t,c均为非负整数且在整形范围内。 输出描述: 削减操作时,先输出该数字,再变为零 若序列元素全为零,则削减操作无效,此时输出 "skipped" 查询时,输出该位置上的数 示例1 输入 7 140 1 120 2 100 3 120 100 -1 100 输出 0 3 3 0 示例2 输入 4 140 3 -1 140 1 -1 输出 3 1 示例3 输入 3 -1 -1 -1 输出 skipped skipped skipped 1 #include <bits/stdc++.h> 2 typedef long long LL; 3

C++ STL排序问题

心不动则不痛 提交于 2020-03-21 05:33:59
/* stl排序 */ #include <iostream> #include <map> #include <vector> #include <list> #include <algorithm> using namespace std; struct STComp :public binary_function<int, int, bool> { inline bool operator()(int x, int y) { //返回true,表示不用变更位置,从大到小排序 return x > y; } }; struct STPrint :public unary_function<int, bool> { inline void operator()(int x) { cout << x << " " ; } }; void test1() { vector<int> v1 = { 1,2,3,4,5,6,7,8 }; //vector排序 sort(v1.begin(), v1.end(), STComp()); for_each(v1.begin(), v1.end(), STPrint()); } void test2() { std::list<int> alist; alist.push_back(3); alist.push_back(4); alist

C++ STL栈和队列

安稳与你 提交于 2020-03-21 02:49:30
在C++标准库(STL)中,实现了栈和队列,方便使用,在这里我整理了一下笔记,作简要介绍。 1,栈(stack): 头文件 : #include<stack> 定义栈 :stack<Type> s; 其中Type为数据类型(如 int,float,char)。 主要操作: s.push(item); //将item压入栈顶 s.pop(); //删除栈顶的元素,但不会返回 s.top(); //返回栈顶的元素,但不会删除 s.size(); //返回栈中元素的个数 s.empty(); //检查栈是否为空,如果为空返回true,否则返回false 2,队列(queue): 头文件 : #include<queue> 定义队列: queue<Type> q; 其中Type为数据类型(如 int,float,char)。 主要操作: q.push(item) //将item压入队列尾部 q.pop() //删除队首元素,但不返回 q.front() //返回队首元素,但不删除 q.back() //返回队尾元素,但不删除 q.size() //返回队列中元素的个数 q.empty() //检查队列是否为空,如果为空返回true,否则返回false 3,双端队列 头文件:#include <deque>deq 定义双端队列:deque<Type> deq 其中Type为数据类型(如

STL优先队列详解

…衆ロ難τιáo~ 提交于 2020-03-21 02:49:18
优先队列 优先队列是一种抽象数据类型(Abstract Date Type,ADT),行为和队列类似,但是先出队的元素不是先进队列的元素,而是队列中优先级最高的元素。 STL的优先队列定义在头文件<queue>和 (队列一样),用"priority_queue<int>pq"来声明; 最基本的用法 定义 : priority_queue<int>pq ; 操作: pq.empty() 如果队列为空返回真 pq.pop() 删除对顶元素 pq.push() 加入一个元素 pq.size() 返回优先队列中拥有的元素个数 pq.top() 返回优先队列对顶元素 下面我们介绍几种优先队列的定义方式: priority_queue<int>pq 默认的是整数越大,优先级越高,如果想让他 整数越小 优先级越高怎么办? STL中也有模板 “priority_queue<int,vector<int>,greater<int > >pq” 上面的尖括号内第一个 参数 为入队元素类型(int),第二个为容器类型(vector<int>),第三个为比较函数(greater<int>) 因此我们也能自定义 priority_queue<int,vector<int>,cmp1 >pq; 最小值优先 priority_queue<int,vector<int>,cmp2 >pq2; 最大值优先

STL之优先队列

大兔子大兔子 提交于 2020-03-21 02:48:50
STL 中优先队列的使用方法(priority_queu) 基本操作: empty() 如果队列为空返回真 pop() 删除对顶元素 push() 加入一个元素 size() 返回优先队列中拥有的元素个数 top() 返回优先队列对顶元素 在默认的优先队列中,优先级高的先出队。在默认的int型中先出队的为较大的数。 使用方法: 头文件: #include <queue> 声明方式: 1、普通方法: priority_queue < int > q; // 通过操作,按照元素从大到小的顺序出队 2、自定义优先级: struct cmp { operator bool ()( int x, int y) { return  x > y; // x小的优先级高 // 也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高 } }; priority_queue < int , vector < int > , cmp > q; // 定义方法 // 其中,第二个参数为容器类型。第三个参数为比较函数。 3、结构体声明方式: struct node { int x, y; friend bool operator < (node a, node b) { return a.x > b.x; // 结构体中,x小的优先级高 } }; priority

C++ Error: Conversion to Non-Scalar Type [closed]

自作多情 提交于 2020-03-18 17:30:08
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . I seem to be having a peculiar error in the following code segment (ignore the excess header files and the blank main function, I just wanted to isolate

C++ Error: Conversion to Non-Scalar Type [closed]

谁都会走 提交于 2020-03-18 17:28:27
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 7 years ago . I seem to be having a peculiar error in the following code segment (ignore the excess header files and the blank main function, I just wanted to isolate

STL注意

扶醉桌前 提交于 2020-03-18 06:08:09
stl中基本容器有string vector list deque set map set 和map都是无序的保存元素 只能通过它提供的接口对里面的元素进行访问 set 集合, 用来判断某一个元素是不是在一个组里面 使用的比较少 map 映射 相当于字典 把一个值映射成另一个值 如果想创建字典的话使用它好了 string vector list deque set 是有序容器 string 是basic_string<char> 的实现, 在内存中是连续存放的 为了提高效率,都会有保留内存,如string s= "abcd",这时s使用的空间可能就是255, 当string再次往s里面添加内容时不会再次分配内存 直到内容>255刊才会再次申请内存 因此提高了它的性能 当内容>255时 string会先分配一个新内存 然后再把内容复制过去 再复制先前的内容 对string的操作 如果是添加到最后时 一般不需要分配内存 所以性能最快 如果是对中间或是开始部分操作 如往那里添加元素或是删除元素 或是代替元素 这时需要进行内存复制 性能会降低 如果删除元素 string一般不会释放它已经分配的内存 为了是下次使用时可以更高效 由于string会有预保留内存 所以如果大量使用的话 会有内存浪费 这点需要考虑 还有就是删除元素时不释放过多的内存 这也要考虑 string中内存是在堆中分配的

STL(二)

社会主义新天地 提交于 2020-03-17 17:39:07
文章目录 1. stack栈容器 2. queue 队列容器 3. List容器 4. set容器 5. map容器 1. stack栈容器 1.1先进后出 1.2栈顶 top 1.3压栈 push 1.4弹出栈顶 pop 1.5大小 size 1.6为空 empty 02 stack栈容器.cpp # define _CRT_SECURE_NO_WARNINGS # include <iostream> # include <stack> using namespace std ; /* stack构造函数 stack<T> stkT;//stack采用模板类实现, stack对象的默认构造形式: stack(const stack &stk);//拷贝构造函数 3.4.3.2 stack赋值操作 stack& operator=(const stack &stk);//重载等号操作符 3.4.3.3 stack数据存取操作 push(elem);//向栈顶添加元素 pop();//从栈顶移除第一个元素 top();//返回栈顶元素 3.4.3.4 stack大小操作 empty();//判断堆栈是否为空 size();//返回堆栈的大小 */ void test01 ( ) { stack < int > s ; //放入数据 push s . push ( 10 ) ; s