我有一个MasterProcess类,它包含一个MasterQuestion向量。 MasterQuestion类包含MasterAnswer的向量。这些包含每个过程的所有可能的问题和相应的答案。用户响应分别存储在ProcessStickyLog和QuestionStickyLog中。这些定义如下。
class MasterProcess
{
int m_processID;
vector <MasterQuestion> m_proc_questions;
};
class MasterQuestion
int m_questionID;
vector <MasterAnswer> m_ques_answers;
};
class MasterAnswer
{
int answer;
double a_score;
bool operator ==(const MasterAnswer& str) const
{
return answer == str.answer;
}
};
class QuestionStickyLog
{
int qID;
int ans;
};
class ProcessStickyLog
{
int procID;
vector <QuestionStickyLog> sl_questions;
};
我想查看用户在粘性日志中提供的答案,并使用主文档中的信息对其进行评分。由于用户可以选择不回答任何特定问题(甚至整个过程),我决定遍历主文档,获取问题/进程的ID,并查找是否在粘性日志中找到该ID。如果是,我从MasterAnswer获取适当的分数并存储它。如果不是,则对该特定问题给予零分。这是代码。
//get each masterProcess from masterDocument
for (m_p_pos1 = m_proc_vector.begin(); m_p_pos1 != m_proc_vector.end(); ++m_p_pos1)
{
int mprocID = m_p_pos1->getProcessID();
ProcessAudit p_audit(mprocID);
p_sl_pos1 = find_if(
proc_sl_vector.begin(),
proc_sl_vector.end(),
[mprocID](const ProcessStickyLog& other)
{
return other.getProcID() == mprocID;
}
);
if (p_sl_pos1 != proc_sl_vector.end())
{
m_ques_vector = m_p_pos1->getProcessQuestions();
sort(m_ques_vector.begin(),m_ques_vector.end());
for (m_q_pos1 = m_ques_vector.begin(); m_q_pos1 != m_ques_vector.end(); ++m_q_pos1)
{
int mquesID = m_q_pos1->getQuestionID();
QuestionAudit q_audit(mquesID);
q_sl_pos1 = find_if(
ques_sl_vector.begin(),
ques_sl_vector.end(),
[mquesID](const QuestionStickyLog& other)
{
return other.getQID() == mquesID;
}
);
if (q_sl_pos1 != ques_sl_vector.end())
{
m_ans_vector = m_q_pos1->getQuestionAnswers();
sort (m_ans_vector.begin(),m_ans_vector.end());
int qans = q_sl_pos1->getAns();
cout << "current sticky log question is " << q_sl_pos1->getQID() << endl;
cout << "current sticky log question answer is " << q_sl_pos1->getAns() << endl;
else
{
...
}
}// end for j
...
}// end for i
几天来,我试图找到为什么它提供的答案是错误的,最后我发现它正在做什么,虽然我不知道它为什么这样做。第二个for循环总是指向错误的元素,即StickyLogQuestion总是在所有进程中都有错误的问题。第一个for循环正常工作并指向正确的进程。对于每个QuestionStickyLog,答案总是相同的。这是该计划的输出。
Question sticky logs printed
=================================================
Process Sticky Log ID: 14
Question Sticky Log ID: 1
Question Sticky Log Ans: 1
-------------------------------------------------
Question Sticky Log ID: 3
Question Sticky Log Ans: 2
-------------------------------------------------
Question Sticky Log ID: 4
Question Sticky Log Ans: 2
-------------------------------------------------
=================================================
Process Sticky Log ID: 15
Question Sticky Log ID: 1
Question Sticky Log Ans: 1
-------------------------------------------------
Question Sticky Log ID: 2
Question Sticky Log Ans: 3
-------------------------------------------------
Question Sticky Log ID: 3
Question Sticky Log Ans: 1
-------------------------------------------------
Question Sticky Log ID: 4
Question Sticky Log Ans: 2
-------------------------------------------------
Question Sticky Log ID: 5
Question Sticky Log Ans: 2
-------------------------------------------------
=================================================
2nd for loop iteration prints as follows
=================================================
current sticky log question is 1
current sticky log question answer is 1
current sticky log question is 2
current sticky log question answer is 3
current sticky log question is 3
current sticky log question answer is 1
Audit Score for Process -> 10 is 0.7
-------------------------------------------------
current sticky log question is 1
current sticky log question answer is 1
current sticky log question is 2
current sticky log question answer is 3
current sticky log question is 3
current sticky log question answer is 1
current sticky log question is 4
current sticky log question answer is 2
current sticky log question is 5
current sticky log question answer is 2
Audit Score for Process -> 12 is 0.4
-------------------------------------------------
我遍历StickyLog向量并打印ProcessID,问题ID和答案,并且它们被正确打印,但是当我再次通过向量来分配分数时,QuestionStickyLog迭代器指向与上一个过程相同的问题。指向。请注意,在“2nd for loop iteration”下显示的输出中,(问题,答案)的以下组合总是出现在stickylog迭代器中:(1,1),(2,3),(3,1),(4 ,2),(5,2)
现在这个输出是完全错误的,而不是用户输入,它让我感到困惑,为什么很长一段时间,直到我注意到这些组合[(1,1),(2,3),(3,1),( 4,2),(5,2)]与Sticky Log Prints中显示的Process 15的输出完全相同。出于某种原因,QuestionStickyLog迭代器总是指向这5个问题,并且对于每个过程中的每个问题,都会重复这些答案。
有人可以指出为什么迭代器总是指向错误的固定元素。
编辑:
这是可编辑的版本(应该是类似的东西,虽然它根本没有向我显示任何输出)我正在做的事情。
编辑:
它现在可以正常工作并完全重现问题。想出来并解决了原来的问题。
#include <vector>
class Ques
{
public:
int qid;
int ans;
Ques(int i, int a)
{
qid =i;
ans =a;
}
int getid() { return qid;}
int getans(){ return ans;}
};
class Proc
{
public:
int pid;
std::vector <Ques> q1;
Proc(int p)
{
pid = p;
}
void addques(Ques obj)
{
q1.push_back(obj);
}
std::vector<Ques>& getques()
{
return q1;
}
bool operator ==(const Proc& str) const
{
return pid == str.pid;
}
};
class Doc
{
public:
int id;
Doc(int i)
{
id = i;
}
std::vector<Proc> procs;
void addproc(Proc obj)
{
procs.push_back(obj);
}
std::vector<Proc>& getproc()
{
return procs;
}
};
class Usrques
{
public:
int id;
int ans;
Usrques(int i, int a)
{
id =i;
ans =a;
}
int getid() { return id;}
int getans(){ return ans;}
bool operator ==(const Usrques& str) const
{
return id == str.id;
}
};
class Usrproc
{
public:
int id;
std::vector<Usrques> uques;
Usrproc(int p)
{
id = p;
}
void addusrques(Usrques obj)
{
uques.push_back(obj);
}
std::vector<Usrques>& getusrques()
{
return uques;
}
bool operator ==(const Usrproc& str) const
{
return id == str.id;
}
};
class Usrdoc
{
public:
int uid;
Usrdoc(int i)
{
uid = i;
}
std::vector<Usrproc> uprocs;
void addusrproc(Usrproc obj)
{
uprocs.push_back(obj);
}
std::vector<Usrproc>& getusrproc()
{
return uprocs;
}
};
using namespace std;
int main ()
{
typedef vector<Proc> p_vector;
p_vector proc_vector;
p_vector::iterator p_pos1;
typedef vector<Ques> q_vector;
q_vector ques_vector;
q_vector::iterator q_pos1;
typedef vector<Usrproc> usrp_vector;
usrp_vector usr_proc_vector;
usrp_vector::iterator usr_p_pos1;
typedef vector<Usrques> usrq_vector;
usrq_vector usr_ques_vector;
usrq_vector::iterator usr_q_pos1;
Ques q1(1,1);
Ques q2(2,1);
Ques q3(3,2);
Proc p1(1);
p1.addques(q1);
p1.addques(q2);
p1.addques(q3);
Ques q4(1,2);
Ques q5(2,3);
Ques q6(3,1);
Proc p2(2);
p2.addques(q4);
p2.addques(q5);
p2.addques(q6);
Doc doc(1);
doc.addproc(p1);
doc.addproc(p2);
Usrques q7(1,2);
Usrques q8(2,2);
Usrques q9(3,1);
Usrproc p3(1);
p3.addusrques(q7);
p3.addusrques(q8);
p3.addusrques(q9);
Usrques q10(1,2);
Usrques q11(2,3);
Usrques q12(3,1);
Usrproc p4(2);
p4.addusrques(q10);
p4.addusrques(q11);
p4.addusrques(q12);
Usrdoc udoc(2);
udoc.addusrproc(p3);
udoc.addusrproc(p4);
proc_vector = doc.getproc();
usr_proc_vector = udoc.getusrproc();
for (p_pos1 = proc_vector.begin(); p_pos1 != proc_vector.end(); ++p_pos1)
{
int mprocID = p_pos1->pid;
usr_p_pos1 = find_if (usr_proc_vector.begin(),usr_proc_vector.end(),
[mprocID](const Usrproc& other)
{ return other.id == mprocID; });
if (usr_p_pos1 != usr_proc_vector.end())
{
ques_vector = p_pos1->getques();
for (q_pos1 = ques_vector.begin(); q_pos1 != ques_vector.end(); ++q_pos1)
{
int mquesID = q_pos1->qid;
usr_ques_vector = usr_p_pos1->getusrques();
usr_q_pos1 = find_if (usr_ques_vector.begin(),usr_ques_vector.end(),
[mquesID](const Usrques& other)
{ return other.id == mquesID; });
if (usr_q_pos1 != usr_ques_vector.end())
{
cout << "current sticky log question is " << usr_q_pos1->id << endl;
cout << "current sticky log question answer is " << usr_q_pos1->ans << endl;
}
else
{
}
}
}
else
{
}
}
}
}