2009-09-06 23 views
0

該程序讀取「電子郵件」(實際上只是一個像電子郵件一樣構造的.txt文件)並用它做各種事情(將數據存儲在地圖中並對其進行處理)。「電子郵件」閱讀器 - 奇數迭代器問題

但是,我遇到了一個不尋常的問題,根據搜索主題輸出電子郵件的「消息」。首先 - 這裏是代碼(你大概可以忽略,除了年底和find_message功能應有盡有。)

#include <string> 
#include <vector> 
#include <map> 
#include <fstream> 
#include <iostream> 
using namespace std; 

typedef vector<string>::const_iterator Line_iter; 
class Message { // a Message points to the first and the last lines of a message 
    Line_iter first; 
    Line_iter last; 
public: 
    Message(Line_iter p1, Line_iter p2) :first(p1), last(p2) {} 
    Line_iter begin() const { return first; } 
    Line_iter end() const { return last; } 
    //... 
}; 

typedef vector<Message>::const_iterator Mess_iter; 

struct Mail_file { // a Mail_file holds all the lines from a file 
        // and simplifies access to messages 
    string name; // file name 
    vector<string> lines; // the lines in order 
    vector<Message> m; // Messages in order 

    Mail_file(const string& n); // read file n into lines 

    Mess_iter begin() const { return m.begin(); } 
    Mess_iter end() const { return m.end(); } 
}; 

Mail_file::Mail_file(const string& n) 
    // open file named "n" 
    // read the lines from "n" into "lines" 
    // find the messages in the lines and compose them in m 
    // for simplicity assume every message is ended by a "----" line 
{ 
    ifstream in(n.c_str()); // open the file 
    if (!in) { 
     cerr << "no " << n << '\n'; 
     exit(1); // terminate the program 
    } 

    string s; 
    while (getline(in,s)) lines.push_back(s); // build the vector of lines 

    Line_iter first = lines.begin(); // build the vector of Messages 
    for (Line_iter p = lines.begin(); p!=lines.end(); ++p) { 
     if (*p == "----") { // end of message 
      m.push_back(Message(first,p)); 
      first = p+1; // ---- not part of message 
     } 
    } 
} 

int is_prefix(const string& s, const string& p) 
    // is p the first part of s? 
{ 
    int n = p.size(); 
    if (string(s,0,n)==p) return n; 
    return 0; 
} 

bool find_from_addr(const Message* m, string& s) 
{ 
    for (Line_iter p = m->begin(); p!=m->end(); ++p) 
     if (int n = is_prefix(*p, "From: ")) { 
      s = string(*p, n); 
      return true; 
     } 
    return false; 
} 

string find_subject(const Message* m) 
{ 
    for (Line_iter p = m->begin(); p!=m->end(); ++p) 
     if (int n = is_prefix(*p, "Subject: ")) return string(*p, n); 
    return ""; 
} 

string find_message(const Message* m) 
{ 
    for (Line_iter p = m->begin(); p!=m->end(); ++p) 
     if (int n = is_prefix(*p, "Subject: ")) { 
      p += 2; 
      return string(*p); 
     } 
    return ""; 
} 

int main() 
{ 
    Mail_file mfile("my-mail-file.txt"); // initialize mfile from a file 

    // first gather messages from each sender together in a multimap 

    multimap<string, const Message*> sender; 
    multimap<string, const Message*> subject; 

    for (Mess_iter p = mfile.begin(); p!=mfile.end(); ++p) { 
     const Message& m = *p; 
     string s; 
     if (find_from_addr(&m,s)) 
      sender.insert(make_pair(s,&m)); 
     subject.insert(make_pair(find_subject(&m), &m)); 
    } 

    // now iterate through the multimap and extract the subjects of John Doe's messages 
    typedef multimap<string, const Message*>::const_iterator MCI; 
    pair<MCI,MCI> pp = sender.equal_range("John Doe"); 
    for (MCI p = pp.first; p != pp.second; ++p) 
     cout << find_subject(p->second) << '\n'; 

    string subject_search; 
    cout << '\n' << "Enter a subject to search for: "; 
    cin >> subject_search; 
    pair<MCI, MCI> sub = subject.equal_range(subject_search); 
    for (MCI p = sub.first; p != sub.second; ++p) 
     cout << '\n' << find_message(p->second); 
} 

出於某種原因,消息輸出輸出任何東西。我做了一些基本的測試,並試圖

cout << find_message(sub.first->second) << endl; 

只是爲了看看,如果功能不能正常工作,它輸出的消息罰款。所以很顯然,for循環出現了問題,即使p分配給了相同的迭代器,它也不會輸出任何內容。任何幫助表示讚賞,謝謝。

P.S.顯然find_message()函數還沒有完成,只輸出第一行帶有一個幻數,但忍受着我。

回答

3

適合我。你是否試圖尋找有空格的主題?我相信的聲明

cin >> subject_search; 

只讀取一個字符串,直到第一個空格字符。也許你想

getline(cin,subject_search); 
+0

完美。謝謝! – trikker 2009-09-06 02:09:31