A question about "ifstream"!

hello,guys! I have a question:these codes is from chapter 3,The C++ Programming Language (Special Edition).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <list>
#include <vector>

int _tmain(int argc, _TCHAR* argv[])
{	
	std::string from,to;
	std::cin >> from >>to;

	std::ifstream is(from.c_str());
	std::istream_iterator <std::string> ii(is);
	std::istream_iterator<std::string> eos;

	std::vector<std::string> b(ii,eos);
	std::sort(b.begin(),b.end());

	std::ofstream<std::string> os(to.c_str());
	std::ostream_iterator <std::string> oo(os,"\n");

	std::unique_copy(b.begin(),b.end(),oo);

	return !is.eof() || !os;
}

when I complie them with vs2003 or Dev-CPP, they replay me with "no define the class of ifstream".

Does I miss any head files, or the version of my compliers is too old?

thanks.

Try adding

#include <algorithm> // for unique_copy, sort
#include <iterator> // for istream_iterator
#include <fstream> // for ifstream, ofstream
Thanks. jsmith!

:)

I think it will be working!

hi. guys!

Those codes above have some problems.

and these codes are ok.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <map>
#include <iterator>
#include <algorithm>

std::map<std::string,int> histgram;

void record(const std::string &s){

	histgram[s]++;
}

void print(const std::pair<std::string,int> &r){
	std::cout<<"The string \""<<r.first<<"\" dispalyed "<<r.second<<" times!\n";
}

int _tmain(int argc, _TCHAR* argv[])
{	
	std::string from,to;
	std::cin >> from >>to;

	std::ifstream is(from.c_str());
	std::istream_iterator <std::string> ii(is);
	std::istream_iterator<std::string> eos;

	std::vector<std::string> b(ii,eos);
	std::sort(b.begin(),b.end());
	std::cout<<"The length of b is :"<<b.size()<<"\n";

	std::ofstream os(to.c_str());
	std::ostream_iterator<std::string> oo(os,"\n");

	std::unique_copy(b.begin(),b.end(),oo);

	std::cout << "from is :"<<from<<", and to is: "<<to<<"\n";

	std::vector<std::string>::const_iterator i=b.begin();
	int n=0;
	while(i !=b.end()){
		++n;
		std::cout << *i<<"\t";
		i++;}

	std::cout<<"\ntotal strings is: "<<n;

	for_each(b.begin(),b.end(),record);
	for_each(histgram.begin(),histgram.end(),print);

	return !is.eof() || !os;
}


Thanks jsmith! again.

c++ is really simple and elegant. :)

Is anyone want to send any c++ coding job to me?

Just email me: toptony@163.com

and I will do my best!

pls to explicit that you come from cplusplus.com
Last edited on
Topic archived. No new replies allowed.