[ create a new paste ] login | about

Link: http://codepad.org/Xby3mFq6    [ raw code | output | fork ]

C++, pasted on Dec 12:
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <set>
#include <map>
#include <cstdlib>

struct LenCmp {
	bool operator () (const std::string & a, const std::string & b){
		return a.size() > b.size();
	}
};

int main(){
	std::string buf("jingle bells jingle bells jingle all the way");
	/*std::cout << "String: ";
	std::getline(std::cin, buf);
	if ( buf.empty() )
		return 1; */

	std::istringstream ist(buf);
	std::multiset<std::string, LenCmp> ms;
	while ( ist >> buf )
		ms.insert(buf);
	
	size_t maxLen = ms.begin()->size(); 

	std::map<std::string, int> map;
	for ( std::multiset<std::string, LenCmp>::const_iterator i = ms.begin(); i != ms.end() && i->size() == maxLen; ++i )
		map[*i]++;

	std::cout << "The maximum length is " << maxLen << " characters." << std::endl;
	std::cout << "Word(s) of this length:" << std::endl;
	std::cout << "Word                          Count\n--------------------------------------------------" <<std::endl;
	for ( std::map<std::string, int>::const_iterator i = map.begin(); i != map.end(); ++i )
		std::cout << std::left << std::setw(30) << i->first << i->second << std::endl;

	// system("pause");
	return 0;
}


Output:
1
2
3
4
5
The maximum length is 6 characters.
Word(s) of this length:
Word                          Count
--------------------------------------------------
jingle                        3


Create a new paste based on this one


Comments: