Functions in list Vector

Source: Internet
Author: User

# Include <vector>
# Include <list>
Using namespace STD;
Then directly use
All functions of list are in English .....
Table 6.12. constructors and destructor of lists operation effect
List <ELEM> C creates an empty list without any elements
List <ELEM> C1 (C2) creates a copy of another list of the same type (all elements are copied)
List <ELEM> C (n) creates a list with n elements that are created by the default constructor
List <ELEM> C (n, ELEM) creates a list initialized with N copies of element ELEM
List <ELEM> C (beg, end) creates a list initialized with the elements of the range [beg, end)
C .~ List <ELEM> () destroys all elements and frees
Memory
Table 6.13. nonmodifying operations of lists operation effect
C. Size () returns the actual number of elements
C. Empty () returns whether the container is empty (equivalent to size () = 0, but might be faster)
C. max_size () returns the maximum number of elements possible
C1 = c2 returns whether C1 is equal to C2
C1! = C2 returns whether C1 is not equal to C2 (equivalent! (C1 = c2 ))
C1 <C2 returns whether C1 is less than C2
C1> C2 returns whether C1 is greater than C2 (equivalent to C2 <C1)
C1 <= c2 returns whether C1 is less than or equal to C2 (equivalent! (C2 <C1 ))
C1> = c2 returns whether C1 is greater than or equal to C2 (equivalent! (C1 <C2 ))
Table 6.14. Assignment operations of lists operation effect
C1 = c2 assigns all elements of C2 to C1
C. Assign (n, ELEM) assigns n copies of element ELEM
C. Assign (beg, end) assigns the elements of the range [beg, end)
C1.swap (C2) swaps the data of C1 and C2
Swap (C1, C2) same (as global function)
Table 6.15. Direct element access of lists operation effect
C. Front () returns the first element (no check whether a first element exists)
C. Back () returns the last element (no check whether a last element exists)
Table 6.16. iterator operations of lists operation effect
C. Begin () returns a bidirectional iterator for the first element
C. End () returns a bidirectional iterator for the position after the last element
C. rbegin () returns a reverse iterator for the first element of a reverse Iteration
C. rend () returns a reverse iterator for the position after the last element of a reverse Iteration
Table 6.17. insert and remove operations of lists operation effect
C. insert (Pos, ELEM) inserts at iterator position POS a copy of ELEM and returns the position of the new element
C. insert (Pos, N, ELEM) inserts at iterator position POS n copies of ELEM (returns nothing)
C. insert (Pos, beg, end) inserts at iterator position POS a copy of all elements of the range [beg, end) (returns nothing)
C. push_back (ELEM) appends a copy of ELEM at the end
C. pop_back () removes the last element (does not return it)
C. push_front (ELEM) inserts a copy of ELEM at the beginning
C. pop_front () removes the first element (does not return it)
C. Remove (VAL) removes all elements with value Val
C. remove_if (OP) removes all elements for which OP (ELEM) yields true
C. Erase (POS) removes the element at iterator position POs and returns the position of the next element
C. Erase (beg, end) removes all elements of the range [beg, end) and returns the position of the next element
C. Resize (Num) changes the number of elements to num (if size () grows, new elements are created by their default constructor)
C. Resize (Num, ELEM) changes the number of elements to num (if size () grows, new elements are copies of ELEM)
C. Clear () removes all elements (makes the container empty)
Table 6.18. Special modifying operations for lists operation effect
C. Unique () removes duplicates of consecutive elements with the same value
C. Unique (OP) removes duplicates of consecutive elements, for which OP () yields true
C1.splice (Pos, C2) moves all elements of C2 to C1 in front of the iterator position POS
C1.splice (Pos, C2, c2pos) moves the element at c2pos in C2 in front of POS of list C1 (C1 and C2 may be identical)
C1.splice (Pos, C2, c2beg, c2end) moves all elements of the range [c2beg, c2end) in C2 in front of POS of list C1 (C1 and C2 may be identical)
C. Sort () sorts all elements with operator <
C. Sort (OP) sorts all elements with OP ()
C1.merge (C2) Assuming both containers contain the elements sorted, moves all elements of C2 into C1 so that all elements are merged and still sorted
C1.merge (C2, OP) Assuming both containers contain the elements sorted due to the sorting criterion OP (), moves all elements of C2 into C1 so that all elements are merged and still sorted according to OP ()
C. Reverse () reverses the order of all elements
Examples of using lists
The following example in particle shows the use of the special member functions for lists:

