stl-algorithm

STL vector reserve() and copy()

有些话、适合烂在心里 提交于 2019-11-28 19:31:39
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. vectest.cpp test program followed by resulting output follows. Compiler: gcc 3.4.4 on cygwin. Nat /** *

Why the sequence-operation algorithms predicates are passed by copy?

被刻印的时光 ゝ 提交于 2019-11-27 22:52:34
I'm wondering why functors are passed by copy to the algorithm functions: template <typename T> struct summatory { summatory() : result(T()) {} void operator()(const T& value) { result += value; std::cout << value << "; ";}; T result; }; std::array<int, 10> a {{ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 }}; summatory<int> sum; std::cout << "\nThe summation of: "; std::for_each(a.begin(), a.end(), sum); std::cout << "is: " << sum.result; I was expecting the following output: The summation of: 1; 1; 2; 3; 5; 8; 13; 21; 34; 55; is: 143 But sum.result contains 0 , that is the default value assigned in the

Why are std::shuffle methods being deprecated in C++14?

十年热恋 提交于 2019-11-27 20:59:23
According to the cppreference.com reference site on std::shufle , the following method is being deprecated in c++14: template< class RandomIt > void random_shuffle( RandomIt first, RandomIt last ); Why will we no longer be able to call the following function without passing a third parameter? std::random_shuffle(v.begin(),v.end()); //no longer valid in c++14 It doesn't appear as though a different function deceleration has a default parameter set. What is the reason behind this? Was there some kind of alternative added? Germán Diago std::random_shuffle may make use, under the hood, of random C

STL algorithms: Why no additional interface for containers (additional to iterator pairs)?

余生颓废 提交于 2019-11-27 05:01:55
I'm wondering why the STL doesn't overload their algorithm functions such that I can call them by simply providing a container and not taking the more verbose way to pass begin + end iterators. I of course understand why we also want to use an iterator pair for processing subsequences of a container / array, however, almost all calls to these methods are using a whole container: std::for_each(myVector.begin(), myVector.end(), doSomething); I'd find it more convenient, readable and maintainable to just write std::for_each(myVector, doSomething); Is there a reason STL doesn't provide these

Why is there no transform_if in the C++ standard library?

妖精的绣舞 提交于 2019-11-27 03:52:38
A use case emerged when wanting to do a contitional copy (1. doable with copy_if ) but from a container of values to a container of pointers to those values (2. doable with transform ). With the available tools I can't do it in less than two steps : #include <vector> #include <algorithm> using namespace std; struct ha { int i; explicit ha(int a) : i(a) {} }; int main() { vector<ha> v{ ha{1}, ha{7}, ha{1} }; // initial vector // GOAL : make a vector of pointers to elements with i < 2 vector<ha*> ph; // target vector vector<ha*> pv; // temporary vector // 1. transform(v.begin(), v.end(), back

map, lambda, remove_if

给你一囗甜甜゛ 提交于 2019-11-27 02:48:08
问题 So, i've problem with std::map, lambda and stl algorithm(remove_if). Actually, same code with std::list or std::vector works well. My test example : #include <map> #include <iostream> #include <algorithm> struct Foo { Foo() : _id(0) {} Foo(int id) : _id(id) { } int _id; }; typedef std::map<int, Foo> FooMap; int main() { FooMap m; for (int i = 0; i < 10; ++i) m[i + 100] = Foo(i); int removeId = 6; // <<< Error here >>> std::remove_if(m.begin(), m.end(), [=](const FooMap::value_type & item) {

How to use std::find/std::find_if with a vector of custom class objects?

做~自己de王妃 提交于 2019-11-26 22:19:12
I have a class representing a user called Nick and I want to use std::find_if on it, where I want to find if the userlist vector has an object included with the same username I pass in. I did a few attempts by trying to create a new Nick object for the username I want to test and overloading the == operator and then trying to use find/find_if on the object: std::vector<Nick> userlist; std::string username = "Nicholas"; if (std::find(userlist.begin(), userlist.end(), new Nick(username, false)) != userlist.end())) { std::cout << "found"; } I have overloaded the == operator so comparing Nick ==

Why the sequence-operation algorithms predicates are passed by copy?

牧云@^-^@ 提交于 2019-11-26 21:13:00
问题 I'm wondering why functors are passed by copy to the algorithm functions: template <typename T> struct summatory { summatory() : result(T()) {} void operator()(const T& value) { result += value; std::cout << value << "; ";}; T result; }; std::array<int, 10> a {{ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 }}; summatory<int> sum; std::cout << "\nThe summation of: "; std::for_each(a.begin(), a.end(), sum); std::cout << "is: " << sum.result; I was expecting the following output: The summation of: 1; 1; 2;

Why are std::shuffle methods being deprecated in C++14?

耗尽温柔 提交于 2019-11-26 20:34:15
问题 According to the cppreference.com reference site on std::shufle, the following method is being deprecated in c++14: template< class RandomIt > void random_shuffle( RandomIt first, RandomIt last ); Why will we no longer be able to call the following function without passing a third parameter? std::random_shuffle(v.begin(),v.end()); //no longer valid in c++14 It doesn't appear as though a different function deceleration has a default parameter set. What is the reason behind this? Was there some

How to remove duplicates from unsorted std::vector while keeping the original ordering using algorithms?

谁都会走 提交于 2019-11-26 16:26:38
I have an array of integers that I need to remove duplicates from while maintaining the order of the first occurrence of each integer. I can see doing it like this, but imagine there is a better way that makes use of STL algorithms better? The insertion is out of my control, so I cannot check for duplicates before inserting. int unsortedRemoveDuplicates(std::vector<int> &numbers) { std::set<int> uniqueNumbers; std::vector<int>::iterator allItr = numbers.begin(); std::vector<int>::iterator unique = allItr; std::vector<int>::iterator endItr = numbers.end(); for (; allItr != endItr; ++allItr) {