stl-algorithm

The fastest way to find union of sets

让人想犯罪 __ 提交于 2019-12-01 03:14:06
I have sets of pairs of int like set<pair<int,int> > x1, x2, ... xn ( n can be between 2 and 20). What is the fastest way to find union of those sets ? Sorry If I wasn't make clear at the beginning, I meant fast in performance, memory allocation is not a problem. Unfortunately, I believe that you are limited to a linear O(N) solution, as all a union would be is a combination of the elements in both sets. template<typename S> S union_sets(const S& s1, const S& s2) { S result = s1; result.insert(s2.cbegin(), s2.cend()); return result; } Assuming that the result needs to be a set too, then you

set_union with multiset containers?

感情迁移 提交于 2019-12-01 03:04:08
问题 What's the return of the algorithm std:set_union when one or both input containers are multisets with duplicated objects? Do dups get lost? Let's suppose for example: multiset<int> ms1; ms1.insert(1); ms1.insert(1); ms1.insert(1); ms1.insert(2); ms1.insert(3); multiset<int> ms2; ms2.insert(1); ms2.insert(1); ms2.insert(2); ms2.insert(2); ms2.insert(4); vector<int> v(10); set_union( ms1.begin(), ms1.end(), ms2.begin(), ms2.end(), v.begin() ); What would the output be? 回答1: From the standard,

The fastest way to find union of sets

别等时光非礼了梦想. 提交于 2019-11-30 22:41:24
问题 I have sets of pairs of int like set<pair<int,int> > x1, x2, ... xn ( n can be between 2 and 20). What is the fastest way to find union of those sets ? Sorry If I wasn't make clear at the beginning, I meant fast in performance, memory allocation is not a problem. 回答1: Unfortunately, I believe that you are limited to a linear O(N) solution, as all a union would be is a combination of the elements in both sets. template<typename S> S union_sets(const S& s1, const S& s2) { S result = s1; result

Are std::fill, std::copy specialized for std::vector<bool>?

喜夏-厌秋 提交于 2019-11-30 12:24:22
When thinking about this question I start to wondering if std::copy() and/or std::fill are specialized (I really mean optimized) for std::vector<bool> . Is this required by C++ standard or, perhaps, it is common approach by C++ std library vendors? Simple speaking, I wonder to know if the following code: std::vector<bool> v(10, false); std::fill(v.begin(), v.end(), true); is in any way better/different than that: std::vector<bool> v(10, false); for (auto it = v.begin(); it != v.end(); ++it) *it = true; To be very strict - can, let say: std::fill<std::vector<bool>::iterator>() go into internal

How to select a random element in std::set in less than O(n) time?

大城市里の小女人 提交于 2019-11-30 11:41:48
This question with an added constraint. I'm willing to allow not-uniform selection as long as it's not to lop sided. Given that " sets are typically implemented as binary search trees " and I expect they will contain some kind of depth or size information for balancing, I would expect you could do some sort of weighted random walk of the tree. However I don't know of any remotely portable way to do that. Edit: The constraint is NOT for the amortized time. Introduce array with size equal to set. Make array elements hold addresses of every element in set. Generate random integer R bounded by

What does std::includes actually do?

旧街凉风 提交于 2019-11-30 08:07:48
From the standard, std::includes : Returns: true if [first2, last2) is empty or if every element in the range [first2, last2) is contained in the range [first1, last1) . Returns false otherwise. Note: as this is under [alg.set.operations] , the ranges must be sorted Taking this literally, if we let R1=[first1, last1) and R2=[first2, last2) , this is evaluating: ∀a∈R2 a∈R1 However, this is not what is actually being evaluated. For R1={1} and R2={1,1,1} , std::includes(R1, R2) returns false: #include <algorithm> #include <iomanip> #include <iostream> #include <vector> int main() { std::vector

How to select a random element in std::set in less than O(n) time?

拈花ヽ惹草 提交于 2019-11-29 17:06:54
问题 This question with an added constraint. I'm willing to allow not-uniform selection as long as it's not to lop sided. Given that "sets are typically implemented as binary search trees" and I expect they will contain some kind of depth or size information for balancing, I would expect you could do some sort of weighted random walk of the tree. However I don't know of any remotely portable way to do that. Edit: The constraint is NOT for the amortized time. 回答1: Introduce array with size equal to

What does std::includes actually do?

℡╲_俬逩灬. 提交于 2019-11-29 10:43:30
问题 From the standard, std::includes : Returns: true if [first2, last2) is empty or if every element in the range [first2, last2) is contained in the range [first1, last1) . Returns false otherwise. Note: as this is under [alg.set.operations] , the ranges must be sorted Taking this literally, if we let R1=[first1, last1) and R2=[first2, last2) , this is evaluating: ∀a∈R2 a∈R1 However, this is not what is actually being evaluated. For R1={1} and R2={1,1,1} , std::includes(R1, R2) returns false:

Wrong results when appending vector to itself using copy and back_inserter [duplicate]

£可爱£侵袭症+ 提交于 2019-11-29 09:29:53
This question already has an answer here: Nice way to append a vector to itself 4 answers Inspired by this question , asking how to append a vector to itself, my first thought was the following (and yes, I realize insert is a better option now): #include <algorithm> #include <iostream> #include <iterator> #include <vector> int main() { std::vector<int> vec {1, 2, 3}; std::copy (std::begin (vec), std::end (vec), std::back_inserter (vec)); for (const auto &v : vec) std::cout << v << ' '; } However, this prints: 1 2 3 1 * 3 The * is a different number every time the program is run. The fact that

Using emplace with algorithms such as std::fill

天大地大妈咪最大 提交于 2019-11-29 07:57:59
问题 I have used vector::emplace_back in order to avoid constructing temporal objects while filling a vector. Here you have a simplified version: class Foo { public: Foo(int i, double d) : i_(i), d_(d) {} /* ... */ }; std::vector<Foo> v; v.reserve(10); for (int i = 0; i < 10; i++) v.emplace_back(1, 1.0); But I wanted to use std::fill_n instead: v.reserve(10); std::fill_n(std::back_inserter(v), 10, Foo(1, 1.0)); In this way, temporal copies will be created, though. I do not know how to use emplace