http://www.codeproject.com/useritems/MFC.asp
Introduction
This is my first article to this site. I am basically a student so please excuse me for any spelling,gramatical or technical mistakes.This tutorial is for totally noobs who want to start programming in VC++ and have pretty much knowledge of c++.
Ok in this tutorial some of the MFC collection classes( used to store data like strings,nos. etc).
Storage
In normal c/c++ programming what we used to do for storing say 'n' number of integers... either we used to declare and array of integers or we used to make a linked list. If we used array of integers we need to know the dimension of the array beforehand and once we define the size of array we cannot change the dimensions of it during execution.This could be solved using linked lists but they are not easy to understand and maintain .
The solution to this ......... MFC!
MFC Collection Classes
MFC collection classes are categorised briefly as:
I) Array Classes : they are :
1.CByteArray 2.CWordArray 3.CUintArray 4.CStringArray 5.CPtrArray 6.CObArray
II)List Classes :They are-
1.CObList 2.CPtrList 3.CStringList
III)Map Classes:
1.CMapWordToPtr 2.CMapPtrToWord 3.CMapPtrToPtr 4.CMapWordToOb 5.CMapStringToOb 6.CMapStringToPtr 7.CMapStringToString
Note: All classes in MFC begin with the alphabet C
These classes contain data of different types. like the array classes contain arrays. Like the name suggests the CByteArray stores array of bytes, CWordArray -array of words,CUintArray-array of unsigned integers,CObArray-array of object pointers etc.
Similarly CObList,CPtrListand CStringList stores Linked lists of CObject pointers, void pointers CStrings etc.
Note:CString is a collection class of string just like char
Map Classes l8r
Now lets do some praticals........
Createe a Win32 console application , give it any name you want and select "AN application that supports MFC" in steps 1 of 1
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
//Create and array of 10 elements
CUIntArray a;
a.SetSize(10);
cout<<"ELEMENTS:";
for(int i=0;i<10;i++)
{
a[i]=i+1;
cout<<a[i]<<" ";
}
cout<<endl;
//Determine the number of elements
int size=a.GetSize(); //returns the size of the array
cout<<"Size:"<<size<<endl;
//Determine the maximum index
cout<<"Upper Bound:"<<a.GetUpperBound();
//Extract an element form a specified location
UINT item=a.GetAt(6);//get element at position 6
//UINT is actuallly a typedef of unsigned int
//Set an element at specified position
a.SetAt(6,99);//Place the number 99 at posion 6
item=a.GetAt(6);
cout<<"New item at 6:"<<item<<endl;
//insert an element at specified position
a.InsertAt(6,100);
cout<<"New Elements:";
for(i=0;i<a.GetUpperBound();i++)
cout<<a[i]<<" ";
//Delete an element from the specified position
a.RemoveAt(3,2);//this means that from position 3 onwards delete 2 elements
//Append a new array to existing array
CUIntArray b;
b.SetSize(5);
for(i=0;i<5;i++)
b[i]=i+50;
a.Append(b);//append b at the end of a
cout<<"the resultant:";
for(i=0;i<a.GetUpperBound();i++)
cout<<a[i]<<" ";
cout<<endl;
//Remove all elements form the array
a.RemoveAll();
b.RemoveAll();
}
rest explaination tomorrow
浙公网安备 33010602011771号