我正在从文件中读取并使用 strtok 将单词作为标记。我正在尝试将单词存储在地图结构中。我真的不知道如何在地图中插入标记。
到目前为止我的代码:
#include <iostream>
#include <string.h>
#include <fstream>
#include <map>
using namespace std;
//std::map <string, int> grade_list;
int main()
{
std::map <string, int> grade_list;
char text[100];
int nr=0, i=1;
char *ptr;
ifstream myfile("ana.txt");
if(!myfile.is_open())
cout << "Could not open file" << endl;
else
{
myfile.get(text, 100);
ptr = strtok(text, " ,.-?!");
while(ptr != NULL)
{
nr++;
cout << ptr << endl;
ptr = strtok(NULL, " ,.-?!");
grade_list.insert(ptr);
i++;
}
}
cout << "\nAveti " << nr << " cuvinte." << endl;
return 0;
}