Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 06-30-2005, 07:35 AM   #1 (permalink)
nathacof
Registered User
 
Join Date: Jun 2005
Posts: 2
nathacof is on a distinguished road
STL Basics tutorial

I'm just wondering how the program presented at the end of the tutorial knows when to stop accepting words from the standard input.
Code:
    // This vector of strings will be holding the words as the user
    // enters them.
    vector<string> words;

    // std::ostream_iterator's evil twin, std::istream_iterator, can
    // be used for doing the opposite: turn an input stream into an
    // iterator. Here we keep copy all strings from cin to the back of
    // the words vector:
    copy (istream_iterator<string> (cin), 
          istream_iterator<string> (),    // "end"
          back_inserter (words));
This is the code that I don't really understand. When I compile the program and run it I can enter an infinite number of words without the program printing the list of words.

Maybe I just don't understand iterators enough.
Code:
istream_iterator();
     Construct an end-of-stream iterator.  This iterator can be used to
     compare against an end-of-stream condition. Use it to provide end
     iterators to algorithms
This is from http://h30097.www3.hp.com/cplus/istr...or_3c__std.htm
It seems that I just don't know how to close the stream?
nathacof is offline   Reply With Quote
Old 06-30-2005, 08:03 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
redhead is on a distinguished road
Quote:
I'm just wondering how the program presented at the end of the tutorial knows when to stop accepting words from the standard input.
It reads till it reads EOS (End Of Stream) that beeing the end of file if it's a file you're reading from or a termination of your input stream when you read from stdin.
Quote:
It seems that I just don't know how to close the stream?
Terminate the tranmission, on *nix thats <ctrl>+D in DOS it's <ctrl>+S, it all depends on what system you're running.

A small example:
Code:
#include <iterator>
#include <vector>
#include <numeric>
#include <iostream>

using namespace std;

int main ()
{
  vector<int> d;
  int total = 0;
  //
  // Collect values from cin until end of file
  // Note use of default constructor to get ending iterator
  //
  cout << "Enter a sequence of integers (eof to quit): " ;
  copy(istream_iterator<int,char>(cin),
       istream_iterator<int,char>(),
       inserter(d,d.begin()));
  //
  // stream the whole vector and the sum to cout
  //
  copy(d.begin(),d.end()-1,
       ostream_iterator<int,char>(cout," + "));
  if (d.size())
    cout << *(d.end()-1) << " = " <<
      accumulate(d.begin(),d.end(),total) << endl;
  return 0;
}
Since this reads anything of type int then anything which isn't an int, like a char.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 06-30-2005, 05:35 PM   #3 (permalink)
nathacof
Registered User
 
Join Date: Jun 2005
Posts: 2
nathacof is on a distinguished road
Ah thank you for the help! ctrl-D causes the programs execution to continue right along, I had actually tried ctrl-x ctrl-c and even a couple random letters.

OK so iterators are just used to traverse over multiple objects in a container?
nathacof is offline   Reply With Quote
Old 06-30-2005, 10:52 PM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,711
redhead is on a distinguished road
Quote:
OK so iterators are just used to traverse over multiple objects in a container?
Yes, in a way, you can think of them as beeing the equivalent of something like
Code:
for(int i=0; i < container.length(); i++)
    // Do stuff with container[i];
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How does the tutorial section work? DavH27 Lounge 3 08-27-2004 02:46 AM
The Largest Tutorial in Code Newbie History! sde Code Newbie News 5 04-24-2004 07:19 PM
tutorial stats sde Lounge 1 03-20-2004 02:45 PM


All times are GMT -8. The time now is 09:32 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting