我使用以下代码来确定单词的频率: //由Briana Morrison撰写的欧文节目
//#pragma warning (disable : 4786)
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
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<string, int, less<string> > words;
// create a multimap of frequencies to words
multimap<int, string, greater<int> > 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<string, int, less<string> >::iterator it = words.begin();
for (it = words.begin(); it != words.end(); it++)
{
pair<int, string> 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<int, string, greater<int> >::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<int, string, greater<int> >::iterator myit=freq.begin();
for (myit = freq.begin(); myit != freq.end(); myit++)
{
outfil<< myit->first << "\t" << myit->second << endl;
}
outfile.close();
如何以快速方式将多重映射写入文件?
答案 0 :(得分:5)
您可以使用'\n'
代替std::endl
,以避免为每一行冲洗它。
outfil << myit->first << '\t' << myit->second << '\n';
答案 1 :(得分:3)
for (myit = freq.begin(); myit != freq.end(); ++myit)
{
outfil<< myit->first << "\t" << myit->second << "\n";
}
这应该更快。
或者您可以缓冲数据并一次性写入所有数据,而不是逐行写入。
答案 2 :(得分:1)
我不明白为什么你的循环每次迭代都会变慢,但请注意你正在使用格式化输出(这是operator<<
所做的),这是众所周知的慢。如果您的字符串不包含空字节,您可以通过ostream::write
编写std::string
来提高代码效率。
outfil << myit->first;
outfil.write( "\t", 1 );
outfil.write( myit->second.c_str(), myit->second.size() );
outfil.write( "\n", 1 );