在地图中插入文件

Insert file in map

本文关键字:文件 插入 地图      更新时间:2023-10-16

我是 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号线

希望有帮助,