C++ 跟踪重复的关键点STL映射?

C++ 跟踪重复的关键点STL映射?,c++,dictionary,stl,duplicates,C++,Dictionary,Stl,Duplicates,我正在尝试使用STL映射编写代码,从文件中读取和解析行。如何跟踪文件中的重复项?关键值是术语和节。我想把复制品放在一个向量中,但我不太确定怎么做。还是有更好的办法 #include <fstream> #include <iostream> #include <iomanip> #include <string> #include <cstring> #include <ctime> #include <utility

我正在尝试使用STL映射编写代码,从文件中读取和解析行。如何跟踪文件中的重复项?关键值是术语和节。我想把复制品放在一个向量中,但我不太确定怎么做。还是有更好的办法

#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <ctime>
#include <utility>
#include <vector>
#include <map>

using namespace std;

int main()
{
  map <string, map <string, int> > subjectCourse;
  vector <string> duplicate;

  // for parsing the input file
  char* token;
  char buf[1000];
  const char* const tab = "\t";

 // open the input file
 ifstream fin;
 fin.open("schedule.txt");
 clock_t startTime = clock(); // start timer
 if (!fin.good()) throw "I/O error";

 while (fin.good())
 {
  // read line
  string line;
  getline(fin, line);
  strcpy(buf, line.c_str());
  if (buf[0] == 0) continue;

  // parse line
  const string term(token = strtok(buf, tab));
  const string section(token = strtok(0, tab));
  const string course((token = strtok(0, tab)) ? token : "");
  const string instructor((token = strtok(0, tab)) ? token : "");
  const string whenWhere( (token = strtok(0, tab)) ? token : "");
  if (course.find('-') == string::npos) continue; // invalid line
  const string subjectCode(course.begin(), course.begin() + course.find('-'));
  subjectCourse[subjectCode][course]++; // enter data to the map
}

fin.close();

for(map<string, map<string, int> >::iterator i = subjectCourse.begin(); i!= subjectCourse.end(); ++i){
cout << (*i).first << ", " << (*i).second.size() << " courses.\n";
  for(map<string, int>::iterator j = (*i).second.begin(); j != (*i).second.end(); ++j)
    cout << "   " << (*j).first << ", " << (*j).second << " class(es).\n";
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
地图学科课程;
载体复制;
//用于解析输入文件
字符*令牌;
char-buf[1000];
常量字符*常量选项卡=“\t”;
//打开输入文件
流鳍;
财务公开(“schedule.txt”);
clock_t startTime=clock();//启动计时器
如果(!fin.good())抛出“I/O错误”;
while(fin.good())
{
//读线
弦线;
getline(fin,line);
strcpy(buf,line.c_str());
如果(buf[0]==0)继续;
//解析行
常量字符串项(标记=strtok(buf,tab));
常量字符串部分(标记=strtok(0,tab));
常量字符串进程((令牌=strtok(0,tab))?令牌:“”);
常量字符串指导员((标记=strtok(0,tab))?标记:“”);
在何处((token=strtok(0,tab))?token:);
如果(course.find('-')==string::npos)继续;//行无效
常量字符串subjectCode(course.begin(),course.begin()+course.find('-');
subjectCourse[subjectCode][course]++;//向地图输入数据
}
fin.close();
for(map::iterator i=subjectCourse.begin();i!=subjectCourse.end();+i){

看来你需要的是。

我更喜欢:

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
多地图学科课程;
载体复制;
//用于解析输入文件
字符*令牌;
char-buf[1000];
常量字符*常量选项卡=“\t”;
//打开输入文件
流鳍;
财务公开(“schedule.txt”);
clock_t startTime=clock();//启动计时器
如果(!fin.good())抛出“I/O错误”;
while(fin.good())
{
//读线
弦线;
getline(fin,line);
strcpy(buf,line.c_str());
如果(buf[0]==0)继续;
//解析行
常量字符串项(标记=strtok(buf,tab));
常量字符串部分(标记=strtok(0,tab));
常量字符串进程((令牌=strtok(0,tab))?令牌:“”);
常量字符串指导员((标记=strtok(0,tab))?标记:“”);
在何处((token=strtok(0,tab))?token:);
如果(course.find('-')==string::npos)继续;//行无效
常量字符串subjectCode(course.begin(),course.begin()+course.find('-');
subjectCourse.insert(std::make_pair(subjectCode,course));//向地图输入数据
}
fin.close();
for(multimap::iterator i=subjectCourse.begin();i!=subjectCourse.end();+i)
{

cout如果您只需要重复检测,一个到int的键映射就足够了>1
为真,您有一个重复项。您是什么意思?因此我还需要跟踪除重复项以外的所有数据的数量?不,我的意思是,如果您关心的只是重复检测,您可以放弃值持久性,而只需使用计数器映射来确定应该是唯一键的内容。如果您想了解插入时的重复检测您想要保留的集合的时间,利用的结果,特别是
std::pair
,它包含一个键控值的迭代器和一个
bool
,指示该项是否是新插入项,可能更接近您想要采用的路径。从声音上看,您有点需要两者。谢谢,但我仍然需要跟踪k重复数。这里有两个键值,我看的大多数示例只比较单个键值。@chipupui mmmh,是和否。你必须计算它们,但它们在多重映射中。我添加了一个示例。
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <ctime>
#include <utility>
#include <vector>
#include <map>

using namespace std;

int main()
{
  multimap <string, string> subjectCourse;
  vector <string> duplicate;

    // for parsing the input file
  char* token;
  char buf[1000];
  const char* const tab = "\t";

    // open the input file
  ifstream fin;
  fin.open("schedule.txt");
    clock_t startTime = clock(); // start timer
    if (!fin.good()) throw "I/O error";

    while (fin.good())
    {
    // read line
      string line;
      getline(fin, line);
      strcpy(buf, line.c_str());
      if (buf[0] == 0) continue;

    // parse line
      const string term(token = strtok(buf, tab));
      const string section(token = strtok(0, tab));
      const string course((token = strtok(0, tab)) ? token : "");
      const string instructor((token = strtok(0, tab)) ? token : "");
      const string whenWhere( (token = strtok(0, tab)) ? token : "");
    if (course.find('-') == string::npos) continue; // invalid line
    const string subjectCode(course.begin(), course.begin() + course.find('-'));
    subjectCourse.insert(std::make_pair(subjectCode, course)); // enter data to the map
  }

  fin.close();

  for(multimap<string, string>::iterator i = subjectCourse.begin(); i!= subjectCourse.end(); ++i)
  {
    cout << " " << i->first << " course, " << i->second << " class(es).\n";
  }
}
 multimap<string, string>::iterator it = subjectCourse.begin();
 while (it != subjectCourse.end())
 {
   multimap<string, string>::iterator next_it = subjectCourse.upper_bound(it->first);
   cout << " " << it->first << " course as " << std::distance(it, next_it) << " class(es).\n";
   it = next_it;
 }
multimap<string, string>::iterator it = subjectCourse.begin();
while (it != subjectCourse.end())
{
  multimap<string, string>::iterator next_it = it;
  while ((subjectCourse.end() != next_it) 
         && (*it == *next_it))
  {
    ++next_it;
  }

  cout << " " << it->first << " course, " << it->second << " classes: " << std::distance(it, next_it) << " occurences.\n";
}