Tuesday, July 24, 2007

STL samples with pointers

Today I developed the STL map pointer spike application...


#include <map>
using namespace std;

typedef map <int,int* > IntMap;
typedef map < int,int* >::iterator IntIter;
void MapPtrTesting()
{
IntMap m_Map;
IntIter iter;

int *p = new int();
*p = 10;
m_Map[0] = p;
iter = m_Map.find(1);
if( iter == m_Map.end())
{
printf("\n Invalid Search...");
return;
}
printf("\n Find the values :%d", *(iter->second));
for(iter = m_Map.begin(); iter != m_Map.end(); iter++)
{
printf( " Value of %d at ",(*iter).first);
printf(" Value is %d",*((*iter).second));
delete (*iter).second;
}
m_Map.clear();
}
int main (int argc, char* argv[])
{
printf("Hello World!\n");
MapPtrTesting();
return 0;
}


STL List sample application :


#include "stdafx.h"
#include <list >
using namespace std;

typedef list<int* > IntegerPtr;
list<int*>::iterator listIter;
void IntegerPtrTesting()
{
IntegerPtr *m_pIntegerPtr = new IntegerPtr();
int* p = new int();
*p = 10;
m_pIntegerPtr->insert(++m_pIntegerPtr->begin(),p);

for(listIter = m_pIntegerPtr->begin(); listIter != m_pIntegerPtr->end(); ++listIter)
{
printf("Value is %d",*(*listIter));
}
for(listIter = m_pIntegerPtr->begin(); listIter != m_pIntegerPtr->end(); ++listIter)
{
delete (*listIter);
*listIter = NULL;
}
m_pIntegerPtr->clear();
if(m_pIntegerPtr)
{
delete m_pIntegerPtr;
m_pIntegerPtr = NULL;
}

}


int main(int argc, char* argv[])
{

printf("Hello World!\n");


IntegerPtrTesting();
return 0;
}

No comments: