stl

C++ STL 之 deque

≡放荡痞女 提交于 2020-01-09 21:08:35
deque 和 vector 的最大差异? 一在于 deque 允许常数时间内对头端进行元素插入和删除操作。 二在于 deque 没有容量的概念,因为它是动态的以分段的连续空间组合而成,随时可以增加一段新的空间并链接起来,换句话说,像 vector 那样“因旧空间不足而重新分配一块更大的空间,然后再复制元素,释放空间”这样的操作不会发生在 deque 身上,也因此deque 没有必要提供所谓的空间保留功能。 特性总结:  双端插入和删除元素效率较高.  指定位置插入也会导致数据元素移动,降低效率.  可随机存取,效率高. 经验之谈 : deque 是分段连续的内存空间,通过中控器维持一种连续内存空间的状态,其实现复杂性要大于 vector queue stack 等容器,其迭代器的实现也更加复杂,在需要对deque 容器元素进行排序的时候,建议先将 deque 容器中数据数据元素拷贝到 vector 容器中,对 vector 进行排序,然后再将排序完成的数据拷贝回 deque 容器。 1 #include <iostream> 2 #include <deque> 3 using namespace std; 4 5 void PrintDeque(deque<int>& d) 6 { 7 for (deque<int>::iterator it = d.begin();

Safe and effective way to put a mutex on a container entry

五迷三道 提交于 2020-01-09 19:23:10
问题 C++'s std::mutex does not have a move constructor. There is a good reason for that. Basically, move constructors themselves are not generally thread safe, and the whole point of a mutex is that multiple threads will be trying to access it simultaneously. An unfortunate implication of this is that a mutex cannot be placed into a container directly. Containers need the ability to safely move their contents around, and you can't do that with a mutex. The easy way out is to just protect the

std::atomic | compare_exchange_weak vs. compare_exchange_strong

好久不见. 提交于 2020-01-09 12:30:25
问题 I'm unsure if it's me not understanding or the documentation isn't clearly formulated. The following excerpt has been taken from the newest draft (N3126, section 29.6): bool atomic_compare_exchange_weak(volatile A* object, C * expected, C desired); bool atomic_compare_exchange_weak(A* object, C * expected, C desired); bool atomic_compare_exchange_strong(volatile A* object, C * expected, C desired); bool atomic_compare_exchange_strong(A* object, C * expected, C desired); bool atomic_compare

std::atomic | compare_exchange_weak vs. compare_exchange_strong

↘锁芯ラ 提交于 2020-01-09 12:28:51
问题 I'm unsure if it's me not understanding or the documentation isn't clearly formulated. The following excerpt has been taken from the newest draft (N3126, section 29.6): bool atomic_compare_exchange_weak(volatile A* object, C * expected, C desired); bool atomic_compare_exchange_weak(A* object, C * expected, C desired); bool atomic_compare_exchange_strong(volatile A* object, C * expected, C desired); bool atomic_compare_exchange_strong(A* object, C * expected, C desired); bool atomic_compare

std::atomic | compare_exchange_weak vs. compare_exchange_strong

泪湿孤枕 提交于 2020-01-09 12:28:08
问题 I'm unsure if it's me not understanding or the documentation isn't clearly formulated. The following excerpt has been taken from the newest draft (N3126, section 29.6): bool atomic_compare_exchange_weak(volatile A* object, C * expected, C desired); bool atomic_compare_exchange_weak(A* object, C * expected, C desired); bool atomic_compare_exchange_strong(volatile A* object, C * expected, C desired); bool atomic_compare_exchange_strong(A* object, C * expected, C desired); bool atomic_compare

Get a pointer to STL container an iterator is referencing?

南楼画角 提交于 2020-01-09 11:34:08
问题 For example, the following is possible: std::set<int> s; std::set<int>::iterator it = s.begin(); I wonder if the opposite is possible, say, std::set<int>* pSet = it->**getContainer**(); // something like this... 回答1: No, there is no portable way to do this. An iterator may not even have a reference to the container . For example, an implementation could use T* as the iterator type for both std::array<T, N> and std::vector<T> , since both store their elements as arrays. In addition, iterators

C++ deque: when iterators are invalidated

人盡茶涼 提交于 2020-01-09 09:02:29
问题 Please correct me if I am wrong. Thank you! insert and erase will relocate elements, but elements before the position where insertion/erasure takes place don't relocate and hence their iterators remain valid. push_back and pop_back don't invalidate any iterators. push_front and pop_front invalidate all iterators. swap won't relocate elements, but somehow I think it should invalidate iterators. 回答1: push_back() and push_front() are defined in terms of insert() . Similarly, pop_back() and pop

How should a size-limited stl-like container be implemented?

隐身守侯 提交于 2020-01-09 08:13:09
问题 While refactoring, I wanted to change an array where entries are added to an std::vector, but for compatibility (persistency, downgrading,...), it still needs to have an upper limit. What is the best way (elegant, stl-like, limited extra code) to have an stl-like container which is limited in size, so you know that inserting an entry fails? Edit : To clarify: I would like an stl-like container, that starts empty, that you can fill with entries and possibly remove entries and that iterate over

How to cheaply assign C-style array to std::vector?

*爱你&永不变心* 提交于 2020-01-09 05:04:26
问题 Currently I do the following: // float *c_array = new float[1024]; void Foo::foo(float *c_array, size_t c_array_size) { //std::vector<float> cpp_array; cpp_array.assign(c_array, c_array + c_array_size); delete [] c_array; } How can I optimize this assigning? I would like not to perform elementwise copy but just swap pointers. 回答1: The current std::vector doesn't provide any capability or interface to take ownership of previously allocated storage. Presumably it would be too easy to pass a

无监督STL-10

人盡茶涼 提交于 2020-01-08 23:00:13
STL-10 input.py 无监督学习数据集的入口代码 STL-10 IIC,tensorflow处理 IIC,tensorflow 来源: CSDN 作者: Drama King 链接: https://blog.csdn.net/cxkshizhenshuai/article/details/103898187