在地图中插入文件
Insert file in map
我是 c++ 的新手。我有两个.txt文件。一个是我想在另一个中找到的 3 个关键词 .txt 文件女巫是一篇长文本。我正在尝试将这两个文件插入地图,但我不知道如何。
我试过这个 num.insert(pair<string,string>(clef,index));
但它不起作用。请帮助我。
提前致谢:)
@Ari0nhh这是我的所有代码:
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <iterator>
using namespace std;
ofstream index( "index.txt", ios::out | ios::app);
int main()
{
map <string, string> num;
map <string,string> ::iterator it;
int ctrLigne = 1;
int ctrPage=1;
int ctr=1;
ifstream docum ("docum.txt", ios::in);
ifstream clef ("clef.txt", ios::in);
/* what I have tried but did not work
while (docum >> clef >> index){
num[clef] = index;
}
num.insert(pair<string,string>(clef,index));
num.insert(clef, index);
*/
if (docum)
{
string ligne;
while(getline(docum, ligne))
{
cout << ligne << " Ligne : " << ctrLigne << " Page : " << ctrPage << endl;
if(ctr==4)
{
ctr=0;
ctrPage++;
}
ctrLigne++;
ctr++;
}
docum.close();
clef.close();
system("pause");
return 0;
}
您可以使用简单的映射来执行此操作,并将文件的内容存储为长字符串。
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
// your code goes here
string fileName="myFile.txt";
string myFilecontent="All The secrets of the worldn Line 1n Line2";//includes line breaks
map<string,string> fileMap;
fileMap[fileName]=myFilecontent;
cout<<fileMap[fileName];
return 0;
}
输出:
世界上所有的秘密
1号线
2号线
希望有帮助,
相关文章:
- 逐行读取文件,并将数据插入变量和数组中
- 如何在C++中.txt文件的开头插入字符串
- 从人员矢量插入和读取文件时出错
- 尝试使用 EvtSetChannelConfigProperty() 函数更新最大事件日志文件大小时插入的错误值
- 在另一个字符串中插入文件扩展名之前的字符串
- 如何在文本文件中间插入字符
- C++2D网格数组,从文件中读取和插入数组值
- 在控制台上输出我从文件中读取的整数,将程序插入无限循环
- 在排序链表中插入结构并将其写入二进制文件
- 如何在 c++ 中将文件中的无空格插入器读取到数组中?
- 为什么 MFC .rc 文件有时会在末尾手动插入 \0?
- C++,如何从文件中读取数据并插入容器:地图
- 在文本文件的某些行和列上插入值
- 将文本文件插入2D矢量C
- 如何在使用 c++/COM 发送时在电子邮件中插入/嵌入图像文件(.png)
- 将文本文件单词插入 AVL
- 在文本文件中在特定行中插入线
- 将整数从文本文件插入到整数数组
- 使用c++将值从文件插入到函数中
- 在Qt中从csv文件插入值到矢量时出错