Use C ++ SDK to implement dynamic array classes

Source: Internet
Author: User

C ++ SDK does not directly provide the dynamic array function. Although it can be implemented using the carray class of MFC or using the pointer + new, the former requires the MFC library, the latter is hard to understand and inefficient. Therefore, I have posted a simple dynamic array class I wrote a long time ago, hoping to inspire some friends.

Code Description: This class is optimized in the assignment of array members and memory allocation, significantly improving the performance of large arrays.

Usage: copy the code to the header file cmyarray. h and reference the file in the project.

I. header file cmyarray. h

# Ifndef _ tdarray_h __# DEFINE _ tdarray_h __# include <windows. h> # include <tchar. h> template <typename T> class cmyarray {PRIVATE: T * m_parray; // memory pointer DWORD m_nitemcount; // Total Number of Members DWORD m_nblockcount; // Number of memory blocks DWORD m_nitemsperblock; // Number of Members contained in each memory block DWORD m_dwallocationgranularity; // memory allocation granularity (I .e. memory block size) DWORD item_size; public: cmyarray () {system_info Si; getsysteminfo (& Si); m_dwallocationgranularity = SI. dwallocationgra Nularity; m_nitemcount = 0; m_nblockcount = 0; m_parray = NULL; item_size = sizeof (t); m_nitemsperblock = m_dwallocationgranularity/item_size ;};~ Cmyarray () {If (m_parray) globalfree (m_parray) ;}; T * Add (T item) {return add (& item) ;}; T * Add (T * item) {If (! M_parray) {m_parray = (T *) globalalloc (gmem_fixed | gmem_zeroinit, m_dwallocationgranularity); If (! M_parray) Throw _ T ("memory overflow"); m_nblockcount = 1;} else {If (m_nitemcount % m_nitemsperblock = 0) {m_nblockcount ++; m_parray = (T *) globalrealloc (m_parray, m_nblockcount * m_dwallocationgranularity, gmem_moveable | gmem_zeroinit); If (! M_parray) Throw _ T ("memory overflow") ;}} T * P = m_parray + m_nitemcount; If (item_size <= 8) * P = * item; elsememcpy (p, item, item_size); m_nitemcount ++; return P ;}; bool remove (DWORD position) {If (position <m_nItemCount-1) {memcpy (m_parray + position, m_parray + Position + 1, (m_nItemCount-1-position) * item_size); m_nitemcount --; return true;} return false;}; void clear () {If (m_parray) globalfree (m_parray); m_nitemcount = 0; m_nblockcount = 0; m_parray = NULL;}; T * getitem (DWORD position) {If (position <m_nitemcount) return m_parray + position; elsereturn NULL;}; t getitemvalue (DWORD position) {return * (m_parray + position) ;}; bool setitem (DWORD position, t item) {return setitem (Position, & item) ;}; bool setitem (DWORD position, T * Item) {If (position <m_nitemcount) {T * P = m_parray + position; if (item_size <= 8) * P = * item; elsememcpy (, item, item_size); Return true;} return false;}; DWORD getcount () {return m_nitemcount ;};## endif/_ tdarray_h __

Ii. Test code:

# Include "stdafx. H "# include <stdio. h> # include <dos. h> # include <conio. h> # include "cmyarray. H "int main (void) {long I; // instantiate the array class cmyarray <long> * parray = new cmyarray <long> (); // Add an array member for (I = 1; I <100000; I ++) {parray-> Add (I );} // remove the array member parray-> remove (99991); // obtain the number of array members long ncount = parray-> getcount (); // display part of the array member for (I = 99990; I <ncount; I ++) {printf ("member % d value: % d. \ R \ n ", I, parray-> getitemvalue (I);} // destroy the array object Delete parray; getchar (); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.