STL Question

G

Guest

am studying a book called "STL for C++" by Leen Ammeraal. copy right 1997

I am on page 160 of 209 pages and I have to got the output right for all of
the author's examples
I had to change the headers, and one function took too many arguments, but
I got it to get the same output as the author had.

However two projects with istream_iterators failed to compile. I am
enclosing the
code (it is possible some ot the headers are uneccasary) to see if
if any one knows how to code modern istream_iteraters. (I didn't have the
problems
with ostream_iterators) .




The following code produces a compiler error:

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <istream>
#include <vector>
#include <iterator>

using namespace std;

int main(int argc, char* argv[]) {
ifstream file("num.txt", ios::in);
int x;
if(file) {
istream_iterator<int, ptrdiff_t> i(file), eof;
}
return 0;
}

:\source\C and C++\stlTester\initer\initer.cpp(18) :
error C2664: '__thiscall std::istream_iterator<int,int,struct
std::char_traits<int> >::std::istream_iterator<int,int,struct
std::char_traits<int> >(class std::basic_istream<int,struct std::char_t
raits<int> > &)' :
cannot convert parameter 1 from 'class std::basic_ifstream<char,struct
std::
char_traits<char> >


Also the follwing code produces the following error:

// copyio.cpp : Defines the entry point for the console application.
//Taken from "STL for C++ programmers" by Leen Ammeral.
// I abandoned it because it line 22 and line 25 didn't compile

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <istream>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <functional>

using namespace std;

int main(int argc, char* argv[])
{
vector<int> a;
ifstream file("example.txt", 0);
if(!file) {
cout << "Cannot open file example.txt" << endl;
return 1;
}
copy(istream_iterator<int, ptrdiff_t> (file),
istream_iterator<int, ptrdiff_t>(),
inserter(a,a.begin()));
copy(a.begin(),a.end(),
ostream_iterator<int>(cout," "));
cout <<endl;

return 0;
}

f:\source\c and c++\stltester\copyio\copyio.cpp(5) : warning C4652: compiler
option 'Generate Browser Info' inconsistent with precompiled header; current
command-line option will override that defined in the precompiled header
F:\source\C and C++\stlTester\copyio\copyio.cpp(25) : error C2440: 'type
cast' : cannot convert from 'class std::basic_ifstream<char,struct
std::char_traits<char> >' to 'class std::istream_iterator<int,int,struct
std::char_traits<int> >'
No constructor could take the source type, or constructor overload
resolution was ambiguous
Error executing cl.exe.


_________________________________________ Mitchell McNurlin ICQ#:235649936
Current ICQ status: SMS: (Send an SMS message to my ICQ): +2783142235649936
More ways to contact me: http://wwp.icq.com/235649936
_________________________________________
 
J

Jerry Coffin

[ ... ]
The following code produces a compiler error:

#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <istream>
#include <vector>
#include <iterator>

using namespace std;

int main(int argc, char* argv[]) {
ifstream file("num.txt", ios::in);
int x;
if(file) {
istream_iterator<int, ptrdiff_t> i(file), eof;

An istream_iterator is normally created with one template
parameter. If you're reading structured data, you need to
define a structure of some sort, and then read in objects
of that type. At first glance, it looks like you're
trying to say you have a file of pairs of int's and
ptrdiff_t's, but storing a ptrdiff_t in a file is a bit
unusual, so I'm not sure whether that's what you intend
or not. Assuming it is, you'd do the job something like:

typedef std::pair<int, ptrdiff_t> f_type;

namespace std {
istream &operator>>(istream &is, f_type &p)
{
is >> p.first;
is >> p.second;
return is;
}
};

if (file) {
std::istream_iterator<f_type> i(file), eof;

A couple of notes: first of all, what we've added to
namespace std is a new specialization of an existing
operator for a user-defined type -- which is nearly the
only time we're allowed to add something to the std
namespace.

This code is being added TO namespace std, so we don't
have to specify 'std' on names -- the current namespace
is always searched for names anyway.
// copyio.cpp : Defines the entry point for the console application.
//Taken from "STL for C++ programmers" by Leen Ammeral.
// I abandoned it because it line 22 and line 25 didn't compile

When posting something like this, it's extremely helpful
if you add a comment on the line(s) you're talking about.
It's quite difficult to guess where to start counting
lines from...
using namespace std;

int main(int argc, char* argv[])
{
vector<int> a;
ifstream file("example.txt", 0);
if(!file) {
cout << "Cannot open file example.txt" << endl;
return 1;
}
copy(istream_iterator<int, ptrdiff_t> (file),
istream_iterator<int, ptrdiff_t>(),
inserter(a,a.begin()));

You have the same basic problem here as above -- an
istream_iterator takes only one template parameter. As
above, it's not at all clear what you intend the
ptrdiff_t to mean. Since I showed code using it above,
this time I'll show code that just reads ints -- since
you're putting the result into a vector of int, that's
probably what you really want (in both cases).

When you're putting things into a vector, you _usually_
want to use back_inserter (though it doesn't matter a
whole lot).

copy(istream_iterator<int>(file),
istream_iterator<int>(),
back_inserter(a, a.begin()));

copy(a.begin(),a.end(),
ostream_iterator<int>(cout," "));

cout <<endl;

Also note that if you intend to copy from an input file
to an output file, without doing any processing in
between, you can skip using the vector in between, and
copy directly from one stream to the other:

copy(istream_iterator<int>(file),
istream_iterator<int>(),
ostream_iterator<int>(cout, " "));

If you want to do processing like sorting that works with
the entire data set, you may need to copy to a container.
If you want to do something on the order of line-based
filtering, you can often use std::transform instead of
std::copy, and (again) do the filtering on the way
through, without creating an intermediate copy of all the
data.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top