当前位置: 首页>>代码示例>>C++>>正文


C++ Bag::Discard方法代码示例

本文整理汇总了C++中Bag::Discard方法的典型用法代码示例。如果您正苦于以下问题:C++ Bag::Discard方法的具体用法?C++ Bag::Discard怎么用?C++ Bag::Discard使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Bag的用法示例。


在下文中一共展示了Bag::Discard方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main()
{
	//test creation of multiple types
	Bag<int> IntBag;
	Bag<double> DoubleBag;
	Bag<string> StringBag;
	bool passed;

	for (int j = 0; j < 50000; j++) {
		IntBag.AddItem(j);
		passed = true;
	}
	
	for (int j = 0; j < 50000; j++){
		if (!IntBag.IsPresent(j)){
			cout << "Error: Lost item " << j << endl;
			passed = false;
		}
	}

	if (passed) {
		cout << "Retained all items \n";
	}
	else
		cout << "Failed test\n";
		passed = true;

	for (int j = 50000; j < 50050; j++){
		if (IntBag.IsPresent(j) || IntBag.IsPresent(j*-1)){
			passed = false;
			cout << "Found extra item: " << j << '\n';
		}
	}

	if (passed) {
		cout << "No extra items \n";
	}
	else
		cout << "Failed extra-item test\n";
		passed = false;

	try{
		IntBag.Discard(-1);
	}
	
	catch (BagException){
		passed = true;
		cout << "Raised expected exception on discarding nonexistent item\n";
	}

	if (passed) {
		cout << "Handled discard of non-existent item \n";
	}
	else
		cout << "Failed discard test\n";
	
	IntBag.Discard(12);
	
	if (IntBag.IsPresent(12)) {
		cout << "Failed to discard item\n";
	}
	else
		cout << "Discard-item test passed.\n";
	
	IntBag.Clear();
	
	if (!IntBag.IsEmpty()) {
		cout << "Failed to clear Bag\n";
	}
	else
		cout << "Cleared Bag OK. \n";
	
	if (IntBag.IsEmpty() && IntBag.Size() != 0) {
		cout << "Error: Reports empty but nonzero count.\n";
	}

	system("pause");
	return 0;
}
开发者ID:zfleeman,项目名称:program6,代码行数:79,代码来源:Main.cpp


注:本文中的Bag::Discard方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。