stl

STL 之 partition()方法 和 stable_partition()方法

自作多情 提交于 2020-02-01 21:45:42
转自: https://blog.csdn.net/godenlove007/article/details/7982307 这两个方法都用来将指定容器的元素根据指定的predicate函数分成两个子序列,其中满足predicate()函数的,返回值为true的作为第一个序列[v.begin(), bound), 而[bound, v.end())的作为第二个序列。两个方法的区别在于, partition()对于两个子序列中的元素并不排序,而stable_partition()则对两个子序列的元素也进行排序。 BidirectionalIterator partition ( BidirectionalIterator first, BidirectionalIterator last, Predicate pred ); BidirectionalIterator stable_partition ( BidirectionalIterator first, BidirectionalIterator last, Predicate pred ); Parameters: first, last 第一个和第二个参数说明给定源容器的范围 [first, last) pred 第三个参数给定进行分组的规则函数 布尔型返回值 对于返回true的所有元素作为第一个子序列

[stl] cf div2 1295C

时光总嘲笑我的痴心妄想 提交于 2020-02-01 20:17:26
题目 题目链接: 题意 从第一个字符串里面找子串拼成第二个字符串,求最少要多少个字串 思路 可以记录第一个式子的字母出现的字母,如果里面没有第二个式子里的字母即输出-1,后面求字串拼凑的时候,可以对s2求它在s1中的位置,如果他在s1中的位置都小于s2中的下标,就相当于从头开始重新找字符串。可以用upper_bound()查找第一个大于s2的坐标。 lower_bound(v.begin(),v.end(),a)找出vector里第一个大于等于a的值。 upper_bound(v.begin(),v.end(),a)找出vector里第一个大于a的值。 *此题不能用lower_bound ,否则a aaaa输出1 代码 # include <cstdio> # include <cstring> # include <cmath> # include <cstdlib> # include <cctype> # include <ctime> # include <iostream> # include <string> # include <map> # include <queue> # include <stack> # include <set> # include <vector> # include <iomanip> # include <list> #

Need some advice to choose the proper container

末鹿安然 提交于 2020-02-01 08:23:06
问题 I'm trying to design a task scheduler to a game engine. A task could be an animation, a trigger controller, etc. My problem is what container to choose. The idea is: when you insert a new task, the container must reorder and put the task in the proper place. Once executed, task could change and be scheduled again or deleted. This is mainly push and pop. But, if possible, it would be nice if I could have random access to an element, but not vital. No matter if the container supports one or

C++语法之STL映射类

好久不见. 提交于 2020-01-31 05:12:34
键值对容器,在需要频繁快速查找时使用 map和multimap类 头文件:< map> 内部结构类似二叉树实现,在插入元素的时进行排序,所以不能替换特定位置的元素 map和multimap的差别在于能不能储存唯一的键 实例化: std :: map < keyType , valueType , Predicate = std :: less < keyType >> Name ; std :: multimap < keyType , valueType , Predicate = std :: less < keyType >> Name ; //示例 // map and multimap key of type int to value of type string map < int , string > mapIntToStr1 ; multimap < int , string > mmapIntToStr1 ; // map and multimap constructed as a copy of another map < int , string > mapIntToStr2 ( mapIntToStr1 ) ; multimap < int , string > mmapIntToStr2 ( mmapIntToStr1 ) ; // map and

STL源码剖析之vector

