stl

Bitset Reference

匆匆过客 提交于 2020-01-13 10:33:32
问题 From http://www.cplusplus.com/reference/stl/bitset/: Because no such small elemental type exists in most C++ environments, the individual elements are accessed as special references which mimic bool elements. How, exactly, does this bit reference work? The only way I could think of would be to use a static array of char s, but then each instance would need to store its index in the array. Since each reference instance would have at least the size of a size_t , that would destroy the

How to use struct as key in std::map

点点圈 提交于 2020-01-13 09:53:50
问题 I want to use a std::map whose key and value elements are structures. I get the following error: error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const GUID I understand that I should overload operator < for that case, but the thing is I don't have access to the code of the structure I want to use ( GUID structure in VC++). Here's the code snippet:

How to use struct as key in std::map

…衆ロ難τιáo~ 提交于 2020-01-13 09:52:10
问题 I want to use a std::map whose key and value elements are structures. I get the following error: error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const GUID I understand that I should overload operator < for that case, but the thing is I don't have access to the code of the structure I want to use ( GUID structure in VC++). Here's the code snippet:

Container Adapters do not support iterators

感情迁移 提交于 2020-01-13 09:41:54
问题 In one of the C++ articles on STL, is being said that - Since container adapters do not support iterators, hence they cannot be used with the STL algorithms. But it did not explain as to why Container Adapters do not support iterators? Can anybody explain me the same? 回答1: What's the point of a stack or a queue having an iterator? A stack is by definition something that you can only push and pop into... An iterator would destroy the whole purpose of these adapters 回答2: I would note that this

Self-contained, STL-compatible implementation of std::vector

你离开我真会死。 提交于 2020-01-13 09:18:08
问题 The implementation of std::vector that ships with Visual Studio 2010 and earlier versions has a well known particularity: the resize method has the following signature (C++03-compliant): void resize(size_type new_size, value_type value); instead of the C++11-compliant signature that's been used by the majority of other STL implementations (like gcc's STL or STLport) long before C++11: void resize(size_type new_size, const value_type& value); The problem with the first variant is that, in some

Type requirements for std::map

Deadly 提交于 2020-01-13 07:58:22
问题 Today I created a map, where the value type has no default constructor. I was surprised that I could not use operator[] to insert the elements to this map, but I had to use the insert method. So, what exactly are requirements for the key and value types for std::map? Here is short example : #include <map> struct A { A(int){} }; int main() { std::map< int, A > m; A a1(2); A a2(3); A a3(4); m[5] = a1; m[3] = a2; m[2] = a3; } I am compiling like this : [vladimir@sandbox tmp]$ g++ b5.cpp -Wall

How does the STL map::find function work without the equality operator?

心不动则不痛 提交于 2020-01-13 07:43:29
问题 Under the hood, an STL map is a red-black tree, and it uses the < operator of its keys or a user-provided comparison to figure out the location for element insertion. map::find() returns the element that matches the supplied key (if any matches are present) How can it do this without using an equality operator? Let's say my map has the keys 1, 2, 3, and 4 in it. Using only <, I could see that the key 2 should go after 1, after 2, and before 3. But I can't tell whether or not 2 is the same as

Why are there so many specializations of std::swap?

旧巷老猫 提交于 2020-01-13 07:36:07
问题 While looking at the documentation for std::swap, I see a lot of specializations. It looks like every STL container, as well as many other std facilities have a specialized swap. I thought with the aid of templates, we wouldn't need all of these specializations? For example, If I write my own pair it works correctly with the templated version: template<class T1,class T2> struct my_pair{ T1 t1; T2 t2; }; int main() { my_pair<int,char> x{1,'a'}; my_pair<int,char> y{2,'b'}; std::swap(x,y); } So

Heapify in C++ STL? [duplicate]

我的梦境 提交于 2020-01-13 04:56:08
问题 This question already has answers here : How to change max element in a heap in C++ standard library? (2 answers) Closed 4 years ago . I am looking for a function that does Heapify but seems that there is no efficient function directly from C++ STL. A heapify function as defined in CLRS text book will take in an element location i, assuming the left and right sub trees of i are both heaps, and make the tree rooted at i become a heap, and complexity is log(n). Given a heap in [first, last), I

Multiple predicates specified during runtime

别说谁变了你拦得住时间么 提交于 2020-01-13 04:48:12
问题 There are operator classes in STL like less, equal_to, greater_equal etc. How to easily combine them to use with for example remove_if function? For example I want to remove in vector elements which are greater than 0 AND less than 3 AND not equal to 2, then it would be something like: remove_if (v.begin(), v.end(), bind2nd(greater<int>(),0) + bind2nd(less<int>(),3) + not1(bind2nd(equal_to<int>(), 2))); User during running of program can specify filtering options for example he can write: