如何能够检查输入数据与之前的输入数据相同

How to be able to check the input data is the same from previous input data?

本文关键字:输入 数据 何能够 检查      更新时间:2023-10-16

我有一个分配,提供了一个数据集。我创建了一个结构数组,想要输入值,但不知道如何输入值,而不让它在同一数组中重复。

例如,数据集中有18名女性,她们参加了各自的活动。

e。g

Tiffany, E1
Tiffany, E2
Tiffany, E3
Tiffany, E4
Tiffany, E5
Tiffany, E6
Tiffany, E8
Tiffany, E9
Dione, E1
Dione, E2
Dione, E3
Dione, E5
Dione, E6
Dione, E7
Dione, E8
Kelly, E2
Kelly, E3
Kelly, E4
Kelly, E5
Kelly, E6
Kelly, E7
Kelly, E8
Kelly, E9

我可以使用什么样的代码,以便当我输入数据集时,第一个结构数组分配给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]