コード例 #1
0
ファイル: Multiset2.cpp プロジェクト: shirley-zhou/CS32
void subtract(const Multiset& ms1, const Multiset& ms2, Multiset& result)
{
	//make sure result is empty
	if (result.size() != 0)
	{
		Multiset temp;
		result = temp;
	}

	for (int m1 = 0; m1 < ms1.uniqueSize(); m1++)
	{
		ItemType m1_val;
		int n1 = ms1.get(m1, m1_val);
		for (int m2 = 0; m2 < ms2.uniqueSize(); m2++)
		{
			ItemType m2_val;
			int n2 = ms2.get(m2, m2_val);

			if (m1_val == m2_val && n1 > n2)
			{
				for (int count = n1-n2; count > 0; count--) //insert n1-n2 number of iterations
					result.insert(m1_val);
			}
		}
	}
}
コード例 #2
0
ファイル: Multiset.cpp プロジェクト: eltonkl/cs32
void subtract(const Multiset& ms1, const Multiset& ms2, Multiset& result)
{
	if (&ms2 == &result) //If ms2 and result are the same object, then modifications to result change ms2, so a copy of ms2 has to be made
	{
		Multiset temp = ms2;
		result = ms1;
		//temp and result are NOT the same object, so temp remains the same for each iteration of the loop
		for (int i = 0; i < temp.uniqueSize(); i++)
		{
			ItemType item;
			int count = temp.get(i, item);
			for (int j = 0; j < count; j++)
				result.erase(item);
		}
	}
	else //ms2 and result are NOT the same object, so ms2 remains the same for each iteration of the loop
	{
		result = ms1;
		//If ms1 and result are the same object, nothing happens
		for (int i = 0; i < ms2.uniqueSize(); i++)
		{
			ItemType item;
			int count = ms2.get(i, item);
			for (int j = 0; j < count; j++)
				result.erase(item);
		}
	}
}
コード例 #3
0
ファイル: Multiset.cpp プロジェクト: eltonkl/cs32
void combine(const Multiset& ms1, const Multiset& ms2, Multiset& result)
{
	bool addFirst = false; //addFirst indicates whether the contents of ms1 should be added to result
	bool addSecond = false; //addSecond does the same, but for ms2

	if (&ms1 == &result) //As result is ms1, only need to add ms2 to result to combine the two
		addSecond = true;
	else if (&ms2 == &result) //As result is ms2, only need to add ms1 to result to combine the two
		addFirst = true;
	else //Empty out result, as it may have contained something prior to this
	{
		int size = result.uniqueSize();
		for (int i = 0; i < size; i++)
		{
			ItemType item;
			result.get(0, item);
			result.eraseAll(item);
		}
		addFirst = true;
		addSecond = true;
	}

	if (addFirst) //ms1 and result are NOT the same object, so ms1.uniqueSize() remains the same for each iteration of the loop
	{
		for (int j = 0; j < ms1.uniqueSize(); j++)
		{
			ItemType item;
			int count = ms1.get(j, item);
			for (int k = 0; k < count; k++)
				result.insert(item);
		}
	}
	if (addSecond) //ms2 and result are NOT the same object, so ms2.uniqueSize() remains the same for each iteration of the loop
	{
		for (int j = 0; j < ms2.uniqueSize(); j++)
		{
			ItemType item;
			int count = ms2.get(j, item);
			for (int k = 0; k < count; k++)
				result.insert(item);
		}
	}
}
コード例 #4
0
ファイル: Multiset2.cpp プロジェクト: shirley-zhou/CS32
void combine(const Multiset& ms1, const Multiset& ms2, Multiset& result)
{
	result = ms1;
	for (int k = 0; k < ms2.uniqueSize(); k++) //loop through ms2 and insert into result
    {
		ItemType x;
        int n = ms2.get(k, x);
		for (int count = n; count > 0; count--) //insert all iterations of value
			result.insert(x);
    }
}
コード例 #5
0
ファイル: main.cpp プロジェクト: Xuxue1/Multiset
int main(void)
{
	Multiset ms;
	ms.insert("fennel");
	ms.insert("fennel");
	ms.insert("fenugreek");
	ms.insert("fennel");
	for (int k = 0; k < ms.uniqueSize(); k++)
	{
		string x;
		int n = ms.get(k, x);
		cout << x << " occurs " << n << " times." << endl;
	}
}