473,383 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

STL Question

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
_________________________________________
Mar 20 '06 #1
1 2225
In article <dn***************@news.uswest.net>,
mm*******@usfamily.net says...

[ ... ]
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 20 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.