(原創) 如何使用multimap? 如何使用StringStream解析文字檔? (C/C++) (STL)

map迷人之處在於一次存key和value兩個值,且key是唯一的,但若遇到key可重複的情況呢?STL另外提供了multimap...

這是我修C++ Lab的題目,讓我們練習使用multimap,但其中能有許多小技巧值得學習:

Write a program to read, parse and insert the entries containing author-title records (file books-11-30-2006.txt). Insert the author-title pairs into a std:multimap. Your ouputs should look like:

1 None.gif key: Elisa Bertino value: Object-Oriented Database Systems
2 None.gifkey: Jeffrey D. Ullman value: Principles of Database Systems ,  2nd Edition
3 None.gifkey: Jeffrey D. Ullman value: A First Course in Database Systems
4 None.gifkey: Jeffrey D. Ullman value: Principles of Database and Knowledge-Base Systems
5 None.gifkey: R. G. G. Cattell value: Object Data Management: Object-Oriented and Extended Relational Database Systems
6 None.gifkey: Stanley B. Lippman value: C++ Primer ,  4th Edition
7 None.gif請按任意鍵繼續 . . .


原本的books-11-30-2006.txt文字檔內容如下

 1 None.gif author  =  {Jeffrey D. Ullman} ,  title  =  {Principles of Database Systems ,  2nd Edition}
 2 None.gif
 3 None.gifauthor  =  {Elisa Bertino} ,  title  =  {Object-Oriented Database Systems}
 4 None.gif
 5 None.gifauthor  =  {Jeffrey D. Ullman} ,  title  =  {A First Course in Database Systems}
 6 None.gif
 7 None.gifauthor  =  {R. G. G. Cattell} ,  title  =  {Object Data Management: Object-Oriented and Extended Relational Database Systems}
 8 None.gif
 9 None.gifauthor  =  {Jeffrey D. Ullman} ,  title  =  {Principles of Database and Knowledge-Base Systems}
10 None.gif
11 None.gifauthor  =  {Stanley B. Lippman} ,  title  =  {C++ Primer ,  4th Edition}


程式碼如下

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /* 
 2InBlock.gif(C) OOMusou 2006 http://oomusou.cnblogs.com
 3InBlock.gif
 4InBlock.gifFilename    : MultiMapWithStringStream.cpp
 5InBlock.gifCompiler    : Visual C++ 8.0 / ISO C++
 6InBlock.gifDescription : Demo how to use multimap and StringStream to parse text
 7InBlock.gifRelease     : 12/14/2006 1.0
 8ExpandedBlockEnd.gif*/

 9 None.gif#include  < iostream >
10 None.gif#include  < fstream >
11 None.gif#include  < sstream >
12 None.gif#include  < map >
13 None.gif#include  < string >
14 None.gif#include  < algorithm >
15 None.gif
16 None.gif using   namespace  std;
17 None.gif
18 None.gif void  print(pair < string string > );
19 None.gif
20 ExpandedBlockStart.gifContractedBlock.gif int  main()  dot.gif {
21InBlock.gif  typedef multimap<stringstring> AuthorBooks;
22InBlock.gif  AuthorBooks authorBooks;
23InBlock.gif
24InBlock.gif  ifstream input("books-11-30-2006.txt");
25InBlock.gif  string line, word, dump;
26ExpandedSubBlockStart.gifContractedSubBlock.gif  while(getline(input,line)) dot.gif{
27ExpandedSubBlockStart.gifContractedSubBlock.gif    if (line == ""dot.gif{
28InBlock.gif      continue;
29ExpandedSubBlockEnd.gif    }

30InBlock.gif
31InBlock.gif    istringstream ss(line);
32InBlock.gif    ss >> dump >> dump; // Ignore author = 
33InBlock.gif
34InBlock.gif    ostringstream author;
35ExpandedSubBlockStart.gifContractedSubBlock.gif    while(ss >> word) dot.gif{
36ExpandedSubBlockStart.gifContractedSubBlock.gif      if (word.find('{'!= string::npos) dot.gif{
37InBlock.gif        // Begin with 1 for not include '{'
38InBlock.gif        author << word.substr(1); 
39ExpandedSubBlockEnd.gif      }
 
40ExpandedSubBlockStart.gifContractedSubBlock.gif      else if (word.find('}'!= string::npos) dot.gif{
41InBlock.gif        // End with word.size()-2 for not include "},"
42InBlock.gif        author << " " << word.substr(0,word.size()-2);
43InBlock.gif        break;
44ExpandedSubBlockEnd.gif      }
 
45ExpandedSubBlockStart.gifContractedSubBlock.gif      else dot.gif{
46InBlock.gif        author << " " << word;
47ExpandedSubBlockEnd.gif      }

48ExpandedSubBlockEnd.gif    }

49InBlock.gif
50InBlock.gif    ostringstream book;
51InBlock.gif    ss >> dump >> dump; // Ignore title = 
52ExpandedSubBlockStart.gifContractedSubBlock.gif    while(ss >> word) dot.gif{
53ExpandedSubBlockStart.gifContractedSubBlock.gif      if (word.find('{'!= string::npos) dot.gif{
54InBlock.gif        book << word.substr(1);
55ExpandedSubBlockEnd.gif      }
 
56ExpandedSubBlockStart.gifContractedSubBlock.gif      else if (word.find('}'!= string::npos) dot.gif{
57InBlock.gif        book << " " << word.substr(0,word.size()-1);
58InBlock.gif        break;
59ExpandedSubBlockEnd.gif      }
 
60ExpandedSubBlockStart.gifContractedSubBlock.gif      else dot.gif{
61InBlock.gif        book << " " << word;
62ExpandedSubBlockEnd.gif      }

63ExpandedSubBlockEnd.gif    }

64InBlock.gif
65InBlock.gif    // StringString to string : author.str()
66InBlock.gif    authorBooks.insert(make_pair(author.str(),book.str()));
67ExpandedSubBlockEnd.gif  }

68InBlock.gif
69InBlock.gif  for_each(authorBooks.begin(), authorBooks.end(), print);
70ExpandedBlockEnd.gif}

71 None.gif
72 ExpandedBlockStart.gifContractedBlock.gif void  print(pair < string string >  elem)  dot.gif {
73InBlock.gif  cout << "key: " << elem.first << " value: " << elem.second << endl;
74ExpandedBlockEnd.gif}


一般人處理字串,都會使用find()或find_last_of(),最後搭配substr(),32行使用StringStream來處理字串。StringStream的好用在於使用了<<和>>方式處理字串,而且可自動轉型,不需考慮型別,第32行,由於author = 這兩個字串並不是我們想處理的,可以將其>>到dump忽略之, 因為只想處理{}中間的值,所以使用find()找到'{'和'}',將中間的值湊成author StringStream。

66行為新增multimap的方式,要新增multimap,只有insert()搭配makepair一種方式而已,不像map可以使用subscripting的方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值