Introduction and example of C + + container class source code __c++

Source: Internet
Author: User


When you first contact the container, you can simply think of him as an array of various types (struct is also possible), ideas and operations are also similar to the number of groups, is the command to use some of the differences


Simple Introduction to C + + container classes


I. Prototypes and constructors
The vector's prototype can be defined as
Vector >
Whose constructors are
Vector ()//Empty
Vector (AL)//designation of a allocator
Vector (n)//Initialize n elements with default t ()
Vector (n, val)//n elements initialized with Val
Vector (N,val,al)///Val initializes n elements, using Al as Distributor
Vector (first,last)//copy generation from your own
Vector (First,last,al)//Copy generation from the previous one to the last, using Al as Distributor




Second, the operation
1. Open Up n space
Vecobj.reserve (N);


2. Current (before reallocation of memory) get maximum capacity
Capacity ();


3. Reassign memory to N
Resize (N)
If it becomes smaller, remove the excess. If it becomes larger, it is filled with t ()


4. Empty
Clear ();
Note that clear () and resize () do not necessarily make the vector smaller, and if you want to free up memory, use Vecobj.swap (vector ())


5. Access to the elements
Front () and back () operations, take the latter and the first element, note that its return is a reference, but rather a left value (l_value), so you can assign a value. To do an operation similar to Vecobj.front () = 3;


To ensure that the front space is valid, otherwise the form is unpredictable.


6. Take the value
[] with at can do this, at will check if there is a Out_of_range exception to be throw


7.push_back: The function is to add a data to the vector tail
The push_back function means inserting an element at the end of the vector.
Vector is simply understood to be a dynamic one-dimensional array push_back function is to insert an element at the tail of this one-dimensional array


Vector<int> v;
V.push_back (1); V Inside is: 1
V.push_back (2); V inside is: 1, 2
V.push_back (3); V inside are: 1, 2, 3.


Pop_back: function to delete the last element


8. Use of Assign
Assign can change the size and initial value, the size is arbitrary, not subject to the initial size of the limit, if set to 0, then empty.
Assign (5,0) changes the vector to 5 sizes and fills with 0
Assign (iax+3,iax+5); From the 4th to 5th fill of the array, note that the left is closed to the right, you can take to iax[3] and iax[4]


9. Use Insert
Insert (it, x), inserts an element x before it
Insert (It,first,last) before it inserts a sequence [first,last] left-right open


10. Use of Erase
Erase (IT) deletes the element at it, and the return value is the next element. If Intvec.erase (Intvec.end ()) is not an error, if you delete a sequence [first,last), use the Erase


(First,last)


11.BVector is a special version of vector, the specific purpose of which needs to be verified


12.flip () reverse an element.


13.swap. Vecobj.swap (Vecobj[i],vecobj[j]);
To mount an object in a container and can and retrieve it, you need to overload operator = =, as follows:
#include
#include
#include
#include
#include
using namespace Std;


Class OBJ
{
Public
OBJ (int x, int y, int z)
{
this->x = x;
This->y = y;
This->z = Z;
}
}


BOOL operator = = (Const obj & OBJ)
{
if (obj.x = = x && obj.y = y && obj.z = z)
{
return true;
}
return false;
}
int GetX ()
{
return this-> x;
}
Private
int x;
int y;
int z;
}


int main (int argc, char * argv[])
{
Vector vecobj;
OBJ obj1 (2,3,4);
OBJ obj2 (4,5,6);
Vecobj.push_back (OBJ1);
Vecobj.push_back (OBJ2);


Vector::iterator It =find (Vecobj.begin (), Vecobj.end (), OBJ (2,3,4));
if (it!= vecobj.end ())
cout << (*it). GetX () << Endl;
return 0;
}




Basic usage of List


As with vectors, the splice () function, which needs to be emphasized, is to insert another list of the specified segments in front of the specified position.
Splice (iterator it, List &x)
Splice (iterator it, List &x, iterator a)
Splice (iterator It,list &x, iterator-I, iterator last)


I. Prototypes and constructors
Typdef list > Listobj;
Constructors
List ()//Empty
List (AL)//Specified allocator empty table
List (n)//n elements, all elements are t ()
List (n,val)//n elements, all elements are T (val)
List (N,val,al)/Ibid., and specify allocator as Al
List (A, last)//copy Construction
List (First,last,al)//Specify Allocator Construction


Second, the operation
1.resize & Clear
Use resize (n) to change the size, using resize (n, val) If you want to fill the idle value with a T (Val).


2.front () & Back ()
If the Listobj object, the return is a left-valued function


3. Insert operation
Insert (iterator it, Val)
Insert (iterator it, I, last)
Insert (Iteratot it, n, x)//inserts n x


4. Remove
Remove (x); Vector.erase (Integrator it)


Delete by value
int iax[] ={3,4,5,6,6,7,8}
List lobj;
Lobj.insert (Lobj.begin (), Iax, Iax + 7);
Lobj.remove (6); //
Delete by function condition


#include
#include
using namespace Std;
A predicate implemented as a function:
BOOL Single_digit (const int& value) {return (VALUE&LT;10);}
A predicate implemented as a class:
Class Is_odd
{
Public
BOOL Operator () (const int& value) {return (value%2) ==1;}
};
int main ()
{
int myints[]= {15,36,7,17,20,39,4,1};
List MyList (myints,myints+8); 15 36 7 17 20 39 4 1
Mylist.remove_if (Single_digit); 15 36 17 20 39
Mylist.remove_if (Is_odd ()); 36 20
cout << "mylist contains:";
for (List::iterator it




=mylist.begin (); It!=mylist.end (); ++it)
cout << "" << *it;
cout << Endl;
return 0;
}
Of course, for class is_odd, you can also write
Template
Class Is_odd
{
};
When called, it is changed to
Mylist.remove_if (Is_odd ());

Related Article

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.