stl

Can raw pointers be used instead of iterators with STL algorithms for containers with linear storage?

痞子三分冷 提交于 2019-12-28 02:12:07
问题 I have a custom vector container that internally stores item a linear array. Last night, I was trying to implement custom iterators for my class to be able to use them with STL algorithms. I have had some success that you can see in here: Live example with custom iterators While doing so, I discovered I can merely pass raw pointers to STL algorithm and they just seem to work fine. Here's the example without any iterators: #include <cstddef> #include <iostream> #include <iterator> #include

Order Statistic Tree in C++

可紊 提交于 2019-12-28 02:09:08
问题 I need an order statistic tree for standard GCC STL map containers. I checked and there is something known as PBDS. Policy based data structures. That usage is also not clear to me. Anyone can tell me how to use STL map containers for order statistic tree? Even if its only on GNU G++ its enough? 回答1: Here is the example of GNU Policy-Based STL MAP implemented as order statistic tree (tested on Linux gcc 4.6.1): #include <iostream> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds

STL containers element destruction order

笑着哭i 提交于 2019-12-28 02:09:04
问题 Does ISO C++ standard mandate any sort of destruction order of objects inside STL containers? Are std::list / std::vector / std::map elements destroyed starting from the beginning or the end of the container? Can I rely on std::map storing its elements in std::pair s internally so a key in a pair is destroyed before its value (or vice versa)? 回答1: Unspecified in the standard. Yes, but this means that the key is destroyed after its associated value. 回答2: Unspecified Yes, you can depend on std:

Efficiency of the STL priority_queue

旧街凉风 提交于 2019-12-28 02:03:52
问题 I have an application (C++) that I think would be well served by an STL priority_queue . The documentation says: Priority_queue is a container adaptor, meaning that it is implemented on top of some underlying container type. By default that underlying type is vector, but a different type may be selected explicitly. and Priority queues are a standard concept, and can be implemented in many different ways; this implementation uses heaps. I had previously assumed that top() is O(1) , and that

A proper way to create a matrix in c++

余生颓废 提交于 2019-12-27 23:41:27
问题 I want to create an adjacency matrix for a graph. Since I read it is not safe to use arrays of the form matrix[x][y] because they don't check for range, I decided to use the vector template class of the stl. All I need to store in the matrix are boolean values. So my question is, if using std::vector<std::vector<bool>* >* produces too much overhead or if there is a more simple way for a matrix and how I can properly initialize it. EDIT: Thanks a lot for the quick answers. I just realized,

C++11 STL containers and thread safety

和自甴很熟 提交于 2019-12-27 19:10:39
问题 I have trouble finding any up-to-date information on this. Do C++11 versions of STL containers have some level of thread safety guaranteed? I do expect that they don't, due to performance reasons. But then again, that's why we have both std::vector::operator[] and std::vector::at. 回答1: Since the existing answers don't cover it (only a comment does), I'll just mention 23.2.2 [container.requirements.dataraces] of the current C++ standard specification which says: implementations are required to

C++11 STL containers and thread safety

蓝咒 提交于 2019-12-27 19:10:25
问题 I have trouble finding any up-to-date information on this. Do C++11 versions of STL containers have some level of thread safety guaranteed? I do expect that they don't, due to performance reasons. But then again, that's why we have both std::vector::operator[] and std::vector::at. 回答1: Since the existing answers don't cover it (only a comment does), I'll just mention 23.2.2 [container.requirements.dataraces] of the current C++ standard specification which says: implementations are required to

【STL记录】Containers--Vectors

让人想犯罪 __ 提交于 2019-12-27 18:26:11
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Vector类似于一个动态数组 使用vector,需加入头文件: #include <vector> 一、Abilities of Vectors Vector复制所有元素到它的内部动态数组中。通常这些元素是有一定顺序的,因此vector是一种有序集合。 Vector提供随机访问,如果知道元素的位置,可以直接访问元素。 当在尾部插入和删除元素时,vector表现很好;但在中间或前端插入、删除元素时,则较慢。 1.Size and Capacity Vector除了提供常用的操作size(),empty(),和max_size(),还提供了一个capacity()函数,用来返回一个vector在它的实际内存中所包含的元素个数。一旦超出了这个capacity,vector将从新分配它的内部内存。 capacity的重要性有两个方面: 再分配(reallocation)使得vector的引用,指针和iterators都失效 再分配(reallocation)需要花费一定时间 为了避免再分配(reallocation),可以使用reserve()函数来确保capacity: vector<int> v; //create an empty vector v.reserve(80); //reserve memory

【STL记录】Containers--Lists

蹲街弑〆低调 提交于 2019-12-27 18:25:57
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> list以双链表的形式管理元素: 使用list时,需要加上头文件: #include <list> list与array,vector,deque的不同之处: list不提供随机访问,比如要访问第五个元素,你必须从第一个元素开始通过链指针到第五个元素。因此,访问任意元素时较慢。 在任意位置插入和删除元素很快,并且时间相同。 list支持的异常处理:每一次操作成功或是一个no-op. Special Modifying Operations for List Operation Effect c.unique() 移除相同元素值的元素,使唯一 c.unique(op) 当op()为true时移除相同元素,使唯一 c.splice(pos, c2) 将c2的全部元素移动到c的pos位置前 c.splice(pos, c2, c2pos) 将c2的c2pos位置的元素移动到c的pos位置前 c.splice(pos, c2, c2beg, c2end) 将c2的[c2beg, c2end)元素移动到c的pos位置前 c.sort() 以<排序所有元素 c.sort(op) 以op()排序所有操作 c.merge(c2) 假定两container中的元素都是已排序的,移动c2的元素到c中,并保持排序 c.merge

【STL记录】Containers--Arrays

杀马特。学长 韩版系。学妹 提交于 2019-12-27 18:25:45
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Array是container class array<>的一个实例,是一个固定长度的元素序列。 在使用array之前,需要include头文件: #include <array> 一、初始化 // all elements of x have value 0 (int()) array<int,4> x = {}; //use an initializer list to initialize array array<int,5> coll = { 42, 377, 611, 21, 44 }; // one element with value 42, followed by 9 elements with value 0 array<int,10> c2 = { 42 }; 二、Array Operations 1.Class array<> 的构造函数 Table 1. Constructors of Class array<> Operation Effect array<Elem, N> c 默认构造函数:使用默认初始化元素创建一个array array<Elem, N> c(c2) 复制构造函数:通过复制创建一个相同的array(所有元素都被复制) array<Elem, N> c = c2