References
Category:1991 debut albums
Category:Jungle (band) albums
Category:German-language albumsQ:
Performing word count in a file in C++
I am performing a word count of a file using C++. I am getting a huge number of words.
I am running it as follows:
// Loop through each line in the file and remove the delimiters and whitespace
while (getline(ifs, buffer, '
'))
{
buffer = buffer.substr(0, buffer.find_first_of('
'));
std::vector tokens;
istringstream iss(buffer);
std::copy(std::istream_iterator(iss), std::istream_iterator(),
std::back_inserter(tokens));
int count = 1;
for(std::string const& token : tokens)
{
if(token.find_first_not_of(" ")!= std::string::npos)
{
count++;
}
}
totalCount = totalCount + count;
}
I know that the number of words is increasing, because when I run a word count on another file which I know has a few hundred thousand words the word count is 549 (on a about 5MB file).
Is there anything I can do to improve this? I know I could do it in a much faster way by using the boost library, but I need it to be done in C++
A:
Instead of the
std::copy(std::istream_iterator(iss),
std::istream_iterator(),
std::back_inserter(tokens));
loop you could replace it with:
std::istringstream iss(buffer);
std::vector
Related links:
Comments