這兩個類是ACE提供的集合容器類。集合容器類也分爲有邊界和無邊界之分。
// ACEset.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "ace/OS_Memory.h"
#include "ace/Log_Msg.h"
#include "ace/Containers.h"
//有限制大小的集合
int runBoundedSet()
{
ACE_TRACE ("SetExample::runBoundedSet");
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Using a bounded set\n")));
ACE_Bounded_Set<int> bset(10);
//插入連詞,驗證集合的性質,同一個集合中插入兩個相同的元素會失敗
int elem[10];
for (int i = 0; i < 10; i++)
{
elem[i] = i;
//Inserting two copies of the same element isn't allowed.
bset.insert (elem[i]);
if (bset.insert(elem[i]) == 1)
{
ACE_DEBUG ((LM_INFO, ACE_TEXT("insert elements %d existed in set\n"), elem[i]));
}
}
//查找元素 5 和 8
int elem1 = 5;
int elem2 = 8;
if (!bset.find (elem1) && !bset.find (elem2))
{
ACE_DEBUG ((LM_INFO, ACE_TEXT ("The elements %d and %d are ") ACE_TEXT ("in the set!\n"), elem1, elem2));
}
//刪除前邊5個原色
for (int j = 0; j < 5; j++)
{
bset.remove (elem[j]); // Remove the element from the set.
ACE_DEBUG((LM_DEBUG, ACE_TEXT ("%d "), elem[j]));
}
printf("\n---------------------------\n");
//然後再查找 0 和 49
if ((bset.find (elem[0]) == -1) && (bset.find (elem[4]) == -1))
{
ACE_DEBUG ((LM_INFO, ACE_TEXT ("The elements %d and %d are ") ACE_TEXT ("NOT in the set!\n"), elem[0], elem[4]));
}
return 0;
}
//沒有限制集合大小類型的集合,並且元素是指針
int runUnboundedSet ()
{
ACE_TRACE ("SetExample::runUnboundedSet");
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Using an unbounded set.\n")));
int *a = NULL;
int *b = NULL;
ACE_Unbounded_Set<int*> uset;
for (int m = 0; m < 10; m++)
{
int *elem = new int;
//特殊記錄一下指針的值,爲了查找。
if (m == 0)
{
a = elem;
}
else if(m==9)
{
b = elem;
}
*elem = m;
uset.insert (elem);
}
//查找的時候一定注意這塊,內存匹配查找,否則要自己實現查找函數
//驗證元素是否在集合
if (!uset.find(a) && !uset.find(b))
{
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Found the elements\n")));
}
// Iterate and destroy the elements in the set.
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Deleting the elements\n")));
//遍歷
ACE_Unbounded_Set_Iterator<int*> iter (uset);
for (iter = uset.begin (); iter != uset.end (); iter++)
{
int* elem = (*iter);
printf("%d ", *elem);
delete elem;
}
printf("\n-------------------------------------\n");
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
runBoundedSet();
runUnboundedSet();
getchar();
return 0;
}