4 ms·
Are you sure about that? Say spam is: int[] spam = {1,2,3}; How will for_each know what to do with this argument list? You would be passing in: 1, segfau
by anewaccountname 19y ago
Are you sure about that? Say spam is:
int[] spam = {1,2,3};
How will for_each know what to do with this argument list? You would be passing in: 1, segfault, and a function object named eggs?
- scott_s 19y agoActually, it should be: for_each(&spam[0], &spam[spam_length-1], eggs); I need to take the address of the arrays, not their contents, and if I'm going to use the same definition of spam_length as the author. This kind of mistake is why one should avoid native arrays in C++ - the abstractions provided by the STL are more expressive and no more expensive. The STL algorithms have no concept of length of a sequence, they only deal with iterators. This is the definition of for_each that Stroustrup provides in The C++ Programming Language: template<class In, class Op> Op for_each(In first, In last, Op f) { while (first != last) f(*first++); return f; } Notice that first and last can be pointers or STL style iterators, and that f can be a function object or an actual function. The template parameter In resolves to int* at compile time.