C++ 如何检查输入数据是否与以前的输入数据相同?

C++ 如何检查输入数据是否与以前的输入数据相同?,c++,arrays,struct,C++,Arrays,Struct,我有一个任务,它提供了一个数据集。我创建了一个struct数组,希望输入值,但不知道如何输入值,而不让它在同一数组中重复 例如,数据集中有18名妇女参加了各自的活动 e、 g 我可以使用什么样的代码,以便在输入数据集时,第一个结构数组被分配给Tiffany及其各自的事件E1、E2、E3等,第二个结构将有她参加的Dione及其事件。在数据集中,不同的妇女参加了不同数量的活动。我想知道它是否能够检查第一个输入(即名字)是否相同,以便创建一个新的数组(如果是另一个女性)。如果不能使用STL,那么只需在

我有一个任务,它提供了一个数据集。我创建了一个struct数组,希望输入值,但不知道如何输入值,而不让它在同一数组中重复

例如,数据集中有18名妇女参加了各自的活动

e、 g


我可以使用什么样的代码,以便在输入数据集时,第一个结构数组被分配给Tiffany及其各自的事件E1、E2、E3等,第二个结构将有她参加的Dione及其事件。在数据集中,不同的妇女参加了不同数量的活动。我想知道它是否能够检查第一个输入(即名字)是否相同,以便创建一个新的数组(如果是另一个女性)。

如果不能使用STL,那么只需在数组中迭代,然后在发现每个人都在更改其事件后即可。下面您可以看到一个伪代码:

string prevName="", currName;

//read currName and event from the file
if (prevName != currName){
    //create a new entry in the array
    prevName = currName;
}
//add event in the current array position
但您也可以通过以下方式使用STL:

//These header are needed
#include <map>
#include <string>
#include <vector>
using namespace std;

//this is an example of defining persons and events
string p = "Tiffany", e = "E1";

//this is our data structure to keep records of people
map<string, vector<string> > PersonEvent;
//updating is as simple as calling this function
PersonEvent[p].push_back(e);

//going through every record of a person
for(int i=0; i<PersonEvent[p].size(); i++)
   //do something with PersonEvent[p][i]
//需要这些头
#包括
#包括
#包括
使用名称空间std;
//这是一个定义人员和事件的示例
字符串p=“Tiffany”,e=“E1”;
//这是我们用来保存人员记录的数据结构
地图人物事件;
//更新与调用此函数一样简单
PersonEvent[p]。推回(e);
//翻阅一个人的所有记录

对于(int i=0;我能从文件中读取数据集并加载到结构中吗?或者,你能在运行时重新生成它吗?我还没有将数据集加载到结构中,因为我仍在查找代码,以便在为下一个女性及其事件创建新数组之前检查第一个女性事件是否在数组中。如果使用struct arrays,它是如何将数据输入到数组中的?比如,我如何能够以这样的方式编码,即我可以检查输入是否不再是Tiffany而是Dione来创建一个新的结构数组?-编辑-我将把它添加到我的问题中以使其更清晰我已经更新了我的答案。如果你需要更多细节,请添加一部分代码,以便我能够解释更多。我也理解d您包含的代码,谢谢。这是我一直使用的代码。这是存储部分。
code
struct Person{string name;string event[];};Person P[];while(input>>ws&&!input.eof()){if(PrevName==CurrentName)输入>>P.name;输入>>P.event;else person*P=new P[SIZE];}
//These header are needed
#include <map>
#include <string>
#include <vector>
using namespace std;

//this is an example of defining persons and events
string p = "Tiffany", e = "E1";

//this is our data structure to keep records of people
map<string, vector<string> > PersonEvent;
//updating is as simple as calling this function
PersonEvent[p].push_back(e);

//going through every record of a person
for(int i=0; i<PersonEvent[p].size(); i++)
   //do something with PersonEvent[p][i]