Jump to content
  • Advertisement
Sign in to follow this  
wforl

effective stl book question

This topic is 3756 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I dont understand how the following is working, from my understandings the constructor for list, that they are using requires, a start and end location in memory. For which istream_iterator<int> dataBegin(dataFile); points to the beginning of the file in memory, which i understand but, the following dataEnd points to a random location doesnt it, its not initialized with anything. istream_iterator<int> dataEnd; here is the full code

#include <list>
#include <iostream>
#include <fstream>

int main()
{
	using namespace std;
	
	ifstream dataFile("ints.dat");



	{
		istream_iterator<int> dataBegin(dataFile);
		istream_iterator<int> dataEnd;
		list<int> data(dataBegin, dataEnd);	

		cout << "First value in the list is " << *data.begin() << endl;
	}
	return 0;
}




and here is what makes sense to me

#include <list>
#include <iostream>
#include <fstream>

int main()
{
	using namespace std;
	
	char* ptr = new char[10];



	{
	
		list<int> data(ptr, ptr+10);	

		cout << "First value in the list is " << *data.begin() << endl;
	}
	return 0;
}


Share this post


Link to post
Share on other sites
Advertisement
They should have given some explanation to make it more clear, bad author, but anyways, according to this site.

Quote:

The default constructor: Constructs an end-of-stream iterator. This is a past-the-end iterator, and it is useful when constructing a "range".


Hope that helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by Portmanteau
They should have given some explanation to make it more clear(clearer even?), bad author, but anyways...


I think you need to learn who the author is and in what context the sample was given in the book before you make such rash comments.
Quote:
Scott Meyers is one of the world's foremost experts on C++ software development. He wrote the best-selling Effective C++ series (Effective C++, More Effective C++, and Effective STL), wrote and designed the innovative Effective C++ CD, is consulting editor for Addison Wesley's Effective Software Development Series, and is an inaugural member of the Advisory Board for The C++ Source. He has served on the technical advisory boards for several start-up companies. A programmer since 1972, he holds an M.S. in Computer Science from Stanford University and a Ph.D. from Brown University.

Share this post


Link to post
Share on other sites
Quote:
Original post by wforl
the following dataEnd points to a random location doesnt it, its not initialized with anything.

istream_iterator<int> dataEnd;

You're mislead. Only native types do not get constructed with a default value when created in storage of automatic duration unless explicitly initialized. The above construction of a std::istream_iterator invokes the default constructor, which in the case of a std::istream_iterator creates a "singular" iterator value (which can be assigned to and compared but not dereferenced on pain of dismemberment). It is used as a standard sentinel value for one-past-the-end of any std:istream_iterator.

Just like a NULL pointer.

Clever, no?

Share this post


Link to post
Share on other sites
Quote:
Original post by wforl
I dont understand how the following is working, from my understandings the constructor for list, that they are using requires, a start and end location in memory.


No, they require start and end iterators. They may correspond to memory locations, but that is not required. What is required is that they are valid iterators. The istream_iterator examples are valid, and the default constructor for an istream_iterator is not random, merely generic.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma
Just like a NULL pointer.


Precisely not like a NULL pointer. It's actually like a past-the-end pointer, with the exception that unlike pointers (which are random-access iterators) every sequence of input-stream iterators has the same past-the-end iterator (this is possible because they're input iterators and not random-access).

Share this post


Link to post
Share on other sites
i still dont understand how

istream_iterator<int> dataEnd;


can determine the position of the end of file, when it doesn't even know what file its looking at.

Please could someone give an in depth reply, as im obviously using pointers and iterators int the wrong context.

Share this post


Link to post
Share on other sites
Quote:
Original post by wforl
i still dont understand how

istream_iterator<int> dataEnd;


can determine the position of the end of file, when it doesn't even know what file its looking at.

Please could someone give an in depth reply, as im obviously using pointers and iterators int the wrong context.


dataEnd doesn't determine the end of the file, it merely represents the end of the file. In the same way that you, as a human, can use the words "end of file" to represent the concept without even know what file you're talking about.

What happens is that, when you create an iterator from a file, and that iterator reaches the end of that file, it compares equal to a default-constructed iterator. That is, operator == returns true. This does not mean that the default-constructed iterator is at the end of the file, but rather that operator == was programmed to act as if this was the case.

Share this post


Link to post
Share on other sites
Quote:
Original post by wforl
i still dont understand how

istream_iterator<int> dataEnd;


can determine the position of the end of file, when it doesn't even know what file its looking at.


Is end-of-file different for each file?

When dataEnd is default constructed, it has the value of end-of-file.

Quote:
Please could someone give an in depth reply, as im obviously using pointers and iterators int the wrong context.


Pointers and iterators are different concepts. Iterators are something that is used to iterate. While some pointers can be realized with some iterators, they are a subset.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!