Data Structure-exercise 18 vector & amp; list

Source: Internet
Author: User

Vector usage
The functions included in the vector are:

 

I wrote a program and tested several common functions:

[Cpp]
# Include <iostream>
# Include <vector>
# Include <list>
# Include <deque>
Using namespace std;
Int main ()
{
// Initialization
Vector <int> a (3, 7 );
Cout <"output element ";
/// Iterator
For (vector <int>: iterator itr = a. begin (); itr <a. end (); ++ itr)
Cout <* itr;
Cout <endl;
 
// Test whether empty () is empty ()
If (! A. empty ())
Cout <"the container size is:" <a. capacity () <"the element is:" <a. size ();
Else
Cout <"the element is empty ";
Cout <endl;
 
// Clear element clear ()
A. clear ();
 
If (! A. empty ())
Cout <"the elements in the container are cleared by clear ()" <endl;
// Resize the container. resize
 
A. resize (5 );
Cout <"the container size is adjusted to:" <a. capacity () <endl;
 
Cout <"the default size of the five elements is :";
For (vector <int >:: iterator it = a. begin (); it <a. end (); ++ it)
Cout <* it;
 
A. push_back (3 );
A. push_back (4 );
 
// Returns the first element.
Cout <"returns the first element" <. front () <", the container capacity is:" <. capacity () <"it can be seen that push_back only adds an element to the end ";
 
Cout <endl <"Get elements through at" <a. at (5) <endl;
 
// Pop return
A. pop_back ();
Cout <"pop_back, the container size is:" <. capacity () <"number of elements:" <. size () <endl <"output element ";
For (vector <int >:: iterator it = a. begin (); it <a. end (); ++ it)
Cout <* it;
Cout <endl <"indicates that pop_back only pops up elements and does not return" <endl;
 
A. assign (); // The capacity is not changed and the size is changed.
Cout <"capacity:" <a. capacity () <endl ;;
A [0] = 2;
Cout <"displays 0th and 3rd elements, and verifies the assignment function of [] and assign:" <a [0] <", "<a [3];
 
Cout <"front () returns the last element" <a. back ();
 
Vector <int>: iterator it = a. begin ();
A. erase (it );
Cout <"the element after erase is :";
For (vector <int >:: iterator it = a. begin (); it <a. end (); ++ it)
Cout <* it;
A. swap (vector <int> ());
Return 0;
}

# Include <iostream>
# Include <vector>
# Include <list>
# Include <deque>
Using namespace std;
Int main ()
{
// Initialization
Vector <int> a (3, 7 );
Cout <"output element ";
/// Iterator
For (vector <int>: iterator itr = a. begin (); itr <a. end (); ++ itr)
Cout <* itr;
Cout <endl;

// Test whether empty () is empty ()
If (! A. empty ())
Cout <"the container size is:" <a. capacity () <"the element is:" <a. size ();
Else
Cout <"the element is empty ";
Cout <endl;

// Clear element clear ()
A. clear ();

If (! A. empty ())
Cout <"the elements in the container are cleared by clear ()" <endl;
// Resize the container. resize

A. resize (5 );
Cout <"the container size is adjusted to:" <a. capacity () <endl;

Cout <"the default size of the five elements is :";
For (vector <int >:: iterator it = a. begin (); it <a. end (); ++ it)
Cout <* it;

A. push_back (3 );
A. push_back (4 );

// Returns the first element.
Cout <"returns the first element" <. front () <", the container capacity is:" <. capacity () <"it can be seen that push_back only adds an element to the end ";

Cout <endl <"Get elements through at" <a. at (5) <endl;

// Pop return
A. pop_back ();
Cout <"pop_back, the container size is:" <. capacity () <"number of elements:" <. size () <endl <"output element ";
For (vector <int >:: iterator it = a. begin (); it <a. end (); ++ it)
Cout <* it;
Cout <endl <"indicates that pop_back only pops up elements and does not return" <endl;

A. assign (); // The capacity is not changed and the size is changed.
Cout <"capacity:" <a. capacity () <endl ;;
A [0] = 2;
Cout <"displays 0th and 3rd elements, and verifies the assignment function of [] and assign:" <a [0] <", "<a [3];

Cout <"front () returns the last element" <a. back ();

Vector <int>: iterator it = a. begin ();
A. erase (it );
Cout <"the element after erase is :";
For (vector <int >:: iterator it = a. begin (); it <a. end (); ++ it)
Cout <* it;
A. swap (vector <int> ());
Return 0;
}
Test Results

:

 

 

List usage
Compared with vector, list has no capacity, because: list does not need to be re-allocated to all the memory. list uses a linked list. Each added element requires a new element, insert it to the list and remember that the list is a double-stranded table. List has one more pash_front and pop_front.
 

[Cpp]
# Include <list>
# Include <iostream>
Using namespace std;
Int main ()
{
// Define LISTINT
Typedef list <int> LISTINT;
Typedef list <char> LISTCHAR;
 
LISTINT list_int (3 );
List_int.assign (4, 5 );
List_int.push_back (6 );
List_int.push_front (10 );
 
List_int.push_front (1 );
List_int.pop_front ();
List_int.pop_back ();
List_int.push_back (6 );
For (LISTINT: iterator itr = list_int.begin (); itr! = List_int.end (); ++ itr)
Cout <* itr <",";
Cout <endl;
For (LISTINT: reverse_iterator itr = list_int.rbegin (); itr! = List_int.rend (); ++ itr)
Cout <* itr <",";
Cout <endl;
List_int.erase (-- list_int.end ());
List_int.sort ();
For (LISTINT: iterator itr = list_int.begin (); itr! = List_int.end (); ++ itr)
Cout <* itr <",";
 
Return 0;
}

# Include <list>
# Include <iostream>
Using namespace std;
Int main ()
{
// Define LISTINT
Typedef list <int> LISTINT;
Typedef list <char> LISTCHAR;

LISTINT list_int (3 );
List_int.assign (4, 5 );
List_int.push_back (6 );
List_int.push_front (10 );

List_int.push_front (1 );
List_int.pop_front ();
List_int.pop_back ();
List_int.push_back (6 );
For (LISTINT: iterator itr = list_int.begin (); itr! = List_int.end (); ++ itr)
Cout <* itr <",";
Cout <endl;
For (LISTINT: reverse_iterator itr = list_int.rbegin (); itr! = List_int.rend (); ++ itr)
Cout <* itr <",";
Cout <endl;
List_int.erase (-- list_int.end ());
List_int.sort ();
For (LISTINT: iterator itr = list_int.begin (); itr! = List_int.end (); ++ itr)
Cout <* itr <",";

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.