gpt4 book ai didi

c++ - "Email"读者 - 奇怪的迭代器问题

转载 作者:行者123 更新时间:2023-11-28 01:12:50 24 4
gpt4 key购买 nike

这个程序读取“电子邮件”(实际上只是一个像电子邮件一样结构的 .txt 文件)并用它做各种事情(在 map 中存储数据并对其进行操作)。

但是,我在根据主题搜索输出电子邮件的“消息”时遇到了一个不寻常的问题。首先 - 这是代码(您可以忽略除结尾和 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 被分配给工作的同一个迭代器,它也不会输出任何东西。感谢任何帮助,谢谢。

附言显然 find_message() 函数还没有完成,只输出带有魔数(Magic Number)的第一行,但请耐心等待。

最佳答案

对我来说效果很好。您是否正在尝试搜索其中包含空格的主题?我相信这个说法

cin >> subject_search;

只读取字符串中的第一个空白字符。也许你想要

getline(cin,subject_search);

关于c++ - "Email"读者 - 奇怪的迭代器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1384665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com