stl

STL中的map、set(学完红黑树后的一个小总结)

亡梦爱人 提交于 2020-01-18 03:59:05
树介绍 树是一种很常用的数据结构,在很多地方都能看到树的应用,因为树往往可以用来优化数据查找的效率。 在一些优秀的系统中就会经常用到各种树结构,比如红黑树、AVL树、B树等。 红黑树的应用: 原理: 根节点是黑色的 只有红黑节点 叶节点都是黑色的nil 红色节点子节点必须是黑色的 对每个节点,从该节点到叶子节点路径上的黑节点数目一致,黑平衡性质 以上的五个性质保证了红黑树是平衡的,所以红黑树的查找效率能够稳定在lgn STL模板库中map和set的底层实现: 红黑树中节点和迭代器的实现 所有这些优秀的底层设计都会把结构和数据分离 struct __rb_tree_node_base { typedef __rb_tree_color_type color_type; typedef __rb_tree_node_base* base_ptr; color_type color; // 红黑树的颜色 base_ptr parent; // 父节点 base_ptr left; // 左子节点 base_ptr right; // 右子节点 } template <class Value> struct __rb_tree_node : public __rb_tree_node_base { typedef __rb_tree_node<Value>* link_type;

STL测试2)计算器简单实现

末鹿安然 提交于 2020-01-17 23:30:47
实现一个基本的计算器来计算一个简单的字符串表达式的值。 字符串表达式可以包含左括号 ( ,右括号 ),加号 + ,减号 -,非负整数和空格 。 示例 1: 输入: "1 + 1" 输出: 2 示例 2: 输入: " 2-1 + 2 " 输出: 3 示例 3: 输入: "(1+(4+5+2)-3)+(6+8)" 输出: 23 说明: 你可以假设所给定的表达式都是有效的。 请不要使用内置的库函数 eval。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/basic-calculator 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 题目大概如此,这里主要还是练习测试STL工具,难度不是特别大,但是在看书的时候发现他用了一种新的模式来进行计算 类似于不同状态之间的转换的一个方法对每种情况进行处理 那么先实现最基本的计算功能函数,因为这里只考了+ - 两种可能,没有乘除等因素影响,因此就只用考虑+ -运算而不用考虑乘除乘方等的优先级。 设计的数据结构是两个栈 一个是数据栈 一个是运算符栈,因为在出现括号的情况下要先把当前计算的结果和运算符保存起来,再进行运算,只有在出现括号结束的时候才可以进行最后运算进而保存,因此计算函数的需求就是从数据栈里取栈顶端两个元素和运算符栈顶的元素进行运算即可。 这里实现计算函数。

C++ STL map A1054 The Dominant Color(20)

自闭症网瘾萝莉.ら 提交于 2020-01-17 11:40:06
#include <bits/stdc++.h> #include<math.h> #include <string> using namespace std; const int maxn = 480005;//最大学生人数 map<int,int> temp; int main(){ int m,n; scanf("%d%d",&m,&n); for(int i=0;i<n;++i){ for(int j = 0;j<m;++j){ int t; scanf("%d",&t); //map<int,int>::iterator it; if(temp.find(t) != temp.end()){ temp[t]++; }else{ temp[t] = 1; } } } map<int,int>::iterator it; int max = 0; int result = 0; for(it = temp.begin();it != temp.end();++it){ if(it->second > max){ max = it->second; result = it->first; } } cout<<result<<endl; system("pause"); return 0; } 来源: https://www.cnblogs.com/JasonPeng1/p

Pass std::list to constructor using boost's list_of doesn't compile

梦想的初衷 提交于 2020-01-17 07:36:07
问题 I am trying to do this: class bbb { public: bbb(std::list<int> lst) { } }; int main() { bbb b((std::list<int>)boost::assign::list_of<int>(10)(10)); return 0; } and get following error from g++: some.cc:35: error: call of overloaded 'bbb(boost::assign_detail::generic_list<int>&)' is ambiguous some.cc:15: note: candidates are: bbb::bbb(std::list<int, std::allocator<int> >) some.cc:13: note: bbb::bbb(const bbb&) Is there any way to work around this problem? Note that I am using gcc 4.4.6. It

Min heap with std::priority_queue is my bottleneck

