stl

How is nth_element Implemented?

心不动则不痛 提交于 2019-12-31 10:49:10
问题 There are a lot of claims on StackOverflow and elsewhere that nth_element is O(n) and that it is typically implemented with Introselect: http://en.cppreference.com/w/cpp/algorithm/nth_element I want to know how this can be achieved. I looked at Wikipedia's explanation of Introselect and that just left me more confused. How can an algorithm switch between QSort and Median-of-Medians? I found the Introsort paper here: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.14.5196&rep=rep1

Where are the headers of the C++ standard library

跟風遠走 提交于 2019-12-31 10:31:51
问题 I wonder where on my file system I find the headers of the C++ Standard library. In particular I am looking for the definition of the vector template. I searched in /usr/include/ and various subdirectories. I also tried 'locate vector.h' which brought up many implementations of vectors, but not the standard one. What am I missing? (The distribution is Gentoo) Background: I'm profiling a library that iterates over vector's most of the time and gprof shows that most of the time is spent in std:

Where are the headers of the C++ standard library

谁说胖子不能爱 提交于 2019-12-31 10:31:46
问题 I wonder where on my file system I find the headers of the C++ Standard library. In particular I am looking for the definition of the vector template. I searched in /usr/include/ and various subdirectories. I also tried 'locate vector.h' which brought up many implementations of vectors, but not the standard one. What am I missing? (The distribution is Gentoo) Background: I'm profiling a library that iterates over vector's most of the time and gprof shows that most of the time is spent in std:

Convert a vector<T> to initializer_list<T>

拈花ヽ惹草 提交于 2019-12-31 08:32:37
问题 Everyone creates std::vector from std::initializer_list , but what about the other way around? eg. if you use a std::initializer_list as a parameter: void someThing(std::initializer_list<int> items) { ... } There are times when you have your items in a vector<T> instead of a literal list: std::vector<int> v; // populate v with values someThing(v); // boom! No viable conversion etc. The more general question is: how to create an stl::initializer_list from a STL iterable, not just std::vector .

Why is the code in most STL implementations so convoluted?

寵の児 提交于 2019-12-31 08:10:53
问题 The STL is a critical piece of the C++ world, most implementations derive from the initial efforts by Stepanov and Musser. My question is given the criticality of the code, and it being one of the primary sources for people to view examples of well written C++ for both awe and learning purposes: Why are the various implementations of the STL so disgusting to look at - convoluted and generally good examples of how not to write C++ code from an aesthetics point of view. The code examples below

C++ STL allocator vs operator new

心不动则不痛 提交于 2019-12-31 08:08:17
问题 According to C++ Primer 4th edition, page 755, there is a note saying: Modern C++ programs ordinarily ought to use the allocator class to allocate memory. It is safer and more flexible. I don't quite understand this statement. So far all the materials I read teach using new to allocate memory in C++. An example of how vector class utilize allocator is shown in the book. However, I cannot think of other scenarios. Can anyone help to clarify this statement? and give me more examples? When should

What is the best way to use two keys with a std::map?

余生颓废 提交于 2019-12-31 08:04:06
问题 I have a std::map that I'm using to store values for x & y coordinates. My data is very sparse, so I don't want to use arrays or vectors, which would result in a massive waste of memory. My data ranges from -250000 to 250000 but I'll only have a few thousand points at the most. Currently I'm creating a std::string with the two coordinates (ie "12x45") and using it as a key. This doesn't seem like the best way to do it. My other thoughts were to use an int64 and shove the two int32s into it

STL sort function in C++ wrt strings

别来无恙 提交于 2019-12-31 06:59:46
问题 So I've been trying to sort a string based on the frequency of its characters. However the online judge I've been using shows me the error Line 17: invalid use of non-static member function 'bool olution::helper(char, char)' Why is the call to my function wrong? I have used the sort() function before, but not to strings. Is my helper() function incorrect? class Solution { public: unordered_map<char,int> freq; bool helper(char c1,char c2){ if(freq[c1]>freq[c2]) return false; else return true;

Can't assign List Iterator [C++]

走远了吗. 提交于 2019-12-31 06:01:17
问题 I have the following method: void* vpArr_t::operator[](int i) const { if (!isEmpty() && i >= 0 && i < nOfItems) { list<void*>::iterator it; int idx; for(it = array.begin(), idx = 0; idx < i; ++it, ++idx); // go to the i'th element return *it; } else { return NULL; } } Where: array is a list type. I'm getting a red underline (compilation error) in the following line: for(it = array.begin(), idx = 0; idx < i; ++it, ++idx); at: it = array.begin() it says that I'm tring to set a list<void*>:

Can't assign List Iterator [C++]

点点圈 提交于 2019-12-31 06:01:12
问题 I have the following method: void* vpArr_t::operator[](int i) const { if (!isEmpty() && i >= 0 && i < nOfItems) { list<void*>::iterator it; int idx; for(it = array.begin(), idx = 0; idx < i; ++it, ++idx); // go to the i'th element return *it; } else { return NULL; } } Where: array is a list type. I'm getting a red underline (compilation error) in the following line: for(it = array.begin(), idx = 0; idx < i; ++it, ++idx); at: it = array.begin() it says that I'm tring to set a list<void*>: