gpt4 book ai didi

c++使用 map 对记事本字符进行排序

转载 作者:行者123 更新时间:2023-11-27 22:56:32 26 4
gpt4 key购买 nike

我希望通过使用映射来查找文本文件中每个字符的数量来对文本文件进行排序。我想我已经很接近了。

void CharStatistic(string filename)
{
ifstream infile;
infile.open(filename);
char x;
int i;
map <char, int> count;
while (infile >> x)
{
count[x]++;
for (auto it = count.begin(); it != count.end(); it++)
cout << it->first << it->second << endl;

}
}

这是我正在阅读的文件中的实际内容。

Computer science is the scientific and practical approach to computation and its applications. It is the systematic study of the feasibility, structure, expression, and mechanization of the methodical procedures (or algorithms) that underlie the acquisition, representation, processing, storage, communication of, and access to information, whether such information is encoded as bits in a computer memory or transcribed in genes and protein structures in a biological cell. An alternate, more succinct definition of computer science is the study of automating algorithmic processes that scale. A computer scientist specializes in the theory of computation and the design of computational systems.

Its subfields can be divided into a variety of theoretical and practical disciplines. Some fields, such as computational complexity theory (which explores the fundamental properties of computational and intractable problems), are highly abstract, while fields such as computer graphics emphasize real-world visual applications. Still other fields focus on the challenges in implementing computation. For example, programming language theory considers various approaches to the description of computation, while the study of computer programming itself investigates various aspects of the use of programming language and complex systems. Human-computer interaction considers the challenges in making computers and computations useful, usable, and universally accessible to humans.

如果可能的话,我还想弄清楚如何打印出出现次数最多的字符。一如既往地非常感谢您的帮助!

最佳答案

在这种情况下我不会使用 map 。只有 256 个值,一个简单的数组只占用 1 KB(假设每个计数 4 个字节)。即使输入可能是稀疏的,所以该数组的某些元素未被使用,只有 1 K,您不太可能通过使用不同的结构获得任何东西。

完成计数后,转换为可以按计数排序的结构数组非常容易(或者,如果您真的只需要计数最大的那个,只需对那个进行线性扫描) .

关于c++使用 map 对记事本字符进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32470803/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com