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

📄 prg11_6.cpp

📁 这是数据结构和算法的国外经典书籍.清华大学出版社出版的<数据结构C++语言描述-应用模板库STL>陈君 译 英文名称是Data Structures with C++ Using STL.
💻 CPP
字号:
#ifdef _MSC_VER
// disable warning messages that identifier was truncated
// to 'number' characters in the debug information
#pragma warning(disable:4786)
#endif	// _MSC_VER

// File: prg11_6.cpp
// the program initializes the multiset softwareSet with software
// product entries. the multiset has duplicates, since some of the
// products have the same vendor. the program outputs the number of
// software products and prompts for a vendor. after outputting the
// number of products for the vendor, the program outputs the set
// of products in the format
//    Vendor: company
//        Products: name1  name2   . . .

#include <iostream>
#include <string>
#include <set>

#include "d_prod.h"

using namespace std;

int main()
{
	product prodArr[] = 
		{product("Microsoft", "Visual C++"), product("Inprise", "C++ Builder"),
		 product("Microsoft", "Word"), product("Ramsoft", "MAS"),
		 product("Inprise", "J Builder"), product("Adobe", "Photoshop"),
		 product("Microsoft", "Excel"), product("Adobe", "Illustrator")};
	int prodArrSize = sizeof(prodArr)/sizeof(product);
	// softwareSet is a multiset of product objects
	multiset<product> softwareSet(prodArr, prodArr+prodArrSize);

	// an iterator and iterator pair for the output
	multiset<product>::iterator iter;
	pair<multiset<product>::iterator, multiset<product>::iterator> p;

	// vendor name used for input
	string vendorName;

	cout << "Number of software products = " << softwareSet.size()
		  << endl << endl;

	// prompt for the name of a vendor
	cout << "Enter a vendor name: ";
	cin >> vendorName;
	cout << endl;
	cout << "There are " << softwareSet.count(product(vendorName, ""))
		  << " " << vendorName << " products in the set" << endl << endl;

	// loop outputs the products in the ascending order of vendor (company)
	iter = softwareSet.begin();
	while (iter != softwareSet.end())
	{
		// determine the range of products with company (*iter).getCompany()
		p = softwareSet.equal_range(*iter);
		cout << "Vendor: " << (*iter).getCompany() << endl
			  << "    Products: ";
		// output each product for the company
		while (iter != p.second)
		{
			cout << (*iter).getName() << "  ";
			iter++;
		}
		cout << endl;
	}

	return 0;
}

/*
Run:

Number of software products = 8

Enter a vendor name: Microsoft

There are 3 Microsoft products in the set

Vendor: Adobe
    Products: Photoshop  Illustrator
Vendor: Inprise
    Products: C++ Builder  J Builder
Vendor: Microsoft
    Products: Visual C++  Word  Excel
Vendor: Ramsoft
    Products: MAS
*/

⌨️ 快捷键说明

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