Deque basic operations
I. Prototype And Constructor
Typedef deque <t, Allocator <t> deqobj;
Constructor
Deque ();
Deque (Al );
Deque (N );
Deque (n, x );
Deque (n, x, Al );
Deque (first, last );
Deque (first, last, Al );
Ii. Operations
1. Resize & clear
Use resize (n) to change the size, and use resize (n, Val) to fill the idle value with T (VAL.
2. Clear operation
It is a good habit to call deqobj. Swap (deque <t, a> () after clear, and you must do the same.
3. font (), back (), operator [], (for example, outbound boundary, not fixed) at () (for example, outbound boundary, throwing an exception), push_back (), push_front (), pop_back (), pop_front (), insert (iterator it, x), insert (iterator it, n, x), insert (iterator first, iterator last ), (point to the value just inserted after insertion), erase (it), delete the value at the location specified by it, erase (iterator first, iterator last) delete the value of the specified range (left closed and right open ). These operations are similar to the above operations.
Basic operations of set and Multiset
I. Prototype And Constructor
Typedef set <key, less <key>, Allocator <key> setobj;
Constructor
Set (); // empty set, sorted by PRED ()
Set (PR); // declare an empty set sorted by PR
Set (PR, Al); // declares that a set sorted by PR is allocated by Al.
Set (first, last)
Set (first, last, Pr)
Set (first, last, PR, Al)
Operation
1. Clear ()
2. Erase (it); erase (first, last)
3. insert (key). The returned value is of the pair <iterator, bool> type. If no element is the same as the inserted element, second is true. In this case, first points to the newly inserted element. Otherwise, if it is false, first still points to the original element.
4. Find (key)
5. lower_bound (key)
6. upper_bound (key)
7. When _range (key), return a pair <iterator, iterator> (lower_bound (key), upper_bound (key ))
8. Length of Count, interval _range
9. key_comp. If k1 is in front of K2, then key_comp () (key1, key2) is true.
10. value_comp. It is the same as key_comp for the set <key> object.
Multiset
1. Insert: Because insert is always successful, it returns the iterator of the new element instead of the pair <iteraor, bool> object.
2. Find returns the first iterator equal to the key.
3. When _ range is returned, any length of [0, setobj. Size () is returned.
4. Count () returns any value of [0, setobj. Size.
Basic list usage
The usage is basically the same as that of vector. The splice () function is used to insert another list of the specified segment to the front of the specified position.
Splice (iterator it, list & X)
Splice (iterator it, list & X, iterator first)
Splice (iterator it, list & X, iterator first, iterator last)
I. Prototype And Constructor
Typdef list <t, Allocator <t> listobj;
Constructor
List () // null
List (Al) // specify the empty table of Allocator
List (n) // n elements. All elements are T ().
List (n, Val) // n elements, all elements are T (VAL)
List (n, Val, Al) // same as above, and specify Allocator as Al
List (first, last) // copy Structure
List (first, last, Al) // specify Allocator Construction
Ii. Operations
1. Resize & clear
Use resize (n) to change the size, and use resize (n, Val) to fill the idle value with T (VAL.
2. Front () & back ()
If the listobj object is a mass object, the return value is a left-value function.
3. insert operation
Insert (iterator it, Val)
Insert (iterator it, first, last)
Insert (iteratot it, n, x) // insert N x
4. Remove
Remove (x); // vector. Erase (integrator it)
Delete by value
Int IAX [] = {3, 4, 5, 6, 6, 7, 8 };
List <int> lobj;
Lobj. insert (lobj. Begin (), IAX, IAX + 7 );
Lobj. Remove (6 );//
Delete by function condition
# Include <iostream>
# Include <list>
Using namespace STD;
// A predicate implemented as a function:
Bool single_digit (const Int & Value) {return (value <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 <int> 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 <int >:: 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 T>
Class is_odd
{
};
Change
Mylist. remove_if (is_odd <int> ());
5. Unique operation
// List: unique
# Include <iostream>
# Include <cmath>
# Include <list>
Using namespace STD;
// A binary predicate implemented as a function:
Bool same_integral_part (double first, double second)
{Return (INT (first) = int (second ));}
// A binary predicate implemented as a class:
Class is_near
{
Public:
Bool operator () (double first, double second)
{Return (FABS (first-second) <5.0 );}
};
Int main ()
{
Double mydoubles [] = {12.15, 2.72, 73.0, 12.77, 3.14,
12.77, 73.35, 72.25, 15.3, 72.25 };
List <double> mylist (mydoubles, mydoubles + 10 );
// Before unique, you must use sort. Remember that the internal implementation of unique is I, I + 1.
Mylist. Sort (); // 2.72, 3.14, 12.15, 12.77, 12.77,
// 15.3, 72.25, 72.25, 73.0, 73.35
Mylist. Unique (); // 2.72, 3.14, 12.15, 12.77
// 15.3, 72.25, 73.0, 73.35
Mylist. Unique (same_integral_part); // 2.72, 3.14, 12.15
// 15.3, 72.25, 73.0
Mylist. Unique (is_near (); // 2.72, 12.15, 72.25
Cout <"mylist contains :";
For (list <double >:: iterator it = mylist. Begin (); it! = Mylist. End (); ++ it)
Cout <"" <* it;
Cout <Endl;
Return 0;
}
6. Sort operations
Sort (); // sort by operator by default, from small to large
Sort (PR); // PR is the functional function.
7. Merge operation
Prior to merge operations, operator must be used to sort the two sequences. Of course, you can also specify the PR sorting function.
Merge (S2)
Merge (S2, Pr );
8. Reverse ()
Flip the entire list
Basic usage of Vector
I. Prototype And Constructor
The prototype of vector can be defined
Vector <t, Allocator <t>
Its constructor is
Vector () // null
Vector (Al) // specify an allocator
Vector (n) // use the default T () to initialize n elements
Vector (n, Val) // use Val to initialize n elements
Vector (n, Val, Al) // use Val to initialize n elements and use Al as a distributor.
Vector (first, last) // generate a copy from your first to last
Vector (first, last, Al) // generate a copy from the first to the last, and use Al as the distributor.
Ii. Operations
1. Open up n Spaces
Vecobj. Reserve (N );
2. Current (before re-allocating memory) Get the maximum capacity
Capacity ();
3. re-allocate the memory to n
Resize (N)
If the value is smaller, the excess value is deleted. If it is larger, T () is used for filling.
4. Clear
Clear ();
Note that both clear () and resize () do not necessarily make the vector smaller. To release the memory, use vecobj. Swap (vector <t, a> ())
5. Access the first and last elements
The Front () and back () operations take the last and last elements. Note that the returned result is a reference, but the left value (l_value). Therefore, values can be assigned. similar to vecobj. front () = 3; operation, but to ensure that the front space is valid, otherwise it is unpredictable.
6. Values
[] And at can perform this operation. At will check that if there is a cross-border exception, out_of_range will be throw
7. push_back, pop_back
Make sure it is not empty
8. Use assign
Assign can be used to change the size and initial value. The size is random and is not limited by the START size. If it is set to 0, it is cleared.
Assign (5, 0) changes the vector size to 5, and adds the value with 0.
Assign (IAX + 3, IAX + 5); from array 4th to 5 fill, pay attention to left closed and right open, you can get IAX [3] and IAX [4]
9. Use Insert
Insert (it, x), insert an element x before it
Insert (it, first, last), insert a sequence before it [first, last) left closed right open
10. Use erase
Erase (IT) deletes the element in it and returns the next element. If intvec. Erase (intvec. End (); does not report an error, if you delete a sequence [first, last), use erase (first, last)
11. bvector is a special version of vector <bool>. Its specific use needs to be verified.
12. Flip () reverse an element. For example, vecobj [I]. Flip ();
13. Swap. vecobj. Swap (vecobj [I], vecobj [J]);
To install an object in the container and retrieve it, you need to reload operator =, as shown below:
# Include <vector>
# Include <iostream>
# Include <stdlib. h>
# Include <time. h>
// # Include <getopt. h>
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 <OBJ> vecobj;
OBJ obj1 (2, 3, 4 );
OBJ obj2 (4,5, 6 );
Vecobj. push_back (obj1 );
Vecobj. push_back (obj2 );
Vector <OBJ >:: iterator it = find (vecobj. Begin (), vecobj. End (), OBJ (2, 3, 4 ));
If (it! = Vecobj. End ())
Cout <(* It). getx () <Endl;
Return 0;
}