我正在从文件中读取,并将这些文字作为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;
}
你可以插入使用INSERT命令来实现。看看这里的例子http://www.cplusplus.com/reference/map/map/insert/ – Pradheep
http://www.cplusplus.com/reference/map/map/operator%5B%5D/ – banuj