stl

How is std::is_empty<T> implemented in VS2015 (or any compiler)?

谁说胖子不能爱 提交于 2020-01-03 08:08:37
问题 My current question has been inspired by attempting to understand how std::unique_ptr<T, D> utilizes template mechanics to instantiate a template class the size of a T* when D (the deleter type) is a lambda function type, but a larger size when D is a function pointer type (since space needs to be allocated in the unique_ptr instance to store the function pointer). Looking through the VS2015 source code, I find that std::unique_ptr derives from std::_Unique_ptr_base , which in turn declares a

How is std::is_empty<T> implemented in VS2015 (or any compiler)?

血红的双手。 提交于 2020-01-03 08:08:27
问题 My current question has been inspired by attempting to understand how std::unique_ptr<T, D> utilizes template mechanics to instantiate a template class the size of a T* when D (the deleter type) is a lambda function type, but a larger size when D is a function pointer type (since space needs to be allocated in the unique_ptr instance to store the function pointer). Looking through the VS2015 source code, I find that std::unique_ptr derives from std::_Unique_ptr_base , which in turn declares a

Find last element in std::vector which satisfies a condition

痞子三分冷 提交于 2020-01-03 04:53:27
问题 I have this requirement to find the last element in the vector which is smaller than a value. Like find_first_of but instead of first i want last. I searched and found that there is no find_last_of but there is find_first_of. Why is that so? Is the standard way is to use find_first_of with reverse iterators? 回答1: Use reverse iterators, like this: #include <iostream> #include <vector> int main() { std::vector<int> v{1,2,42,42,63}; auto result = std::find_if(v.rbegin(), v.rend(), [](int i) {

C++ STL map in shared memory

允我心安 提交于 2020-01-03 04:47:32
问题 i need to place a STL map in shared memory. Also have multiple process access that map. Any pointers to how it is done ? I have checked this link. But need a more simpler method. Map in Shared memory 回答1: For this to work you need to use a custom allocator that will allocate from the shared memory region, so that the map nodes are all in shared memory, and so that the pointer type of the allocator is not just a raw pointer but can refer to the shared memory region when it is mapped to

Shift operations

与世无争的帅哥 提交于 2020-01-03 04:32:10
问题 I saw the following posted by one of the fellow stackoverflower and it sort of dumbfounds me. Would someone explain the shifting operations in the following code snippet: std::vector<bool> a; a.push_back(true); a.push_back(false); //... for (auto it = a.begin(); it != a.end();) // see 0x for meaning of auto { unsigned b = 0; for (int i = 0; i < 8*sizeof(b); ++i) { b |= (*it & 1) << (8*sizeof(b) - 1 - i); ++it; } // flush 'b' } 回答1: 8 * sizeof(b) is the number of bits that can be stored in 'b'

C++ iterate over vector of objects and apply STL algorithms to member variables

岁酱吖の 提交于 2020-01-03 03:30:10
问题 What again is the quick way to iterate over a vector of custom objects but access only a single member in order to apply general STL-algorithms? struct Foo { std::string a; double b = 1.0; }; int main() { std::vector<Foo> fooVector(20); // iterate over all members b -- as if we were iterating over a std::vector<double> std::discrete_distribution<int> dist(/*??*/, /*??*/); } With "quick" I mean no custom iterator -- or only a very lightweight one (--a few lines of code, no boost iterator

Using custom allocator for AllocatorAwareContainer data members of a class

流过昼夜 提交于 2020-01-03 00:39:12
问题 Given a non-stateless custom allocator A (say, arena allocator, binded to some contiguous memory chunk of runtime-known size) and class S , containing a fileds of AllocatorAwareContainer types: struct S { A() = default; // another c-tors std::vector< T > v; std::shared_ptr< U > s; }; I want to use A for a subset (or even for all) AllocatorAwareContainer fields of class S . It is clear, that I should to provide one another template parameter for class S and change types of all the interesting

Using custom allocator for AllocatorAwareContainer data members of a class

坚强是说给别人听的谎言 提交于 2020-01-03 00:39:08
问题 Given a non-stateless custom allocator A (say, arena allocator, binded to some contiguous memory chunk of runtime-known size) and class S , containing a fileds of AllocatorAwareContainer types: struct S { A() = default; // another c-tors std::vector< T > v; std::shared_ptr< U > s; }; I want to use A for a subset (or even for all) AllocatorAwareContainer fields of class S . It is clear, that I should to provide one another template parameter for class S and change types of all the interesting

C++ A-star implementation — determining whether a node is already in the priority queue of open items

我的未来我决定 提交于 2020-01-02 14:05:52
问题 One step in the A* pathfinding algorithm requires searching the list of open nodes for the node you're currently interacting with, and adding that node to the list if it isn't already there, or updating its value and parent, if it's present but with a higher weight than the current version of the node. These behaviors aren't supported in the STL priority_queue structure. How should I implement that step? Updates since this question is getting a lot of views: std::priority_queue may look like

C++ A-star implementation — determining whether a node is already in the priority queue of open items

﹥>﹥吖頭↗ 提交于 2020-01-02 14:05:22
问题 One step in the A* pathfinding algorithm requires searching the list of open nodes for the node you're currently interacting with, and adding that node to the list if it isn't already there, or updating its value and parent, if it's present but with a higher weight than the current version of the node. These behaviors aren't supported in the STL priority_queue structure. How should I implement that step? Updates since this question is getting a lot of views: std::priority_queue may look like