stl

How to produce a random number sequence that doesn't produce more than X consecutive elements

限于喜欢 提交于 2019-12-22 05:18:09
问题 Ok, I really don't know how to frame the question properly because I barely have any idea how to describe what I want in one sentence and I apologize. Let me get straight to the point and you can just skip the rest cause I just want to show that I've tried something and not coming here to ask a question on a whim. I need an algorithm that produces 6 random numbers where it may not produce more than 2 consecutive numbers in that sequence. example: 3 3 4 4 2 1 ^FINE. example: 3 3 3 4 4 2 ^ NO!

C++ STL总结

谁说我不能喝 提交于 2019-12-22 05:14:13
STL概述 STL (Standard Template Library, 标准模板库) 是惠普实验室开发的一系列软件的统称。主要核心分为三大部分:容器(container)、算法(algorithm)和迭代器(iterator),另外还有容器适配器(container adaptor)和函数对象(functor)等其它标准组件。 容器: 顺序容器: 名称 特性 vector 模拟的数据结构式动态数组,在内存中是连续储存的,支持随机存取,支持在 尾部 快速插入和删除元素,搜索速度较慢 deque 称为双端队列,在内存中的储存方式是小片连续,每片之间用链表连接起来,支持随机存取,支持在 头部和尾部 快速插入和删除元素,搜索速度较慢 list 称为双向链表,在内存中的储存是不连续的,每个元素的内存之间用指针相连, 不支持 随机存取(因为要从首或尾遍历至指定位置),但是支持在 任意位置 快速插入和删除元素,搜索速度 最慢 ,扩展内存时无需复制和拷贝原元素 array 称为静态数组,在内存中是连续储存的,支持随机存取,不支持插入或删除元素 forward_list 称为前向链表,在内存中的储存是不连续的,同list一样支持在任意位置快速插入和删除元素,不支持随机存取,搜索速度也较慢,与list最大的区别在于其只能从头部遍历至尾部,不能反向遍历,因此没有保存后向指针,比list更省内存

Does an allocator.construct loop equal std::uninitialized_copy?

谁都会走 提交于 2019-12-22 05:13:47
问题 In this context T is a certain type and allocator is an allocator object for that type. By default it is std::allocator<T> but this is not necessarily true. I have a chunk of memory acquired by allocator.allocate(n) . I also have a container con of T objects (say, a std::vector<T> ). I want to initialize that chunk of memory with the T object(s). The location of the chunk of memory is stored in T* data . Are these two code examples always identical? #include <memory> // example 1 std:

set_difference and set_intersection simultaneously

ぃ、小莉子 提交于 2019-12-22 04:57:13
问题 I'm wondering if there is any facility in the standard library to simultaneously compute the set intersection and set difference between two sorted ranges. Something with a signature along the lines of: template <class Input1, class Input2, class Output1, class Output2, class Output3> Output3 decompose_sets (Input1 first1, Input1 last1, Input2 first2, Input2 last2, Output1 result1, Output2 result2, Output3 result3 ); Such that after a call to decompose sets , result1 contains all the elements

Memory Leaks - STL sets

一世执手 提交于 2019-12-22 04:39:09
问题 I am trying to plug up all my memory leaks (which is massive). I am new to STL. I have a class library where I have 3 sets. I am also creating a lot of memory with new in the library class for adding info to the sets... Do I need to deallocate the sets? If so, how? Here is the library.h #pragma once #include <ostream> #include <map> #include <set> #include <string> #include "Item.h" using namespace std; typedef set<Item*> ItemSet; typedef map<string,Item*> ItemMap; typedef map<string,ItemSet*

Processing files larger than 2 GB in C++ with STL

混江龙づ霸主 提交于 2019-12-22 04:37:38
问题 I am doing binary file processing and in my algorithm I would like to know the actual type of pos_type and off_type , for example when computing the size of the file or seeking to a given position ( tellg and seekg ). When computing the size of the file I just static_cast the pos_type to an int64_t and it seems to work fine. How about seekg ? Is it safe to pass an int64_t to it? Is there a way to make pos_type and off_type to be an int64_t , perhaps using traits ? I would like to eliminate

Can I prevent std::sort from copying the passed comparison object

你离开我真会死。 提交于 2019-12-22 04:14:31
问题 We're using a comparator object to sort a vector: std::vector<Data> v = .... Comparator c = .... std::sort(v.begin(), v,end(), c); However, this makes copies of c during the sorting, and is causing performance problems, because Comparator objects store a big map (in which lookups are done when calling the comparison function). I thought I could force the use of references with: const Comparator &ref = c; std::sort(v.begin(), v.end(), ref); but copies still happen with this. Is there a way to

Why prefer std::vector over std::deque? [duplicate]

大憨熊 提交于 2019-12-22 04:07:20
问题 This question already has answers here : Why would I prefer using vector to deque (9 answers) Closed 9 months ago . They both have access complexity of O(1) and random insertion/removal complexity of O(n). But vector costs more when expanding because of reallocation and copy, while deque does not have this issue. It seems deque has a better performance, but why most people use vector instead of deque? 回答1: why most people use vector instead of deque? Because this is what they have been taught

Why prefer std::vector over std::deque? [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 04:07:13
问题 This question already has answers here : Why would I prefer using vector to deque (9 answers) Closed 9 months ago . They both have access complexity of O(1) and random insertion/removal complexity of O(n). But vector costs more when expanding because of reallocation and copy, while deque does not have this issue. It seems deque has a better performance, but why most people use vector instead of deque? 回答1: why most people use vector instead of deque? Because this is what they have been taught

What is the performance of STL bitset::count() method?

懵懂的女人 提交于 2019-12-22 04:06:17
问题 I searched around and could not find the performance time specifications for bitset::count(). Does anybody know what it is (O(n) or better) and where to find it? EDIT By STL I refer only to the Standard Template Library. 回答1: I read this file (C:\cygwin\lib\gcc\i686-pc-cygwin\3.4.4\include\c++\bitset) on my computer. See these /// Returns the number of bits which are set. size_t count() const { return this->_M_do_count(); } size_t _M_do_count() const { size_t __result = 0; for (size_t __i = 0