送分小仙女□ 提交于 2020-01-30 12:03:52
容器vector的底层实现是由数组(连续的线性空间)实现的,veector是动态分配内存空间的,也就是说,随着元素的加入,它的内部机制会自行扩充空间以便于容纳新元素。因此vector的底层实现技术的核心是对大小的控制以及重新分配内存空间时对数据的移动效率。因为vector的底层实现是由数组来实现的,所以vector的迭代器就是原生指针。vector的简单实现如下: 我把迭代器的内嵌的相应的型别封装的起来。 template<typename T> struct IteratorType { typedef T Value_type; //迭代器所指之物的型别 typedef T* Pointer; typedef T* Iterator; //vector的迭代器 typedef T& Reference; typedef size_t Size_type; //vector容器的大小 typedef size_t Difference_type; //迭代器之间的距离 }; #include"IteratorType.h" #pragma once template<typename T> class Vector { public: Vector() :start(nullptr), finish(nullptr), end_finish(nullptr) {}; Vector

STL序列式容器

筅森魡賤 提交于 2020-01-30 09:35:15
1、vector 空间运用的灵活性。 实现技术——关键是对大小的控制以及重新配置时的数据移动效率。 配置新空间、数据移动、释还旧空间 erase(int position)先移动覆盖元素之后删除最后一个 vector维护的是一个连续线性空间提供的是Random Access Iterators find(begin,end,k)如果找到k则iter指向k,如果没找打iter指向end。当在此插入元素的时候,如果插入成功返回*iter的时候的时候程序出错(内存的原因,可能是经过插入步骤之后iter指针失效,但从新遍历整个vector时是正确的,刚插入的元素也在里边)。--------不清楚为什么不失效??? 所谓的动态增加大小,并不是在原空间之后连续新空间(因为无法保证空间之后尚有可供配置的空间),对vector的任何操作,一旦引起空间重新配置,指向原vector的所有迭代器就都失效了。 insert(position,n,x),插入点之后现有元素>=n,先把position+n----finish复制到finish之后,在吧position-----position+n复制到finish之前,之后在插入元素。插入点之后现有元素<=n,先插入n-(finish-position)个x,之后把position----finish(旧的那个,不是现在更行的finish)复制到x之后的内存

C++ STL容器之map 简单使用

梦想的初衷 提交于 2020-01-30 06:10:40
3.8.2.1 map构造函数 map<T1, T2> mapTT;//map默认构造函数: map(const map &mp);//拷贝构造函数 3.8.2.2 map赋值操作 map& operator=(const map &mp);//重载等号操作符 swap(mp);//交换两个集合容器 3.8.2.3 map大小操作 size();//返回容器中元素的数目 empty();//判断容器是否为空 3.8.2.4 map插入数据元素操作 map.insert(...); //往容器插入元素,返回pair<iterator,bool> map<int, string> mapStu; // 第一种 通过pair的方式插入对象 mapStu.insert(pair<int, string>(3, "小张")); // 第二种 通过pair的方式插入对象 mapStu.inset(make_pair(-1, "校长")); // 第三种 通过value_type的方式插入对象 mapStu.insert(map<int, string>::value_type(1, "小李")); // 第四种 通过数组的方式插入值 mapStu[3] = "小刘"; mapStu[5] = "小王"; 3.8.2.5 map删除操作 clear();//删除所有元素 erase(pos);/

setw() does not affect reading integer fields

那年仲夏 提交于 2020-01-30 05:15:38
问题 I wrote a code like this: int d{ 0 }; cin >> setw(2) >> d; But it seems setw() has no effect on reading integers. If so, how we could implement behaviour of %2d of scanf() with istream ? 回答1: setw() is not designed to be used with integral types. What would it do? Extract the last two decimal digits of the integer? What would happen if you had put std::hex into the stream? The best approach is to read the number then deal with it yourself. For example, if you want to extract the least

Why is std::bitset<8> 4 bytes big?

醉酒当歌 提交于 2020-01-30 05:05:36
问题 It seems for std::bitset<1 to 32>, the size is set to 4 bytes. For sizes 33 to 64, it jumps straight up to 8 bytes. There can't be any overhead because std::bitset<32> is an even 4 bytes. I can see aligning to byte length when dealing with bits, but why would a bitset need to align to word length, especially for a container most likely to be used in situations with a tight memory budget? This is under VS2010. 回答1: I assume that indexing into the bitset is done by grabbing a 32-bit value and

STL源码剖析读书笔记之vector

元气小坏坏 提交于 2020-01-30 02:36:06
STL源码剖析读书笔记之vector 1.vector概述 vector是一种序列式容器,我的理解是 vector就像数组。 但是数组有一个很大的问题就是当我们分配 一个一定大小的数组的时候,起初 也许我们不会觉得数组容量太小不合需求,但是随着数据量的增加, 数组尺寸 大小不再满足需求,此时我们需要手动的去扩展其大小。然而vector就帮我们 完全实现了一个可 自适应增长的数组功能。 那么这样看来vector其实也就是一种可自适应增长的动态数组的类的实现。 2.关于vector的定义 其实用过vector的人都知道 vector的定义大都像这个样子: 1 vector<int> v1; 2 vector<sting> v2; 很显然vector是一种与类型无关的类模板,支持各种类型的vector,这 也很好的体现了C++的泛型思想,正 是这 种类型无关性,使得STL在各种 平台上广为使用。 这里我只是贴出STL源码剖析书中一小部分vector定义 1 template <class T,class Alloc=alloc> 2 class vector 3 { 4 public: 5 typedef T value_type; 6 typedef valuetype* pointer; 7 typedef valuetype* iterator; 8 typedef