stl

Why can I not pass this comparison function as a template argument?

允我心安 提交于 2020-06-02 10:50:35
问题 I am trying to create an std::set with a function I defined for sorting, but I get the error: "Error: function "GFX::MeshCompare" is not a type name" Mesh.h namespace GFX { struct Mesh { [...] }; inline bool MeshCompare(const Mesh& a, const Mesh& b) { return ( (a.pTech < b.pTech) || ( (b.pTech == a.pTech) && (a.pMaterial < b.pMaterial) ) || ( (b.pTech == a.pTech) && (a.pMaterial == b.pMaterial) && (a.topology < b.topology) ) ); } }; Renderer.h namespace GFX { class Renderer { private: [...]

Why can I not pass this comparison function as a template argument?

大兔子大兔子 提交于 2020-06-02 10:46:11
问题 I am trying to create an std::set with a function I defined for sorting, but I get the error: "Error: function "GFX::MeshCompare" is not a type name" Mesh.h namespace GFX { struct Mesh { [...] }; inline bool MeshCompare(const Mesh& a, const Mesh& b) { return ( (a.pTech < b.pTech) || ( (b.pTech == a.pTech) && (a.pMaterial < b.pMaterial) ) || ( (b.pTech == a.pTech) && (a.pMaterial == b.pMaterial) && (a.topology < b.topology) ) ); } }; Renderer.h namespace GFX { class Renderer { private: [...]

Getting index of an element in a std::queue by its value

两盒软妹~` 提交于 2020-05-28 15:23:42
问题 Is there a simple way to get the position of an element in a std::queue by its value in C++? For example: std::queue<int> numbers; numbers.push(7); numners.push(4); numbers.push(11); int position = numbers.getPosition(4); //should be 1 回答1: If you want to get the index of an element you should probably consider using an std::deque container instead of a std::queue container adapter , as already suggested in this other answer. If you still want to stick to to the std::queue container adapter

How does the compare function in std::map in C++ work if it is reflexively true?

ⅰ亾dé卋堺 提交于 2020-05-25 08:27:28
问题 I have a map in my project. Every time I insert a new element, I want to ensure that the key of the new element I insert is at least a minimum width apart from other elements in the map. To do this I wrote a custom compare class like this: class PulseCompare { public: PulseCompare(int minwidth_):minwidth(minwidth_){}; bool operator()(const int x, const int y) const { if(abs(x-y)>minwidth) return false; else return true; } private: int minwidth; }; and created the map like this: std::map<int

Use bind1st or bind2nd?

一曲冷凌霜 提交于 2020-05-11 09:05:09
问题 vector<int> vwInts; vector<int> vwIntsB; for(int i=0; i<10; i++) vwInts.push_back(i); transform(vwInts.begin(), vwInts.end(), inserter(vwIntsB, vwIntsB.begin()), bind1st(plus<int>(), 5)); // method one transform(vwInts.begin(), vwInts.end(), inserter(vwIntsB, vwIntsB.begin()), bind2nd(plus<int>(), 5)); // method two I know the usage difference between bind1st and bind2nd and both method one and method two provide the expected results for me. Is it true that there is no big difference in this

Why did std::allocator lose member types/functions in C++17?

﹥>﹥吖頭↗ 提交于 2020-05-11 03:49:06
问题 While looking at std::allocator, I see that members: value_type , pointer , const_pointer , reference , const_reference , size_type , difference_type , and rebind have all been deprecated. Allocators will also no longer have the members: address , max_size , construct , or destroy . Why did this happen? Did it have something to do with polymophic allocators? 回答1: If you look at the relevant isocpp paper you can see that the first set you mention is now thought to be better placed in std:

Efficient linked list in C++?

北城余情 提交于 2020-05-09 19:07:11
问题 This document says std::list is inefficient: std::list is an extremely inefficient class that is rarely useful. It performs a heap allocation for every element inserted into it, thus having an extremely high constant factor, particularly for small data types. Comment: that is to my surprise. std::list is a doubly linked list, so despite its inefficiency in element construction, it supports insert/delete in O(1) time complexity, but this feature is completely ignored in this quoted paragraph.

is_integral vs is_integer: is one of them redundant?

♀尐吖头ヾ 提交于 2020-04-12 07:45:08
问题 is_integral and is_integer seem to answer the same thing in the same way . From the links to the related documentation pages, is_integral seems to be missing specializations for the following types signed char unsigned char unsigned short unsigned int unsigned long unsigned long long Yet a compiled example, shows (of course) their identical behaviour on those types as well: #include <iostream> #include <type_traits> using namespace std; int main() { cout << is_integral<signed char >::value <<

STL简介(Introduction to the Standard Template Library)

邮差的信 提交于 2020-04-08 00:56:54
昨天晚上花了一个晚上,翻译了一篇关于STL的文章。呵呵,第一次翻译这种东西,感觉计算机书籍还是英文原版的比较好,因为很多概念没法用中文恰当的表示(简直就是只可意会不可言传啊,:-))。由于第一次翻译,呵呵,水平肯定比较菜了,关键是对STL以前没看过,比较陌生,所以翻译得很辛苦。还是看英文原版的好,翻译的太辛苦了。 STL简介(Introduction to the Standard Template Library) gshine 译 STL(标准模板库,Standard Template Library),是一个包含容器(container)类,算法(algorithm)和迭代器(iterators)的C++类库。它提供了许多计算机科学方面的基本算法和数据结构。STL是一个泛型(generic)库,这意味着它的各个组件(components)都已经最大程度的参数化了,基本上STL里面的所有组件都是一个模板(template)。所以,在你使用STL之前,必须保证你已经理解了C++中模板(template)是如何工作的。 容器和算法(container and algorithm) 和大多数类库一样,STL也包含容器类—它的主要目的是容纳其他的对象(通常是多个)。这些容器类包括以下几个类(classes):vector,list,deque,set,multiset,map

C++ STL,list vector区别

拟墨画扇 提交于 2020-04-08 00:56:40
顺序性容器: 向量 vector : 是一个线性顺序结构。相当于数组,但其大小可以不预先指定,并且自动扩展。它可以像数组一样被操作,由于它的特性我们完全可以将vector 看作动态数组。 在创建一个vector 后,它会自动在内存中分配一块连续的内存空间进行数据存储,初始的空间大小可以预先指定也可以由vector 默认指定,这个大小即capacity ()函数的返回值。当存储的数据超过分配的空间时vector 会重新分配一块内存块,但这样的分配是很耗时的,在重新分配空间时它会做这样的动作: 首先,vector 会申请一块更大的内存块; 然后,将原来的数据拷贝到新的内存块中; 其次,销毁掉原内存块中的对象(调用对象的析构函数); 最后,将原来的内存空间释放掉。 如果vector 保存的数据量很大时,这样的操作一定会导致糟糕的性能(这也是vector 被设计成比较容易拷贝的值类型的原因)。所以说vector 不是在什么情况下性能都好,只有在预先知道它大小的情况下vector 的性能才是最优的。 vector 的特点: (1) 指定一块如同数组一样的连续存储,但空间可以动态扩展。即它可以像数组一样操作,并且可以进行动态操作。通常体现在push_back() pop_back() 。 (2) 随机访问方便,它像数组一样被访问,即支持[ ] 操作符和vector.at() (3) 节省空间