用于查找文件中单词出现的程序 [英] Program to find occurences of a word in a file

查看:98
本文介绍了用于查找文件中单词出现的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

对于我最近接受的一次采访,我获得了离线

编程测验。

30分钟我有用C ++编写代码来计算给定文件中出现的每个唯一单词的次数



即使在测验结果出来之后,我也尝试了最好的水平解决方案

但是cudnt找到了一个有效的解决方案。 :(


这就是我所做的。


1.读取文件中的所有单词并将它们存储在矢量中

容器。

2.使用最有效的
算法(如Quicksort,Mergesort等)对矢量容器中的单词进行排序

3.然后找到重复和出现很容易。


我被要求在没有排序的情况下这样做。

有更好的方法吗?

Neehar

Hello
For one of the interviews I took recently, I was given an offline
programming quiz.
In 30 minutes I had to write code in C++ to counts the number of times
each unique word appears in a given file.
I tried my level best even after the quiz to come up with a solution
but cudnt find an efficient one. :(

This is what I did.

1. Read all the words from the file and store them in a vector
container.
2. Sort the words in the vector container using most efficient
algorithm ( like Quicksort, Mergesort etc)
3. Then finding duplicates and occurences is very easy.

I was asked to do this without sorting.
Is there a better way to do this?
Neehar

推荐答案

Neehar写道:
Neehar wrote:

你好

对于我最近接受的一次采访,我获得了离线

编程测验。

30分钟内我必须编写代码C ++计算给定文件中出现的每个唯一单词的次数



即使在测验后提出解决方案我也尝试了我的水平

但是cudnt找到了一个有效的。:(


这就是我做的。


1.读取文件中的所有单词并将它们存储在一个向量

容器中。

2.使用大多数对向量容器中的单词进行排序高效

算法(如Quicksort,Mergesort等)

3.然后查找重复和出现非常容易。


我是要求在没有排序的情况下这样做。

有更好的方法吗?
Hello
For one of the interviews I took recently, I was given an offline
programming quiz.
In 30 minutes I had to write code in C++ to counts the number of times
each unique word appears in a given file.
I tried my level best even after the quiz to come up with a solution
but cudnt find an efficient one. :(

This is what I did.

1. Read all the words from the file and store them in a vector
container.
2. Sort the words in the vector container using most efficient
algorithm ( like Quicksort, Mergesort etc)
3. Then finding duplicates and occurences is very easy.

I was asked to do this without sorting.
Is there a better way to do this?



std :: map< std :: string,int>

std::map<std::string,int>


2007-11-19 23 :29:38 -0500,Neehar< ne ************ @ gmail.comsaid:
On 2007-11-19 23:29:38 -0500, Neehar <ne************@gmail.comsaid:

Hello

对于我最近接受的一次采访,我获得了离线

编程测验。

30分钟内我不得不用C ++编写代码来计算数字时间

每个唯一的单词出现在给定的文件中。

即使在测验之后我也尝试了我的水平以获得解决方案

但是cudnt找到了一个有效的。 :(


这就是我所做的。


1.读取文件中的所有单词并将它们存储在矢量中

容器。

2.使用最有效的
算法(如Quicksort,Mergesort等)对矢量容器中的单词进行排序

3.然后找到重复和出现很容易。


我被要求在没有排序的情况下这样做。

有更好的方法吗?
Hello
For one of the interviews I took recently, I was given an offline
programming quiz.
In 30 minutes I had to write code in C++ to counts the number of times
each unique word appears in a given file.
I tried my level best even after the quiz to come up with a solution
but cudnt find an efficient one. :(

This is what I did.

1. Read all the words from the file and store them in a vector
container.
2. Sort the words in the vector container using most efficient
algorithm ( like Quicksort, Mergesort etc)
3. Then finding duplicates and occurences is very easy.

I was asked to do this without sorting.
Is there a better way to do this?



不确定它是否更好,但你不能使用std :: map吗?


std :: map< string,int wordlist;

string word;


while((word = readword(cin))!="" )

{

wordlist [word] ++;

}

Not sure if it''s better, but can''t you use a std::map instead?

std::map<string, int wordlist;
string word;

while ((word = readword(cin)) != "")
{
wordlist[word] ++;
}


>


Neehar
>

Neehar



-


-kira

--

-kira


Neehar写道:
Neehar wrote:

你好

对于我最近接受的一次采访,我获得了离线

编程测验。

在30分钟内,我不得不用C ++编写代码来计算次数

每个唯一的单词出现在给定的文件中。

我甚至在测验后尝试了我的水平以获得解决方案

但cudnt find一个有效的。 :(


这就是我所做的。


1.读取文件中的所有单词并将它们存储在矢量中

容器。

2.使用最有效的
算法(如Quicksort,Mergesort等)对矢量容器中的单词进行排序

3.然后找到重复和出现很容易。


我被要求在没有排序的情况下这样做。

有更好的方法吗?
Hello
For one of the interviews I took recently, I was given an offline
programming quiz.
In 30 minutes I had to write code in C++ to counts the number of times
each unique word appears in a given file.
I tried my level best even after the quiz to come up with a solution
but cudnt find an efficient one. :(

This is what I did.

1. Read all the words from the file and store them in a vector
container.
2. Sort the words in the vector container using most efficient
algorithm ( like Quicksort, Mergesort etc)
3. Then finding duplicates and occurences is very easy.

I was asked to do this without sorting.
Is there a better way to do this?



std :: map< std :: string,unsigned long count;

std :: string word;

while(std :: cin> word){

++ count [word];

}

现在,有问题决定一个单词是什么。上面只是将

留给运营商>> ;.

最好


Kai-Uwe Bux

std::map< std::string, unsigned long count;
std::string word;
while ( std::cin >word ) {
++ count[ word ];
}
Now, there are issues on deciding what a word is. The above just leaves that
to operator>>.
Best

Kai-Uwe Bux


这篇关于用于查找文件中单词出现的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