這是我修C++ Lab題目
Read all the words from Alice_stroy1.txt using istream_iterator and write all unique words in a size order into cout using ostream_iterator. For those of the same size, write them in an alphbetic order.
1
/**//*
2
(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4
Filename : stream_iterator_alice.cpp
5
Compiler : Visual C++ 8.0 / ISO C++
6
Description : Demo how to transform() & sort() & stable_sort()
7
Release : 12/17/2006 1.0
8
*/
9
#include <fstream>
10
#include <iterator>
11
#include <vector>
12
#include <algorithm>
13
#include <iostream>
14
#include <string>
15
#include <cctype> // ispunct()
16
17
using namespace std;
18
19
bool sortRule(const string&, const string&);
20
string noPunct(const string&);
21
22
int main()
{
23
ifstream inFile("Alice_story1.txt");
24
25
vector<string> svec;
26
// Copy ifstream to vector
27
copy(istream_iterator<string>(inFile), istream_iterator<string>(), back_inserter(svec));
28
29
// transform vector, cutting off punctuation
30
transform(svec.begin(), svec.end(), svec.begin(), noPunct);
31
// Sort vector for unique
32
sort(svec.begin(), svec.end());
33
// Unique vector
34
vector<string>::iterator iter = unique(svec.begin(), svec.end());
35
svec.erase(iter, svec.end());
36
37
stable_sort(svec.begin(), svec.end(), sortRule);
38
// Copy to cout
39
copy(svec.begin(), svec.end(), ostream_iterator<string>(cout,"\n"));
40
return 0;
41
}
42
43
<img id=Codehighlighter1_1166_1200_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1166_1200_Closed_Text.style.display='none'; Codehighlighter1_1166_1200_Open_Image.style.display='inline'; Codeh


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
