Re: STL vector - which style of for() loop?
See inline below:
Gerry Hickman wrote:
Hi,
I have this code below, and would like to know which for() loop style is
best - the second one seems a bit of a mouthful!
* Beware line wrap *
int _tmain(int argc, _TCHAR* argv[])
{
// Call a function that populates strings
vector< string > TheStrings;
TheStrings = returnStrings();
for(unsigned i = 0; i < TheStrings.size(); i++) {
cout << TheStrings[i] << endl;
}
for(vector<string>::iterator myIterator = TheStrings.begin();
myIterator != TheStrings.end(); myIterator++)
{
cout << *myIterator << endl;
}
std::copy( TheStrings.begin()
, TheStrings.end()
, std::ostream_iterator(cout) );
return 0;
}
vector< string > returnStrings()
{
// From Google Groups, demo of how to return a vector from a func
vector< string > Strings;
Strings.push_back( "hello" );
Strings.push_back( "hello1" );
Strings.push_back( "hello2" );
return Strings; // Original author didn't have this?
}
Jeff F