Extending a program to count line frequency

I have this simple program to count the word frequency:

#include <iostream>
#include <map>
#include <cctype>
#include <string>
using namespace std;
int main(void) {
  map<string, int> words;
  map<string, int>::iterator i;
string s;
while (cin >> s) {
    transform(s.begin(), s.end(), s.begin(), ::tolower);
for (i=words.begin(); i != words.end(); i++)
    cout << i->first << " " << i->second << endl;
return 0;

Now how do I make it to count line frequency instead of word frequency. Each line is separated by '\n' new line character in the input.


If you want to count the total lines, then its very simple:

 size_t countLine = 0;
 while(std::getline(std::cin, s)) countLine++;

But if you really want to count frequency of same line, then in your code, replace

string s;


line s;

where line is defined as:

struct line : std::string
    friend std::istream & operator >> (std::istream &in, line &ln) 
        return std::getline(in, ln);

Or, you can make it generic:

template<typename CharT, typename Traits = std::char_traits<CharT>, typename Allocator = std::allocator<CharT>>
struct basic_line : public std::basic_string<CharT, Traits, Allocator>
    friend std::basic_istream<CharT, Traits> & operator >> (std::basic_istream<CharT, Traits > &in, basic_line<CharT, Traits, Allocator> &ln) 
        return std::getline(in, ln);

then define these typedefs:

typedef basic_line<char>    line;
typedef basic_line<wchar_t> wline;

Need Your Help

cross platform way to explore the containing folder of a file

java browser folder windows-explorer

is there functionality in java to open the containing folder of a file across any platform. I can accomplish this in windows by running Runtime rt = Runtime.getRuntime(); Process proc =

How can I select all Eloquent models from a MySQL database inserted today, within the last week or the last month?

php mysql database laravel laravel-4

I have an Eloquent Model (Test) associated with a MySQL table called tests, with the following structure:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.