// Cont/list1.cpp
# Include <iostream>
# Include <list>
# Include <algorithm>
Using namespace STD;
Void printlists (const list <int> & 11, const list <int> & 12)
{
Cout <"list1 :";
Copy (l1.begin (), l1.end (), ostream_iterator <int> (cout ,""));
Cout <Endl <"list2 :";
Copy (12. Begin (), 12.end(), ostream_iterator <int> (cout ,""));
Cout <Endl;
}
Int main ()
{
// Create two empty lists
List <int> list1, list2;
// Fill both lists with elements
For (INT I = 0; I <6; ++ I ){
List1.push _ back (I );
List2.push _ Front (I );
}
Printlists (list1, list2 );
// Insert all elements of list1 before the first element with value 3 of list2
//-Find () returns an iterator to the first element with value 3
List2.splice (find (list2.begin (), list2.end (), // destination position
3 ),
List1); // source list
Printlists (list1, list2 );
// Move first element to the end
List2.splice (list2.end (), // destination position
List2, // source list
List2.begin (); // Source Position
Printlists (list1, list2 );
// Sort second list, assign to list1 and remove duplicates
List2.sort ();
List1 = list2;
List2.unique ();
Printlists (list1, list2 );
// Merge both sorted lists into the first list
List1.merge (list2 );
Printlists (list1, list2 );
}

The program has the following output:

List1: 0 1 2 3 4 5
List2: 5 4 3 2 1 0
List1:
List2: 5 4 0 1 2 3 4 5 3 2 1 0
List1:
List2: 4 0 1 2 3 4 5 3 2 1 0 5
List1: 0 0 1 1 2 2 3 3 4 5 5
List2: 0 1 2 3 4 5
List1: 0 0 0 1 1 1 2 2 2 3 3 3 4 4 5 5 5
List2:

Vector
// Create a vector and allocate memory to it
STD: vector <int> V; // create an empty vector
V. Reserve (80); // reserve memory for 80 Elements
// Create a vector and initialize it with the default constructor, so the speed is slow.
STD: vector <t> V (5); // creates a vector and initializes it with five values
// (Callfive times the default constructor of type T)

Table 6.2. constructors and Destructors of vectors operation effect
Vector <ELEM> C creates an empty vector without any elements
Vector <ELEM> C1 (C2) creates a copy of another vector of the same type (all elements are copied)
Vector <ELEM> C (n) creates a vector with n elements that are created by the default constructor
Vector <ELEM> C (n, ELEM) creates a vector initialized with N copies of element ELEM
Vector <ELEM> C (beg, end) creates a vector initialized with the elements of the range [beg, end)
C .~ Vector <ELEM> () destroys all elements and frees the memory
Table 6.3. nonmodifying operations of vectors operation effect
C. Size () returns the actual number of elements
C. Empty () returns whether the container is empty (equivalent to size () = 0, but might be faster)
C. max_size () returns the maximum number of elements possible
Capacity () returns the maximum possible number of elements without reallocation
Reserve () enlarges capacity, if not enough yet [7] // continue allocating memory if not enough
C1 = c2 returns whether C1 is equal to C2
C1! = C2 returns whether C1 is not equal to C2 (equivalent! (C1 = c2 ))
C1 <C2 returns whether C1 is less than C2
C1> C2 returns whether C1 is greater than C2 (equivalent to C2 <C1)
C1 <= c2 returns whether C1 is less than or equal to C2 (equivalent! (C2 <C1 ))
C1> = c2 returns whether C1 is greater than or equal to C2 (equivalent! (C1 <C2 ))
Table 6.4. Assignment operations of vectors operation effect
C1 = c2 assigns all elements of C2 to C1
C. Assign (n, ELEM) assigns n copies of element ELEM
C. Assign (beg, end) assigns the elements of the range [beg, end)
C1.swap (C2) swaps the data of C1 and C2
Swap (C1, C2) same (as global function)
Table 6.5. Direct element access of vectors operation effect
C. At (idx) returns the element with index idx (throws range error exception if idx is out of range)
C [idx] returns the element with index idx (no range checking)
C. Front () returns the first element (no check whether a first element exists)
C. Back () returns the last element (no check whether a last element exists)
An out_of_range exception occurs when an element is accessed through.
When you use [] overload to access the service, only an error is reported.
Table 6.6. iterator operations of vectors operation effect
C. Begin () returns a random access iterator for the first element
C. End () returns a random access iterator for the position after the last element
C. rbegin () returns a reverse iterator for the first element of a reverse Iteration
C. rend () returns a reverse iterator for the position after the last element of a reverse Iteration
Table 6.7. insert and remove operations of vectors operation effect
C. insert (Pos, ELEM) inserts at iterator position POS a copy of ELEM and returns the position of the new element
C. insert (Pos, N, ELEM) inserts at iterator position POS n copies of ELEM (returns nothing)
C. insert (Pos, beg, end) inserts at iterator position POS a copy of all elements of the range [beg, end) (returns nothing)
C. push_back (ELEM) appends a copy of ELEM at the end
C. pop_back () removes the last element (does not return it)
C. Erase (POS) removes the element at iterator position POs and returns the position of the next element
C. Erase (beg, end) removes all elements of the range [beg, end) and returns the position of the next element
C. Resize (Num) changes the number of elements to num (if size () grows, new elements are created by their default constructor)
C. Resize (Num, ELEM) changes the number of elements to num (if size () grows, new elements are copies of ELEM)
C. Clear () removes all elements (makes the container empty)
STD: vector <ELEM> Coll;
...
// Remove all elements with value Val
Coll. Erase (remove (Coll. Begin (), coll. End (),
Val ),
Coll. End ());
STD: vector <ELEM> Coll;
...
// Remove first element with value Val
STD: vector <ELEM >:: iterator Pos;
Pos = find (Coll. Begin (), coll. End (),
Val );
If (Pos! = Coll. End ()){
Coll. Erase (POS );
}
Vector <bool> has special functions.
Table 6.8. Special Operations of vector <bool> operation effect
C. Flip () negates all Boolean elements (complement of all BITs)
M [idx]. Flip () negates the Boolean Element with index idx (complement of a single bit)
M [idx] = Val assigns Val to the Boolean Element with index idx (assignment to a single bit)
M [idx1] = m [idx2] assigns the value of the element with index idx2 to the element with index idx1
Examples of Using vectors
The following example shows a simple usage of vectors:

// Cont/vector1.cpp
# Include <iostream>
# Include <vector>
# Include <string>
# Include <algorithm>
Using namespace STD;
Int main ()
{
// Create empty vector for strings
Vector <string> sentence;
// Reserve memory for five elements to avoid reallocation
Sentence. Reserve (5 );
// Append some elements
Sentence. push_back ("hello ,");
Sentence. push_back ("how ");
Sentence. push_back ("are ");
Sentence. push_back ("you ");
Sentence. push_back ("? ");
// Print elements separated with spaces
Copy (sentence. Begin (), sentence. End (),
Ostream_iterator <string> (cout ,""));
Cout <Endl;
// Print ''technical data''
Cout <"max_size ():" <sentence. max_size () <Endl;
Cout <"size ():" <sentence. Size () <Endl;
Cout <"capacity ():" <sentence. Capacity () <Endl;
// Swap Second and Fourth Element
Swap (sentence [1], sentence [3]);
// Insert element "always" before element "? "
Sentence. insert (find (sentence. Begin (), sentence. End (),"? "),
"Always ");
// Assign "! "To the last element
Sentence. Back () = "! ";
// Print elements separated with spaces
Copy (sentence. Begin (), sentence. End (),
Ostream_iterator <string> (cout ,""));
Cout <Endl;
// Print "Technical Data" again
Cout <"max_size ():" <sentence. max_size () <Endl;
Cout <"size ():" <sentence. Size () <Endl;
Cout <"capacity ():" <sentence. Capacity () <Endl;
}

The output of the program might look like this:

Hello, how are you?
Max_size (): 268435455
Size (): 5
Capacity (): 5
Hello, you are how always!
Max_size (): 268435455
Size (): 6
Capacity (): 10
 
 

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.