⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maps.cpp

📁 斯坦福Energy211/CME211课《c++编程——地球科学科学家和工程师》的课件
💻 CPP
字号:
// Program that demonstrates maps and multimaps#include <iostream>#include <fstream>#include <map>using namespace std;// Use typedef to shorten type names// Map with string keys and string valuestypedef map<string, string> namemap;// Iterator for namemaptypedef map<string, string>::iterator name_iter;// Multimap with string keys and string valuestypedef multimap<string, string> coursemap;// Iterator for coursemaptypedef multimap<string, string>::iterator course_iter;int main() {		// File names.txt contains first and last names	// of students, and which course they are enrolled in	// (energy211 or cme211)	ifstream namefile("names.txt");	// map of last name to first name	namemap names;	// multimap of course to last names	coursemap courses;	// Keep reading from the file until no more are left	while ( !namefile.eof() )	{		string first_name;		string last_name;		string course;		// Read one student's info		namefile >> first_name;		namefile >> last_name;		namefile >> course;		// Add entry (last_name,first_name) to names map		names[last_name] = first_name;		// Add entry (course,last_name) to courses multimap		courses.insert( make_pair( course, last_name ) );	}		// Print out all entries in names map	// Use begin() iterator to initialize iterator i that	// iterates over all elements	for ( name_iter i(names.begin()); i != names.end(); i++ )	{		// Iterator points to a pair object consisting of		// the name and the value.  Syntax i->first is 		// equivalent to (*i).first.  -> operator accesses		// members from pointers to objects.  A pair object		// has members first and second.  In this case,		// first is the key and second is the value		cout << i->first << ", " << i->second << endl;	}	// This looks up a last name in the names map and returns	// the corresponding value (first name), if found	cout << "Enter a last name: ";	string last;	cin >> last;	// Use [] operator to retrieve value from key	string first = names[last];	// If [] doesn't find a value, it returns a default	// value, which is the empty string "" in this case	if ( first.size() > 0 )		cout << "First name is " << first << endl;	else		cout << "Name not found" << endl;		// This looks up a course in the course multimap and	// prints all of the corresponding values	cout << "Enter a course: ";	string course;	cin >> course;	cout << "Students enrolled in " << course << ":" << endl;	// Instead of using [], since there can be more than	// one value, we use the functions lower_bound() and	// upper_bound() to iterate over all of the values.	// lower_bound() plays a similar role to begin() for	// iterating over all elements, and upper_bound() plays	// a similar role to end()	course_iter begin( courses.lower_bound( course ) );	course_iter end( courses.upper_bound( course ) );	// Once again, the iterator points to a pair	for ( ; begin != end; begin++ )		cout << begin->second << ", " << 			names[begin->second] << endl;	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -