#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <string>
#include <map>
#include <set>
 
using namespace std;
 
int main()
{
	map<string, double> scores;
	scores["Tom"] = 90;
	scores["Diana"] = 86;
	scores["Harry"] = 100;
	cout <<"Diana " << scores["Diana"]<<endl;
 
 
	map<string, vector<string>> food;
	vector<string> mark_diet;
	mark_diet.push_back("Spagetti");
	mark_diet.push_back("Mac and Cheese");
	mark_diet.push_back("Hogies");
	mark_diet.push_back("Bagels");
	food["Mark"] = mark_diet;
	vector<string> kevin_diet;
	kevin_diet.push_back("Steak");
	mark_diet.push_back("Steak");
	kevin_diet.push_back("Rice Bread");
	kevin_diet.push_back("Steak");
	kevin_diet.push_back("Salad");
	food["Kevin"] = kevin_diet;
	food["Joe"].push_back("bbq");
	food["Joe"].push_back("fries");
 
	cout << "Let's look at the entire food map:\n";
	for (map<string,vector<string>>::iterator eater = food.begin(); eater != food.end(); eater++) {
		cout << eater->first << " can eat:\n";
		for (vector<string>::iterator food_it = eater->second.begin(); food_it != eater->second.end(); food_it++) {
			cout << *food_it << " ";
		}
	cout << endl;
	}
 
 
	cout < "Random items for Kevin:\n";
	for (vector<string>::iterator food_it = food["Kevin"].begin(); food_it != food["Kevin"].end(); food_it++) {
		cout << *food_it << " ";
	}
	cout << endl;
 
 
 
 
	// Lets pick a few random meals for Kevin
	int menu;
	for(int i = 0; i < 10; i++) {
		menu = rand()*food["Kevin"].size()/RAND_MAX; 
		cout << "Picked Random Menu for Kevin "<<menu<<" " << food["Kevin"][menu]<<endl;
	}
 
	menu = rand()*food["Mark"].size()/RAND_MAX; 
	cout << "Picked Random Menu for Mark "<<menu<<" " << food["Mark"][menu]<<endl;
 
	// Now lets look at sets
	set <string> foodstuff;
	foodstuff.insert("Steak");
	foodstuff.insert("Steak");
	foodstuff.insert("Steak");
	foodstuff.insert("Steak");
	foodstuff.insert("Spagetti");
	foodstuff.insert("Spagetti");
	foodstuff.insert("Spagetti");
	foodstuff.insert("Toast");
	foodstuff.insert("Toast");
	for(set <string>::iterator nm = foodstuff.begin(); nm != foodstuff.end(); nm++) {
		cout << *nm<<endl;
	}
	cout <<"Count for Steak "<<foodstuff.count("Steak")<<endl;
	cout <<"Count for Brats "<<foodstuff.count("Brats")<<endl;
 
	multiset <string> foodstuff2;
	foodstuff2.insert("Steak");
	foodstuff2.insert("Steak");
	foodstuff2.insert("Steak");
	foodstuff2.insert("Steak");
	foodstuff2.insert("Spagetti");
	foodstuff2.insert("Spagetti");
	foodstuff2.insert("Spagetti");
	foodstuff2.insert("Toast");
	foodstuff2.insert("Toast");
	for(multiset <string>::iterator nm = foodstuff2.begin(); nm != foodstuff2.end(); nm++) {
		cout << *nm<<endl;
	}
	cout <<"MultiCount for Steak "<<foodstuff2.count("Steak")<<endl;
	cout <<"MultiCount for Brats "<<foodstuff2.count("Brats")<<endl;
	system("pause");
}

Modern version:

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>
#include <string>
#include <map>
#include <set>
 
using namespace std;
 
int main()
{
	map<string, double> scores;
	scores["Tom"] = 90;
	scores["Diana"] = 86;
	scores["Harry"] = 100;
	cout << "Diana " << scores["Diana"] << endl;
 
 
	map<string, vector<string>> food;
	vector<string> mark_diet;
	mark_diet.push_back("Spagetti");
	mark_diet.push_back("Mac and Cheese");
	mark_diet.push_back("Hogies");
	mark_diet.push_back("Bagels");
	food["Mark"] = mark_diet;
	vector<string> kevin_diet;
	kevin_diet.push_back("Steak");
	mark_diet.push_back("Steak");
	kevin_diet.push_back("Rice Bread");
	kevin_diet.push_back("Steak");
	kevin_diet.push_back("Salad");
	food["Kevin"] = kevin_diet;
	food["Joe"].push_back("bbq");
	food["Joe"].push_back("fries");
 
	cout << "Let's look at who is in the food map:\n";
	for (map<string, vector<string>>::iterator eater = food.begin(); eater != food.end(); eater++) {
		cout << eater->first << endl;
	}
	cout << "Let's look at the entire food map:\n";
	for (map<string, vector<string>>::iterator eater = food.begin(); eater != food.end(); eater++) {
		cout << eater->first << " can eat:\n";
		for (vector<string>::iterator food_it = eater->second.begin(); food_it != eater->second.end(); food_it++) {
			cout << *food_it << " ";
		}
		cout << endl;
	}
	cout << "Let's look at the entire food map: (easier)\n";
	for (auto eater : food) {
		cout << eater.first << " can eat:\n";
		for (auto food_it : eater.second) {
			cout << food_it << " ";
		}
		cout << endl;
	}
 
 
 
	// Lets pick a few random meals for Kevin
	int menu;
	for (int i = 0; i < 10; i++) {
		menu = rand()%food["Kevin"].size();
		cout << "Picked Random Menu for Kevin " << menu << " " << food["Kevin"][menu] << endl;
	}
 
	menu = rand()%food["Mark"].size();
	cout << "Picked Random Menu for Mark " << menu << " " << food["Mark"][menu] << endl;
 
	// Now lets look at sets
	cout << "\nSET:\n";
	set <string> foodstuff;
	foodstuff.insert("Steak");
	foodstuff.insert("Steak");
	foodstuff.insert("Steak");
	foodstuff.insert("Steak");
	foodstuff.insert("Spagetti");
	foodstuff.insert("Spagetti");
	foodstuff.insert("Spagetti");
	foodstuff.insert("Toast");
	foodstuff.insert("Toast");
	for (set <string>::iterator nm = foodstuff.begin(); nm != foodstuff.end(); nm++) {
		cout << *nm << endl;
	}
	cout << "easier\n";
	for (auto nm : foodstuff) {
		cout << nm << endl;
	}
	cout << "Count for Steak " << foodstuff.count("Steak") << endl;
	cout << "Count for Brats " << foodstuff.count("Brats") << endl;
 
	cout << "\nSET:\n";
	multiset <string> foodstuff2;
	foodstuff2.insert("Steak");
	foodstuff2.insert("Steak");
	foodstuff2.insert("Steak");
	foodstuff2.insert("Steak");
	foodstuff2.insert("Spagetti");
	foodstuff2.insert("Spagetti");
	foodstuff2.insert("Spagetti");
	foodstuff2.insert("Toast");
	foodstuff2.insert("Toast");
	for (auto nm : foodstuff2) {
		cout << nm << endl;
	}
	cout << "MultiCount for Steak " << foodstuff2.count("Steak") << endl;
	cout << "MultiCount for Brats " << foodstuff2.count("Brats") << endl;
	system("pause");
}
cs-142/maps-and-sets.txt · Last modified: 2015/12/08 16:19 by kseppi
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0