Learning notes-Thoughts on writing Vector

Source: Internet
Author: User

I tried to write a vector container myself. I thought it was very simple. Who knows how to find a lot of difficulties when writing the vector? I also made some silly and fatal mistakes.

For example, the first one is

Int * Source = new int [10];
Int n = sizeof (source)/sizeof (INT );
Cout <"n =" <n <Endl;
What do you think is output?
At the beginning, I thought the output was 10. Wrong. The correct answer is 1. the sizeof (source) is still equal to sizeof (INT ); so I wrote a transfer where the loop condition is originally I <sizeof (source)/sizeof (INT)
Later, I learned that this would not work. I also thought about the container in STL. The algorithm is generally used to pass in begin and end. It turns out to be like this, so I cannot pass in an address. I need two addresses.
The second is also an idiot.
I thought
Int * array;
Array = new int (10); defines an array statement. In fact, this only defines an integer pointer array with the integer value 10;
Which of the following statements is true?
Array = new int [10];
Another one is new and delete. This is really the difficulty of C ++ and is often wrong.

 

Int * p = 0;
P = new int (1 );
Delete P;
P = 0;
I often found that the original null and 0 are also null pointers and thought
It can also be written
Int * P = NULL;
P = new int (1 );
Delete P;
P = NULL;
After the delete operation, you need to assign a null pointer so that it is okay to delete the NULL pointer.
# Include <iostream>
Using namespace STD;

Class carray
{
PRIVATE:
Int * array;
Int sizeofarray;
Int capacityofarray;
Void transfer (int * destination, const int * sourcefirst, const int * sourcelast );
Public:
Carray (INT capacity = 0 );
Int size () const;
Int capacity () const;
Int * begin () const;
Int * end () const;
Int push_back (const int value );
Int at (INT nindex) const;
Bool empty ();
Int pop_back ();
Void reserve (const int capacity );
Void insert (const int nindex, const int value );
~ Carray ();
};
Void carray: Transfer (int * destination, const int * sourcefirst, const int * sourcelast)
{
Cout <"Transfer (int * destination, const int * sourcefirst, const int * sourcelast)" <Endl;
For (const int * iterator = sourcefirst; iterator! = Sourcelast; iterator ++)
{
* (Destination + (iterator-sourcefirst) = * (iterator );
}
}
Carray: carray (INT capacity)
{
Cout <"carray (INT capacity = 0)" <Endl;
Capacityofarray = capacity;
Sizeofarray = 0;
Array = new int [capacityofarray];
}

Int carray: size () const
{
Return sizeofarray;
}

Int carray: Capacity () const
{
Return capacityofarray;
}

Int * carray: Begin () const
{
Return array;
}

Int * carray: end () const
{
Return array + sizeofarray;
}

Int carray: push_back (const int value)
{
Cout <"push_back (const int value)" <Endl;
If (sizeofarray> = capacityofarray)
{
If (sizeofarray = 0)
{
Array = new int [++ capacityofarray];
}
Else
{
Int * temp = new int [sizeofarray];
Temp = array;
Array = new int [++ capacityofarray];
Transfer (array, temp, temp + sizeofarray );
Delete [] temp; // do I need to delete the memory here?
Temp = NULL;
}
}
* (Array + sizeofarray) = value;
Sizeofarray ++;
Return value;
}

Int carray: At (INT nindex) const
{
If (nindex> sizeofarray)
{
Cout <"overload !!! "<Endl;
}
Return * (array + nIndex-1 );
}
Bool carray: Empty ()
{
If (sizeofarray = 0)
{
Return true;
}
Return false;
}
Int carray: pop_back ()
{
If (sizeofarray)
{
Deleting (array + sizeOfArray-1 );
Sizeofarray --;
Return 1;
}
Return 0;

}

Void carray: Reserve (INT capacity)
{
Int * temp = new int [sizeofarray];
Temp = array;
Capacityofarray = capacity;
Array = new int [capacityofarray];
Transfer (array, temp, temp + sizeofarray );
Delete [] temp;
Temp = NULL;
}

Void carray: insert (const int nindex, const int value)
{
If (sizeofarray + 1) = nindex)
{
Push_back (value );
}
Else
{
Reserve (++ capacityofarray );
For (INT I = sizeofarray; I> nindex; I --)
{
* (Array + i-1) = * (array + I );
}
* (Array + nIndex-1) = value;
Sizeofarray ++;
}
}

Carray ::~ Carray ()
{
Cout <"~ Carray () "<Endl;
If (this-> capacityofarray> 0)
{
Delete [] array;
Array = NULL;
}
}
Int main ()
{
Cin. Get ();
Return 0;
}

Author: Lin yufei
Source: http://www.cnblogs.com/zhengyuhong/
The copyright of this article is shared by the author and the blog Park. You are welcome to repost it. However, you must keep the author's information without the author's consent and provide the original article connection clearly on the article page.

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.