stl

STL——Number

房东的猫 提交于 2020-01-24 05:44:58
STL——Number 题目描述 We define Shuaishuai-Number as a number which is the sum of a prime square(平方), prime cube(立方), and prime fourth power(四次方). The first four Shuaishuai numbers are: How many Shuaishuai numbers in [1,n]? (1<=n<=50 000 000) 输入描述: The input will consist of a integer n. 输出描述: You should output how many Shuaishuai numbers in [1…n] 示例 输入 28 输出 1 说明 There is only one Shuaishuai number. 方法一: 仅使用vector # include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <algorithm> # include <vector> # include <set> # include <cmath> using namespace std ; vector <

【c++】STL之set用法总结

こ雲淡風輕ζ 提交于 2020-01-24 02:11:26
介绍 set和multiset会根据特定准则将元素自动排序(从小到大)。两者的不同之处在于multiset允许元素重复而set不允许。 所以不能直接改变元素值,因为这样会打乱原本正确的顺序。因此要改变元素值必须先删除旧元素,再插入新元素。 C++ STL中标准关联容器set, multiset, map, multimap内部采用的就是一种非常高效的平衡检索二叉树:红黑树,也成为RB树(Red-Black Tree)。RB树的统计性能要好于一般平衡二叉树,所以被STL选择作为了关联容器的内部结构。 用法 1 头文件 # include <set> 2 非更易型操作,用来查询大小互相比较 c.key_comp() c.value_comp() c.empty()——返回容器是否为空 c.size()——返回目前元素的个数 c.max_size()——返回元素个数之最大可能量 c1==c2 c1!=c2 … 3 查找 c.count(val)——返回元素值为val的元素个数,有些就没有count函数,比如vector之类的容器没有count c.find(val)——返回元素值为val的第一个元素,如果找不到就返回end() 4.迭代器 begin(); // 返回指向第一个元素的迭代器 end(); // 返回指向最后一个元素的迭代器 rbegin()

STL学习之mismatch();

穿精又带淫゛_ 提交于 2020-01-23 23:52:28
std::mismatch 定义于头文件 <algorithm> 一、定义: (共八种定义方式,一开始先了解两种即可) 1. template < class InputIt1, class InputIt2 > constexpr std:: pair <InputIt1,InputIt2 > mismatch ( InputIt1 first1, InputIt1 last1, InputIt2 first2 ) ;//constexpr关键字在很早就出来了,但是直到C++20才开始在STL中使用,所有声明之前都有 2. template < class InputIt1, class InputIt2 > constexpr std:: pair <InputIt1,InputIt2 > mismatch ( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2 ) ; 这两者有什么差别呢? 首先我们要知道正常情况下,这个函数是找到两个容器里面的值不匹配的迭代器并且返回,返回的是 指向二个不相等元素的迭代器的 std::pair,对,是两个迭代器,分别指向前后两个容器。 当没有InputIt2 last2 返回来自二个范围:一个以 [first1, last1) 定义而另一个以 [first2

Does std::vector.push_back(std::move(foo)) make sense?

谁都会走 提交于 2020-01-23 08:27:53
问题 I have come across this in some code (details eliminated for clarity): std::vector<std::vector<int>> foo; { std::vector<int> bar = {42}; foo.push_back(std::move(bar)); // Hmmm... } // Indicate `bar` is no longer needed. The std::move looks unnecessary to me, but is it? Is the behaviour any different from just foo.push_back(bar); ? What if instead of an int the element is a class such as pcl::PointXYZ as it is in my actual code? UPDATE : I have altered the code to more explicitly indicate that

Does std::vector.push_back(std::move(foo)) make sense?

柔情痞子 提交于 2020-01-23 08:27:24
问题 I have come across this in some code (details eliminated for clarity): std::vector<std::vector<int>> foo; { std::vector<int> bar = {42}; foo.push_back(std::move(bar)); // Hmmm... } // Indicate `bar` is no longer needed. The std::move looks unnecessary to me, but is it? Is the behaviour any different from just foo.push_back(bar); ? What if instead of an int the element is a class such as pcl::PointXYZ as it is in my actual code? UPDATE : I have altered the code to more explicitly indicate that

Smart pointers as class members for polymorphism

三世轮回 提交于 2020-01-23 08:17:10
问题 I'm new to smart pointers and I would be really grateful if somebody could give me a hint whether the way I'm handling smart pointers as class members is correct. More precisely, the solution that I would like to achieve is in the context of class polymorphism and should be ideally exception-safe. Given a container of heterogeneuous objects ( std::vector<shared_ptr<CBase> > my_vector ), the usual way to add elements is: my_vector.push_back( shared_ptr<CBase>(new CChild(1))) , so that later on

Smart pointers as class members for polymorphism

怎甘沉沦 提交于 2020-01-23 08:17:06
问题 I'm new to smart pointers and I would be really grateful if somebody could give me a hint whether the way I'm handling smart pointers as class members is correct. More precisely, the solution that I would like to achieve is in the context of class polymorphism and should be ideally exception-safe. Given a container of heterogeneuous objects ( std::vector<shared_ptr<CBase> > my_vector ), the usual way to add elements is: my_vector.push_back( shared_ptr<CBase>(new CChild(1))) , so that later on

STL(标准模板库)

旧时模样 提交于 2020-01-23 07:42:01
(此篇只是一个目录,将分成单篇去完成) STL 主要有三个部分组成: 容器, 迭代器, 算法。 第一部分:知其然 一,容器 * 顺序容器 向量(Vector) 双端队列(Dequeue) 表(List) PS: Copy 方法 * 关联容器 集合(Set) 多重集合(Multiset) 映射(Map) 多重映射(Multimap) * 容器适配器 栈(Stack) 队列(Queue) 优先队列(Priority Queue) 二, 迭代器 输入迭代器 输出迭代器 前向迭代器 双向迭代器 随机访问迭代器 PS:迭代器声明式及流迭代器 三, 算法 非修改算法 修改算法 数字算法 堆算法 PS: 函数对象 插入迭代器 第二部分:知其所以然 来源: https://www.cnblogs.com/wjchang/p/3671664.html

Are std::find and std::map.find both O(logN)?

空扰寡人 提交于 2020-01-23 05:55:22
问题 Are std::find and std::map.find both O(logN)? If they are, how does std::find implement search over std::map in logarithmic time? Is the implementation of std::find specialized for the std::map use case? 回答1: No, std::find is O(N), regardless of the container. It doesn't know the "container", there is no specialization for std::map . std::find uses only the iterators, which do not have information about the underlying container. According to cppreference.com, the implementation is equivalent

侯捷 STL

泄露秘密 提交于 2020-01-22 16:42:24
侯捷 STL 源码之前 了无秘密 1.1 headers、版本、重要资源 C++ Standard Library(标准库) vs. Standard Template Library(标准模板库) 2.2 STL 体系结构基础 六大部件: 容器(Container) 分配器(Allocators) 算法(Algorithms) 迭代器(Iterators) 适配器(Adapters) 仿函数(Functors) #include<vector> #include<algorithm> #include<functional> #include<iostream> using namespace std; int main() { int ia[6] = {27, 210, 12, 47, 109, 83}; vector< int , allocator<int> > vi(ia,ia + 6); cout << count_if(vi.begin(), vi.end(), not1(bind2nd( less<int>(), 40 ))); return 0; } 复杂度,Complexity,Big-Oh “前闭后开”区间 range-base for statement and auto (C++11) 3.3 容器 - 结构与分类(一) 大致上分为两种,一个叫做序列式