stl-algorithm

Why does std::max_element require a ForwardIterator?

和自甴很熟 提交于 2019-12-19 13:04:20
问题 The C++ standard library's max_element algorithm requires the iterators passed as inputs to model ForwardIterator . My understanding is that ForwardIterator refines InputIterator by specifying that you can use a ForwardIterator to iterate over the same range multiple times. Therefore, multi-pass algorithms require ForwardIterator s. However, max_element is not a multi-pass algorithm - it is sufficient to iterate over a range once to determine its maximum element. So why does max_element need

Why does std::max_element require a ForwardIterator?

≡放荡痞女 提交于 2019-12-19 13:03:21
问题 The C++ standard library's max_element algorithm requires the iterators passed as inputs to model ForwardIterator . My understanding is that ForwardIterator refines InputIterator by specifying that you can use a ForwardIterator to iterate over the same range multiple times. Therefore, multi-pass algorithms require ForwardIterator s. However, max_element is not a multi-pass algorithm - it is sufficient to iterate over a range once to determine its maximum element. So why does max_element need

How to read arbitrary number of values using std::copy?

旧城冷巷雨未停 提交于 2019-12-19 00:56:00
问题 I'm trying to code opposite action to this: std::ostream outs; // properly initialized of course std::set<int> my_set; // ditto outs << my_set.size(); std::copy( my_set.begin(), my_set.end(), std::ostream_iterator<int>( outs ) ); it should be something like this: std::istream ins; std::set<int>::size_type size; ins >> size; std::copy( std::istream_iterator<int>( ins ), std::istream_iterator<int>( ins ) ???, std::inserter( my_set, my_set.end() ) ); But I'm stuck with the 'end' iterator --

Are std::fill, std::copy specialized for std::vector<bool>?

点点圈 提交于 2019-12-18 14:55:07
问题 When thinking about this question I start to wondering if std::copy() and/or std::fill are specialized (I really mean optimized) for std::vector<bool> . Is this required by C++ standard or, perhaps, it is common approach by C++ std library vendors? Simple speaking, I wonder to know if the following code: std::vector<bool> v(10, false); std::fill(v.begin(), v.end(), true); is in any way better/different than that: std::vector<bool> v(10, false); for (auto it = v.begin(); it != v.end(); ++it)

How to implement a lambda function for a sort algorithm involving object members, indirection, and casting?

可紊 提交于 2019-12-18 07:02:53
问题 I'm working on some code and I have a section where I do a one off sort function. To implement it I decided it was easiest to overload the operator< function. What I would prefer to do is move the implementation of the sort closer to the actual call by using some sort of boost::bind, boost::phoenix, lambda or some other type of implementation. Unfortunately I don't have access to new C++11 functionality. Below is some example code. // In a header struct foo { char * a; char * c_str() { return

Wrong results when appending vector to itself using copy and back_inserter [duplicate]

夙愿已清 提交于 2019-12-18 05:47:15
问题 This question already has answers here : Nice way to append a vector to itself (4 answers) Closed 5 years ago . Inspired by this question, asking how to append a vector to itself, my first thought was the following (and yes, I realize insert is a better option now): #include <algorithm> #include <iostream> #include <iterator> #include <vector> int main() { std::vector<int> vec {1, 2, 3}; std::copy (std::begin (vec), std::end (vec), std::back_inserter (vec)); for (const auto &v : vec) std:

Why does std::find_if(first, last, p) not take predicate by reference?

不打扰是莪最后的温柔 提交于 2019-12-18 05:47:06
问题 I was looking at the various signatures for std::find_if on cppreference.com, and I noticed that the flavors that take a predicate function appear to accept it by value: template< class InputIt, class UnaryPredicate > InputIt find_if( InputIt first, InputIt last, UnaryPredicate p ); If I understand them correctly, lambdas with captured variables allocate storage for either references or copies of their data, and so presumably a "pass-by-value" would imply that the copies of captured data are

std::ostream_iterator prevent last item from using the delimiter [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-18 03:32:21
问题 This question already has answers here : Printing lists with commas C++ (27 answers) Closed 3 years ago . Is there a way to use a std::ostream_iterator (or similar) such that the delimiter isn't placed for the last element? #include <iterator> #include <vector> #include <algorithm> #include <string> using namespace std; int main(int argc, char *argv[]) { std::vector<int> ints = {10,20,30,40,50,60,70,80,90}; std::copy(ints.begin(),ints.end(),std::ostream_iterator<int>(std::cout, ",")); } Will

STL vector reserve() and copy()

馋奶兔 提交于 2019-12-17 23:42:47
问题 Greetings, I am trying to perform a copy from one vector (vec1) to another vector (vec2) using the following 2 abbreviated lines of code (full test app follows): vec2.reserve( vec1.size() ); copy(vec1.begin(), vec1.end(), vec2.begin()); While the call to vec2 sets the capacity of vector vec2, the copying of data to vec2 seems to not fill in the values from vec1 to vec2. Replacing the copy() function with calls to push_back() works as expected. What am I missing here? Thanks for your help.

how to find the intersection of two std::set in C++?

五迷三道 提交于 2019-12-17 17:34:14
问题 I have been trying to find the intersection between two std::set in C++, but I keep getting an error. I created a small sample test for this #include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; int main() { set<int> s1; set<int> s2; s1.insert(1); s1.insert(2); s1.insert(3); s1.insert(4); s2.insert(1); s2.insert(6); s2.insert(3); s2.insert(0); set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end()); return 0; } The latter program does not generate