stl

[C++ STL] map使用详解

自闭症网瘾萝莉.ら 提交于 2020-01-29 22:36:13
一、概述 map 由红黑树实现,其元素都是 “键值/实值” 所形成的一个对组(key/value pairs)。每个元素有一个键,是排序准则的基础。每一个键只能出现一次,不允许重复。 map主要用于资料一对一映射的情况,map 内部自建一颗红黑树,这颗树具有对数据自动排序的功能,所以在 map 内部所有的数据都是有序的。比如一个班级中,每个学生的学号跟他的姓名就存在着一对一映射的关系。 二、定义及初始化 使用之前必须加相应容器的头文件: #include <map> // map属于std命名域的,因此需要通过命名限定,例如using std::map; 定义的代码如下: map<int, string> a; // 定义一个int类型的映射a // map<int, string> a(10); // error,未定义这种构造函数 // map<int, string> a(10, 1); // error,未定义这种构造函数 map<int, string> b(a); // 定义并用映射a初始化映射b // map<int, string> b(a.begin(), a.end()); // error,未定义这种构造函数 三、基本操作 3.1 容量函数 容器大小: mp.size(); 容器最大容量: mp.max_size(); 容器判空: mp.empty();

【STL】IP地址冲突了

跟風遠走 提交于 2020-01-29 17:36:59
描述 TZC机房的电脑居然可以修改IP,结果导致机房IP冲突,因为同时只能一个IP联网,因此很多同学的电脑无法上网了,现在给出一个机房的各个电脑的IP配置情况,如果只考虑冲突情况,问最多几台电脑能够同时联网? 输入 第一行为机房的电脑数量n(n<=100)。 接下来有n行,每行为一个ip地址,格式为a.b.c.d,其中0<=a,b,c,d<=255。 输出 输出最多可能同时联网的电脑数量。 样例输入 5 10.64.131.1 10.64.131.3 10.64.131.1 10.64.131.2 10.64.131.2 样例输出 3 题目来源 TZOJ 分析: 用了MAP就很好做,想了两种模拟的方法,但都有瑕疵。 代码: #include<bits/stdc++.h> using namespace std; int main() { string s; int T,ans=0; cin>>T; map<string,int> m; while(T–) { cin>>s; m[s]++; if(m[s]==1) ans++; } cout<<ans<<endl; return 0; } 来源: CSDN 作者: Skynamer 链接: https://blog.csdn.net/Skynamer/article/details/104107992

Heapify in logarithmic time using the C++ standard library

半世苍凉 提交于 2020-01-29 16:59:11
问题 I have a heap using std::make_heap : std::vector<int> v{1,2,3,5,9,20,3}; std::make_heap(v.begin(), v.end()); now I update the heap by changing one random element: v[3] = 35; Is there a way in standard library in to adjust heap again in O(log n) time where n is size of container. Basically I am looking for heapify function. I know what element has been changed. I understand that std::make_heap is O(n log n) time. I have also gone through duplicate question but that is different in sense that

Heapify in logarithmic time using the C++ standard library

寵の児 提交于 2020-01-29 16:58:48
问题 I have a heap using std::make_heap : std::vector<int> v{1,2,3,5,9,20,3}; std::make_heap(v.begin(), v.end()); now I update the heap by changing one random element: v[3] = 35; Is there a way in standard library in to adjust heap again in O(log n) time where n is size of container. Basically I am looking for heapify function. I know what element has been changed. I understand that std::make_heap is O(n log n) time. I have also gone through duplicate question but that is different in sense that

stl之std::remove_copy

℡╲_俬逩灬. 提交于 2020-01-29 12:26:48
template <class InputIterator, class OutputIterator, class T> OutputIterator remove_copy (InputIterator first, InputIterator last, OutputIterator result, const T& val); 说明: Copies the elements in the range [first,last) to the range beginning at result, except those elements that compare equal to val. 对range进行遍历,除了==val的值,其他的copy到以result开始的位置 算法结果等价于 template <class InputIterator, class OutputIterator, class T> OutputIterator remove_copy (InputIterator first, InputIterator last, OutputIterator result, const T& val) { while (first!=last) { if (!(*first == val)) { *result = *first; ++result; } +

C++ STL map按value排序

邮差的信 提交于 2020-01-29 00:15:14
(菜鸟的学习笔记,大佬们还是跳过吧) 放入map中的元素默认是按key排序的(用迭代器输出一遍就能发现) 按value进行排序,思路是先把map中的元素转存到vector,再用sort对vector进行排序 转存过程: //map <string, int> M; //vector <pair<string, int>> V; //map <string, int>::iterator mit; for ( mit = M . begin ( ) ; mit != M . end ( ) ; mit ++ ) { V . push_back ( make_pair ( mit - > first , mit - > second ) ) ; } 完整代码如下: # include <stdio.h> # include <iostream> # include <vector> # include <map> # include <string> # include <algorithm> using namespace std ; bool cmp ( const pair < string , int > & a , const pair < string , int > & b ) { return a . second > b . second ; } int main (

D. Colored Boots(STL)

我只是一个虾纸丫 提交于 2020-01-29 00:00:30
D. Colored Boots(STL、桶排) There are nn left boots and nn right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark (’?’). Thus, you are given two strings ll and rr, both of length nn. The character lili stands for the color of the ii-th left boot and the character riri stands for the color of the ii-th right boot. A lowercase Latin letter denotes a specific color, but the question mark (’?’) denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite)

C++迭代器(STL迭代器)

会有一股神秘感。 提交于 2020-01-28 14:38:10
迭代器按照定义方式可以分为以下四种: (1)正向迭代器,定义方法如下: 容器类名::iterator 迭代器名; (2)常量正向迭代器,定义方法如下: 容器类名::const_iterator 迭代器名; (3)反向迭代器,定义方法如下: 容器类名::reverse_iterator 迭代器名; (4)常量反向迭代器,定义方法如下: 容器类名::const_reverse_iterator 迭代器名; 通过迭代器,我们可以读取它指向的元素, *迭代器名 就表示迭代器所指向的元素,通过非常量迭代器还能修改其指向的元素 迭代器都可以进行 ++ 操作,反向迭代器和正向迭代器的区别在于: 对正向迭代器进行 ++ 操作时,迭代器会指向容器中的后一个元素; 对反向迭代器进行 ++ 操作时,迭代器会指向容器中的前一个元素; 注意:容器适配器stack,queue,priority_queue 没有迭代器。 常用的迭代器按功能强弱分为:输入,输出,正向,双向,随机访问 五种。 (1)正向迭代器 假设p是一个正向迭代器,则p支持以下操作: ++p,p++,*p,两个迭代器可以进行相互赋值,以及==,!=比较 (2)双向迭代器 双向迭代器具有正向迭代器的所有功能,并且可以进行 --p 和 p-- 的操作 (3)随机访问迭代器 随机访问迭代器具有双向迭代器的所有功能,并且还可以进行以下操作

Using an std::string as a key for an std::map

若如初见. 提交于 2020-01-28 06:24:10
问题 I would like to have an std::map (int .NET 4.0). We of course know that a map is a tree and requires an operator< that string does not define for us. Error 24 error C2676: binary '<' : 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files\microsoft visual studio 10.0\vc\include\xfunctional 125 1 FXCMMarketDataServer So I put my google-foo to work and found this solution: struct StringComparerForMap { public: bool

Using an std::string as a key for an std::map

雨燕双飞 提交于 2020-01-28 06:21:14
问题 I would like to have an std::map (int .NET 4.0). We of course know that a map is a tree and requires an operator< that string does not define for us. Error 24 error C2676: binary '<' : 'const std::string' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files\microsoft visual studio 10.0\vc\include\xfunctional 125 1 FXCMMarketDataServer So I put my google-foo to work and found this solution: struct StringComparerForMap { public: bool