stl

Extending the C++ Standard Library by inheritance?

对着背影说爱祢 提交于 2020-01-08 15:52:25
问题 It is a commonly held belief that the the C++ Standard library is not generally intended to be extended using inheritance. Certainly, I (and others) have criticised people who suggest deriving from classes such as std::vector . However, this question: c++ exceptions, can what() be NULL? made me realise that there is at least one part of the Standard Library that is intended to be so extended - std::exception . So, my question has two parts: Are there any other Standard Library classes which

Extending the C++ Standard Library by inheritance?

帅比萌擦擦* 提交于 2020-01-08 15:51:50
问题 It is a commonly held belief that the the C++ Standard library is not generally intended to be extended using inheritance. Certainly, I (and others) have criticised people who suggest deriving from classes such as std::vector . However, this question: c++ exceptions, can what() be NULL? made me realise that there is at least one part of the Standard Library that is intended to be so extended - std::exception . So, my question has two parts: Are there any other Standard Library classes which

GCC STL bound checking

孤者浪人 提交于 2020-01-08 12:26:48
问题 How do I enable bound checking for operator[] and iterators? 回答1: You can activate runtime iterator and bounds checking by compiling with -D_GLIBCXX_DEBUG . Also note that random-access containers provide the always bounds-checking at() -operation in addition to operator [] . References: GCC STL debug mode: http://gcc.gnu.org/onlinedocs/libstdc++/manual/debug_mode_using.html#debug_mode.using.mode at() operation: std::vector::at(), std::deque::at() and std::array::at() 回答2: you should overload

Is list::size() really O(n)?

给你一囗甜甜゛ 提交于 2020-01-08 11:44:05
问题 Recently, I noticed some people mentioning that std::list::size() has a linear complexity. According to some sources, this is in fact implementation dependent as the standard doesn't say what the complexity has to be. The comment in this blog entry says: Actually, it depends on which STL you are using. Microsoft Visual Studio V6 implements size() as {return (_Size); } whereas gcc (at least in versions 3.3.2 and 4.1.0) do it as { return std::distance(begin(), end()); } The first has constant

一个从硬盘上取空间的STL内存空间分配器

浪尽此生 提交于 2020-01-07 18:39:31
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 最近在读侯捷写的《STL源码剖析》。 看完STL的内存空间分配器这章。在这章里,作者开玩笑的说,你甚至可以写一个直接从硬盘上取空间的配置器。我想,我确实可以写这样的分配器。然后今天就动手写了一个。不过这个分配器只是写着玩玩,不仅效率奇低,还有很多BUG。所以大家可以看着玩玩,可千万别使用啊。 #ifndef __ALLOCATOR_H__ #define __ALLOCATOR_H__ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/mman.h> #include <string.h> #include <new> #include <cstddef> #include <cstdlib> #include <climits> #include <iostream> using namespace std; namespace Costaxu { #define BUFFER_FILE_NAME "/tmp/costaxu_buffer" #define BUFFER_SIZE 1024*1024*1024 template <class T> inline T* _hd_allocate

STL vector 容器介绍

旧街凉风 提交于 2020-01-07 17:44:43
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> A Presentation of the STL Vector Container (By Nitron ) 翻译 masterlee 介绍 std::vector ,并且讨论它在 STL 中的算法和条件函数 remove_if() 。 Download Console Demo - 6.19 Kb Download MFC Demo - 14.6 Kb 介绍 这篇文章的目的是为了介绍 std::vector ,如何恰当地使用它们的成员函数等操作。本文中还讨论了条件函数和函数指针在迭代算法中使用,如在 remove_if() 和 for_each() 中的使用。通过阅读这篇文章读者应该能够有效地使用 vector 容器,而且应该不会再去使用 C 类型的动态数组了。 Vector 总览 vector 是 C++ 标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库。 vector 之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说, vector 是一个能够存放任意类型的动态数组,能够增加和压缩数据。 为了可以使用 vector ,必须在你的头文件中包含下面的代码: # include <vector> vector 属于 std 命名域的

STL vector用法介绍

两盒软妹~` 提交于 2020-01-07 17:17:47
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 介绍 这篇文章的目的是为了介绍 std::vector ,如何恰当地使用它们的成员函数等操作。本文中还讨论了条件函数和函数指针在迭代算法中使用,如在 remove_if() 和 for_each() 中的使用。通过阅读这篇文章读者应该能够有效地使用 vector 容器,而且应该不会再去使用 C 类型的动态数组了。 Vector 总览 vector 是 C++ 标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库。 vector 之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说, vector 是一个能够存放任意类型的动态数组,能够增加和压缩数据。 为了可以使用 vector ,必须在你的头文件中包含下面的代码: # include <vector> vector 属于 std 命名域的,因此需要通过命名限定,如下完成你的代码: using std::vector; vector< int > vInts; 或者连在一起,使用全名: std::vector< int > vInts; 建议使用全局的命名域方式: using namespace std; 在后面的操作中全局的命名域方式会造成一些问题。 vector 容器提供了很多接口,在下面的表中列出

Container of derived objects / smart pointers

对着背影说爱祢 提交于 2020-01-07 08:29:12
问题 Lets say i have: class Base {...}; class Derived1 : public Base {...}; class Derived2 : public Base {...}; class Derived3 : public Base {...}; class Store { public: void Add(const Base&); //Adds mix of Derived1/2/3 private: vector<const Base*> vec; vector<shared_ptr<const Base> > vec_smart; }; //------------------------------------------------------ void Store::Add(const Base& b){ vec.push_back(&b); //got sliced } When using vector of pointers to Base, it got sliced. I suppose i have to use

How to pass user data to compare function of std::sort?

核能气质少年 提交于 2020-01-07 07:27:33
问题 Like qsort() , it seems that C++ std::sort() does not allow to pass user data to the sort function. For example: An array of structure like struct A { int version; int index; } array[100] has to be sorted in order, but using this array struct B { int value; } key[100] as the sort key. struct A::index indexes array key . Here's a non-working sort function. It needs to have a pointer to the key array somehow: bool comp(struct A *a1, struct A *a2) { return key[a1->index].value < key[a2->index]

Are there consequences to writing to an unopened stream?

放肆的年华 提交于 2020-01-07 06:14:09
问题 If I have this: std::ofstream empty; for(int i = 0; i < 99999; ++i) { empty << "Nothing..." << std::endl; } Will this ever cause an out of memory exception or any other problems since the stream is getting data pushed in but it is not going anywhere? Thanks 回答1: When the file stream is default-constructed, the 6 pointers to the internal buffer are initialized to nullptr . Any I/O attempted on the stream will fail because there is no available memory, and ios_base::badbit and ios_base::failbit