♀尐吖头ヾ 提交于 2020-01-17 05:49:05
问题 Alternative title: Implement min heap with something faster than std::priority_queue . grpof gave me: time seconds seconds calls s/call s/call name 84.12 105.54 105.54 320000 0.00 0.00 _ZN3RKDI24Division_Euclidean_spaceIfEE2nnEjRKSt6vectorIfSaIfEERKfRS3_ISt4pairIfiESaISB_EERiiPjRSt14priority_queueISt5tupleIJfiiEES3_ISJ_SaISJ_EESt7greaterISJ_EES9_RKjS7_S7_i which I believe is my only std::priority_queue used in the project. The 'Division_Euclidean_space' part confuses me, since it's a class

using std::transform the final vector remains empty

送分小仙女□ 提交于 2020-01-16 16:58:45
问题 I am not using std::transform often, however I found it very useful and I am starting replacing some for loops with this algorithm. What is wrong here? I want to keep all the elements of the vector vec that have code > 100. I would expect to have a new std::vector with 3 elements: 133, 144 and 155. But after the algorithm the size is 0. What is wrong? TEST_CASE("testing trasf1", "[tras1]") { std::vector<Test2> vec { {1,1}, {3,3}, {11,11}, {12,12}, {133,133}, {19,19}, {21,21}, {22,22}, {23,23}

Is there a standard library equivalent of timeGetTime()?

落爺英雄遲暮 提交于 2020-01-16 13:16:21
问题 I've tried search without really finding an answer to this question that meets me requirements or explains it clearly enough. I'm looking for a function or a way to implement a function that can retrieve the number of ticks or milliseconds in the same way that the timeGetTime() function does in windows. I'm looking for a solution that only uses standard C++, no additional libraries or platform specifics (like timeGetTime() on windows or a linux equivalent; a multi-platform solution). I'm

UVA - 540-Team Queue解题报告(stl+模拟)

偶尔善良 提交于 2020-01-16 10:12:56
目录 题目概述 思路分析 完整代码 题目概述 链接: https://vjudge.net/problem/UVA-540 有t个团队的人正在排一个长队。每次新来一个人时,如果他有队友在排队,那么这个新人会插队到最后一个队友身后。如果没有任何一个队友排队,则他会排到长队的队尾。输入每个团队中所有队员的编号,要求支持如下3种指令(前两种指令可以穿插进行)。 ENQUEUE:编号为X的人进入长队。 DEQUEUE:长队队首出队。 STOP:停止模拟。 对于每个DEQUEUE指令,输出出队的人的编号。 输入描述: 输入文件中有一组或多组测试数据。 每组测试数据开始有t个团队。下面t行,每行的第一个数字代表这个团队人数,后面是这几个人的编号。编号为0到999999之间的一个整数。 每组测试数据以“STOP”结束。 输入以t==0时结束。 警告:一个测试用例可能包含最多200000(二十万)个命令,所以实现 团队的队列应该是有效的。 输出描述: 对于每组测试数据,先打印一句"Scenario #k",k是第几组数据。对于每一个"DEQUEUE"指令,输出一个出队的人的编号。每组测试数据后要换行,即使是最后一组测试数据。 样例输入: 2 3 101 102 103 3 201 202 203 ENQUEUE 101 ENQUEUE 201 ENQUEUE 102 ENQUEUE 202

Is std::vector::begin() - 1 undefined?

一曲冷凌霜 提交于 2020-01-16 09:09:28
问题 I need to iterate over some elements in backward order and I'm using: for ( /* ... */ it = vec.end() - 1, end = vec.begin() ; it >= end ; --it ) { // ... I now that end() - 1 is defined for some containers, including vector, but now I need to know if begin decrement is also defined. EDIT I don't know if I could use reverse_iterator, because I'll need to pass these iterators as parameters to std::vector::erase and from the documentation, it looks that they are different types. 回答1: Yes, it is

Regex filter for STL messages

对着背影说爱祢 提交于 2020-01-16 05:16:06
问题 Given the follow STL error: ./poly_power/poly_class.cpp:496: error: no matching function for call to ‘state_operator< polynomial< variable_term< polynomial< variable_term< std::basic_string<char, std::char_traits< char>, std::allocator< char> >, int> >, polynomial< variable_term<std::basic_string< char, std::char_traits< char>, std::allocator<char> >, int> > > >, polynomial<variable_term< polynomial< variable_term< std::basic_string< char, std::char_traits< char>, std::allocator< char> >, int