Use of vector and list for C + + container objects

Source: Internet
Author: User

In <<c++ primer>> Fourth Edition exercise section 9.3.4 exercise 9.20 is a topic that writes a program to determine whether a vector<int> container contains elements with a list <int> containers are exactly the same. The test code is as follows:

1#include"stdafx.h"2#include <iostream>3#include <string>4#include <list>5#include <deque>6#include <vector>7 8 9 using namespacestd;Ten  One intMain () A { -vector<int>Vect; -list<int>Li; the     intVect_copy[] = {1,2,3,2,1,2,6}; -     intLi_copy[] = {2,4,6, Wu,7,0,2}; -Vect.insert (Vect.begin (), Vect_copy, vect_copy+7); -Li.insert (Li.begin (), li_copy,li_copy+7); +  -     if(Vect.size ()! =li.size ()) +     { Acout <<"it is different."<<Endl; at         return 0; -     } -  -      for(vector<int>::iterator begin = Vect.begin (); Begin! = Vect.end (); ++begin) -     { -         BOOLFlag =false; in          for(list<int>::iterator Begin_li = Li.begin (); Begin_li! = Li.end (); ++Begin_li) -         { to             if(*begin = = *Begin_li) +             { -Flag =true; the                 Continue; *             } $         }Panax Notoginseng         if(flag) -         { the  +         } A         Else the         { +cout << *begin <<"In Vect isn't in the list"<<Endl; -         } $     } $System"PAUSE"); -     return 0; -}
View Code

The container object has an INSERT member function that is used to insert elements into the container, the first parameter is the insertion position, an iterator, and the following two parameters are the beginning and ending of the element iterator that needs to be inserted. Because the array name is a pointer, the array name is also passed directly here.

In addition, there are two loops that traverse vectors and lists, respectively.

The get element operation is as follows:

1#include"stdafx.h"2#include <vector>3#include <iostream>4 5 6 using namespacestd;7 8 9 intMain ()Ten { Onevector<int>Vect; A     intArry[] = {1,2,3, +, the, -}; -vector<int>Vect2; -Vect.insert (Vect.begin (), arry,arry+6); thecout << vect[0] <<Endl; -cout << Vect.front () <<Endl; -cout << *vect.begin () <<Endl; -cout << vect2[0] <<Endl; +System"PAUSE"); -     return 0; +}
View Code

It is important to note that if the container object is empty, it must be judged first, otherwise the operation is undefined. The code above cannot be run.

Use of vector and list for C + + container objects

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.