Okay, first, quotes go
before responses. This makes it easier to figure out what response goes with what quote. Second, don't quote
everything. That just makes the thread longer. Instead, pick out relevant statements and quote them.
>Prototype for 'for_each':
I'm quite familiar with for_each, so you don't need to tell me what it is.
>Judge it by running & analyzing things
Well, just by looking: It doesn't give the right output (0-4 instead of 1-5), you fail to include the requisite headers, you fail to qualify cout with std, you have to write two tiny functions to get for_each to work properly (thus displaying one of for_each's drawbacks), you have to make sure that vecString is properly sized from the start, vecString is a poor name, you've introduced an unnecessary static variable, and main doesn't return void (it returns int).
So I ask again, how is your use of for_each better than this, which is a better fit for your solution:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main()
{
int a[] = {1,2,3,4,5};
std::vector<int> v;
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, "\n"));
}
>Well why do u need a temporary list
To get the values. The list is either explicit through an array or other data structure, or implicit through a generator function or function object.
>..iterators are the only thing that is required
Um, do you even know what an iterator is?
>I still say its trivial
Then you're wrong. I said "add to a vector", not "assign to a vector". I'm talking about starting with an empty vector and using for_each to actually insert values into it, much like you would with copy:
int a[] = {1,2,3,4,5};
std::vector<int> v;
std::copy(a, a + 5, std::back_inserter(v));
Any idiot can write a function to assign values to an existing vector and use for_each. That's not a challenge at all, and it doesn't address the initial problem that the tip covers.
>why don't u listen to everybody, analyze things and make statement
I listened to what you said, analyzed your solution, and deemed it inappropriate. Thus my statements. Perhaps you should get your ego in check and try to understand what I'm trying to teach you. And yes, I am trying to teach you since your code suggests a lack of fundamental understanding about C++ in general and your comments display only a remedial understanding of the "STL".