How to read words from text file into a map in C++

1 Answer

0 votes
#include <iostream>
#include <map>
#include <string>
#include <fstream>

using std::cout;
using std::endl;
using std::map;
using std::string;

int main()
{
	map<string, int> mp;
	std::ifstream in("d:\\data.txt");

	string word;
	while (in >> word)
		mp[word] = 1 + word[0];

	in.close();

	typedef map<string, int>::const_iterator CI;

	for (CI it = mp.begin(); it != mp.end(); it++)
		cout << it->first << " : " << it->second << endl;

	return 0;
}

/*
run:

c# : 100
c++ : 100
java : 107
php : 113
python : 113

*/

 



answered May 7, 2018 by avibootz

Related questions

1 answer 181 views
1 answer 162 views
1 answer 159 views
1 answer 330 views
1 answer 182 views
1 answer 258 views
1 answer 250 views
...