写多页映射文件快捷方式c ++

 孤独游侠1976_127 发布于 2023-02-10 11:18

我使用以下代码来解决单词的频率://由Briana Morrison编写的Owen程序

//#pragma warning (disable : 4786)
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

// program assumes that the filename is the only thing passed into program
// if you are using standard argc and argv, then arguments to main should change, and uncomment 
//   first line.
int main(int argc, char * argv[])
{
    string filename(argv[1]);
  //  string filename;

    //cout << "Enter filename" << endl;
    //cin >> filename;

    ifstream infile(filename.c_str());
    //ifstream infile("poe.txt");

    string word;
    bool debug = false; // for debugging purposes
    int count = 0;      // count of words for debugging

    // create a map of words to frequencies
    map > words;
    // create a multimap of frequencies to words
    multimap > freq;

    // loop while there is input in the file  
    infile >> word; //priming read
    while (infile)
    {
       count++;
       // convert word to lowercase
       for (int i = 0; i < word.length(); i++)
           if ('A' <= word[i] && word[i] <= 'Z')
              word[i] = tolower(word[i]);

        if (debug) cout << word << endl;
        // if word not found, add to map, otherwise increment count
        if (words.find(word) != words.end())
        {
            words[word]++;
            if (debug) cout << word << " found and count incremented to " << words[word] << endl;
        }
        else
        {
            words[word] = 1;
            if (debug) cout << word << " not found and count incremented to " << words[word] << endl;
        }

        infile >> word;
    }

    if (debug) cout << "count is " << count << " and map has " << words.size() << endl;

    // now go through map and add everything to multimap...words still in alphabetical order
    map >::iterator it = words.begin();
    for (it = words.begin(); it != words.end(); it++)
    {
        pair p(it->second, it->first);
        freq.insert(p);
    }

    if (debug) cout << "map has " << words.size() << " and multimap has " << freq.size() << endl;

    ofstream outfile("myout.txt");

    multimap >::iterator myit=freq.begin();
    for (myit = freq.begin(); myit != freq.end(); myit++)
    {
        outfile << myit->first << "\t" << myit->second << endl;
    }
    outfile.close();

  return 0;
}

我认为问题不在这里

当我将文字写入文件时,每次迭代都会变慢,为什么?

        ofstream outfile("myout.txt");

        multimap >::iterator myit=freq.begin();
        for (myit = freq.begin(); myit != freq.end(); myit++)
        {
            outfil<< myit->first << "\t" << myit->second << endl;
        }
       outfile.close();

如何以快速的方式将多图写入文件?

1 个回答
  • 您可以使用'\n'而不是std::endl避免为每一行冲洗它.

    outfil << myit->first << '\t' << myit->second << '\n';
    

    2023-02-10 11:21 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有