Bibliography: Visual C + + Getting Started classic Seventh edition Ivor Horton Tenth chapter
Recognize two containers: vector and List
Container: is one of the six components of the STL (Standard Template Library). (Container, container adapter, iterator, algorithm, function object, function adapter)
A container is an object used to store and organize other objects. Provides the type of object to be stored to create a container class from an STL template.
Vector <t>: Represents an array that can increment the capacity at the necessary moment, which stores elements of type T. You can add new elements only at the end of a vector container.
Vector <int> MyData;//create a container that stores values of type int, the initial capacity of the storage element is 0;
Mydata.push_back (99);//Add a new element to the end of the vector;
Mydata.pop_back ();//delete an element at the end
Mydata.clear ();
Mydata.insert (Begin (MyData) +1,88);//Insert a new element after the 1th element 88
Vec.insert (Begin (VEC) +1,3,22);//Insert 3 elements after the first element, 22,22,22
Vec.reserve (datasize);//reserve Space for containers
Example: (Construction and reading of vector container)
//Person.h#pragmaOnce#include<iostream>#include<cstring>classperson{ Public: Person (); Public: ~Person ();Private: voidInitname (Const Char* First,Const Char*second); Char*FirstName; Char*Secondname; Public: voidShowperson ()Const; Person (Char* First,Char*second); Person (ConstPerson &p); Person ( person&&p); person&operator=(Constperson&p); //Moveperson&operator= (person&&p); BOOL operator< (Constperson& p)Const;};
//Person.cpp#include"Person.h"Person ::P Erson (): FirstName (NULL), Secondname (null) {}person::~Person () {}voidPerson::initname (Const Char* First,Const Char*second) {size_t length{strlen (first)+1 }; FirstName=New Char[length]; strcpy_s (firstname, length, first); Length= strlen (second) +1; Secondname=New Char[length]; strcpy_s (secondname, length, second);}voidPerson::showperson ()Const{std::cout<< FirstName <<""<< Secondname <<Std::endl;} Person::P Erson (Char* First,Char*second) {Initname (first, second);} Person::P Erson (ConstPerson &p) {Initname (p.firstname, p.secondname);} Person::P Erson ( person&&p) {FirstName=P.firstname; Secondname=P.secondname; //reset Rvalue object pointer to prevent deletionP.firstname =nullptr; P.secondname=nullptr;}//Copyperson& Person::operator=(Constperson&p) { //Todo:insert Return statement here if(&p! = This) { Delete[] FirstName; Delete[] secondname; Initname (P.firstname, p.secondname); } return* This;}//Moveperson& Person::operator= (person&&p) { if(&p! = This) { Delete[] FirstName; Delete[] secondname; FirstName=P.firstname; Secondname=P.secondname; //reset Rvalue object pointer to prevent deletionP.firstname =nullptr; P.secondname=nullptr; } return* This; //Todo:insert Return statement here}BOOLPerson::operator< (Constperson& p)Const{ intresult{strcmp (Secondname, P.secondname)}; return(result<0|| result==0&&STRCMP (Firstname,p.firstname) <0);}
//Ex10.02.cpp//storing objects in a vector#include <iostream>#include<vector>#include"Person.h"usingstd::vector;usingstd::cout;usingStd::endl;intmain () {vector<Person>people; Constsize_t maxlength{ - }; CharFirstname[maxlength]; CharSecondname[maxlength]; while(1) {cout<<"Enter a first name or press ENTER to end:"; Std::cin.getline (FirstName, MaxLength,'\ n'); if(strlen (FirstName) = =0) { Break; } cout<<"Enter the second name:"; Std::cin.getline (Secondname, MaxLength,'\ n'); People.emplace_back (Person (firstname, secondname)); } cout<<Endl; Auto ITER=Cbegin (people); while(ITER! =cend (People)) {iter-Showperson (); ++ITER; } CharMYNAMEF [ -] = {"Myfirst" }; Charmynames[ -] = {"Mysecond" }; Person insert_t (MYNAMEF, mynames); People.insert (Begin (People)+1, insert_t); ITER= Begin (People) +1; ITER-Showperson ();}
List<t>
The advantage of implementing a doubly linked list is that you can insert or delete elements at any point in the sequence at a fixed time, to make sure that the list does not directly access its elements based on location.
The element is accessed by iterating through the elements in the list, starting at a known location.
Create:std::list<double> values (50,2.728);
Insert: Values.insert (++begin (values), 75);
Building elements: Emplace (,); Emplace_back (); Emplace_front ();
Access: for (const Auto & s:values) {Std::cout<<s<<endl;}
Example:
//Example for list//get some sentences from keyboard, then store it in the list#include <iostream>#include<list>#include<string>#include<functional>usingSTD::string;usingstd::cout;usingStd::endl;voidListall (Conststd::list<string> &strings) { for(Auto &s:strings) {cout<< s <<Endl; }}intMain () {std::list<string> text;//Create Listcout <<"Enter a few lines of Text.just press Enter to end:"<<Endl; stringsentence; while(Getline (std::cin, sentence,'\ n'), !Sentence.empty ()) {Text.push_front (sentence); } cout<<"your text in reverse order:"<< Endl;//Flashback OutputListall (text); Text.sort ();//Sortcout <<"\nyour text in ascending sequence:"<<Endl; Listall (text);}
Postscript:
These two containers are still only in the stage of being able to use, to understand and experience the difference between the advantages and disadvantages of the program, and to learn more about the data structure of knowledge.
There are many containers in the STL, which are not available for the time being, and there are times for systematic learning.
Vectors and lists in the STL