Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How to do loop statements in C++?
Index -> Programming, C++ -> C++ Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Tue May 02, 2006 8:10 pm   Post subject: (No subject)

Sanjay wrote:
code:
   int foo[] = {1, 2, 3, 4, 5};

   copy(foo, foo + 5, ostream_iterator<int>(cout, "\n"));

That scares me. Is it an actual For loop or an imitation?


The std::copy function is in the "algorithm" header. To it we pass two iterators. The first iterator is the beginning of the sequence, and the second is the end of the sequence.

Because iterators in C++ use the same operators as array access, we can provide the name of the array (which is actually a pointer to the beginning of the array in memory) as the initial iterator.

Since foo is 5 elements long, "foo + 5" indicates the end of the array. COnsider the following equivalences to better understand this:

code:
*foo == *(foo + 0) == foo[0]
*(foo + 5) == foo[5]


For an array of length 5, the last index is 4. Therefore if we have an index of 5, we are past the end of the array.

The third argument to the std::copy function is the output iterator. For this we use the ostream_iterator class from the "iterator" header. To its constructor we pass the ostream object we want to copy to. In this case, std::cout. We also pass the separator. This is a string that gets output after each element.

Make more sense now? Smile
Sponsor
Sponsor
Sponsor
sponsor
Sanjay




PostPosted: Tue May 02, 2006 8:46 pm   Post subject: (No subject)

Yep, thanks mate.

This is the only real reason I would consider .NET: If I was working with a group of programmers who don't understand:
for(i=1;i<5,i++){cout << i;}

It reads like normal English Very Happy (Actually, after a night of programming till 5 in the morning, I actually said "cout hello, Alex" to a friend. Maybe reading like English isn't such a good idea..,)
wtd




PostPosted: Tue May 02, 2006 9:03 pm   Post subject: (No subject)

Sanjay wrote:
Yep, thanks mate.

This is the only real reason I would consider .NET: If I was working with a group of programmers who don't understand:
for(i=1;i<5,i++){cout << i;}


If you're working with a group of programmers who cannot understand such simple code (once you fix your typo Wink ) then switching to another language/environment will not save you.
wtd




PostPosted: Tue May 02, 2006 9:07 pm   Post subject: (No subject)

Another example:

Let's read a bunch of integers into a vector.

code:
vector<int> numbers;

copy(istream_iterator<int>(cin), istream_iterator<int>(),
     back_inserter(numbers));
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 19 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: