Summary of C + + sequential container learning

Source: Internet
Author: User

C + + sequential container

I. Description of C + + arrays

Dynamic memory allocation for one-dimensional arrays:

Int *num=new Int[len];

Delete []num;

Dynamic memory allocation for two-dimensional arrays:

1, know the second dimension

char (*num) [n];//Pointer to array

num = Newchar[m][n];

Delete[]num;

2, know the first dimension

Char*num [Array of m];//pointers

for (int i=0; i<m; i++)

Num[i] = new Char[n];

for (i=0; i<m; i++)

Delete[]num [i];

Delete[]num;

3, a known one-dimensional allocation of memory (to ensure the continuity of memory)

Char*num [Array of m];//pointers

num [0] = new Char[m*n];

for (int i=1; i<m; i++)

num [i] = num [i-1] + N;

Delete[] a[0];

4, two dimensions are unknown

char** num;

num = new char* [m];//array of assigned pointers

for (int i=0; i<m; i++)

Num[i] = new Char[n];

for (i=0; i<m; i++)

Delete[]num [i];

Delete[]num;

5, two dimensions are unknown, one time to allocate memory (to ensure the continuity of memory)

char * * NUM;

num = new char* [m];

num [0] = new CHAR[M * n];//allocate all space at once

for (int i=1; i<m; i++)

Num[i] = num [i-1] + n;//assigns the array to which each pointer points

delete[]num [0];

Delete[]num;

Ii. C + + container type

Vector Variable size array random access convenience

Deque two-terminal queue random access convenience

List of two-way linked list containers intermediate insertion and deletion is very convenient if there are a lot of small elements extra overhead is very serious

Forward_list one-way linked list container Intermediate Insert Delete very convenient if there are a lot of small elements extra overhead is very serious

Array fixed size arrays

String used to hold strings


Operation of the container:

1. Add elements to the container:

Push_back---Addition of array and forward_list, Push_back adds an element from the tail;

Push_front==list, forward_list, deque containers support Push_forward inserting elements from the beginning;

Intsert-vector, String,list,deque supports insert members, and Forward_list supports special inserts.

The new emplace-Standard introduces three members, and Emplace_front,emplace,emplace_back is a construct rather than a copy element,

2. The operation of accessing elements in the sequential container:

vector<string>c;

C.back ();//Returns a reference to the tail of C, if C is null, the function behavior is undefined;

C.front ();//Returns a reference to the head in C, if C is null, the function behavior is undefined;

C[n];

c.at (n);//Returns a reference with the subscript n, or an exception if the subscript is out of bounds

Note: The at operation is only applicable to the string vector deque array operation; Back is not applicable with forward_list.

3. Delete operation:

Vector <string>c;

C.push_back ();

C.push_front ();

C.erase (P);

C.erase (b,e);

C.clear ();

Note: Member functions that delete elements do not check their parameters and must be guaranteed to exist before they are deleted.

4, (unidirectional linked list) Forward_list Special use {Insert Delete action}

forward_list<int>fist;

Fist.before_begin ()

Fist.cbefore_begin ();

Fist.insert_after ();

Emplace_after ();

Fist.erase_after ();

5. Change the container size

C.resize (n);

C.resize (n,t);

6. member functions for managing capacity

Vector<string> C;

C.shrink_yo_fit); applies to vector string deque only, reducing capacity () to size ().

C.capacity (); How many elements can be stored without reallocating memory

C.reserve (n); Allocates a memory space that can hold at least N.

7. Container Adapter

size_type– a type that is sufficient to hold the maximum value of the current type

value_type-element type

container_type-realization of the underlying type of adaptation

The sample code is as follows:

#include <iostream> #include <string> #include <vector> using namespace Std;int main () {/*//container definition and initialization One container is copied to the other container list<string> str ({"FHT", "HJS", "Hut"}), Vector<const char*> str1 ("A", "B", "BCX");// Copy forward_list<string> Words (Str1.begin (), Str1.end ()); Deque<string> Nu (Str.begin (), it);//Use assign assignment (for sequential containers only) str.assign (Str1.cbegin (), Str1.cend ());// Use swap to exchange two identical containers vector<string> A1 (Ten);vector<string> A2 (A1,A2);//Add elements from the tail string word;vector <string> Contains;while (Cin>>word) {contains.push_back (word);}//Insert element from head list<string> li1;while ( Cin>>word) {Li1.push_front (word);} Insert vector<string> v2;list<string> Li2;li2.insert (Li2.begin (), "Hello") from a specific location in the container; Li2.insert (V2.begin () , "Hello");//vector does not support inserting from the head but can be inserted in this way, but this speed is very slow//forward_list use forward_list<string> fist={ 1,23,4,5,6,6,7,7,8,8,8,665,56,789,6,6,5,5,99,65,78};auto One=fist.before ();//Current Element auto Two=fist.begin (); while ( Curr!=fist.end ()) {if (*curr%2) Curr=fist.erase_after (one); else One=curr;++curr; }*///Manage capacity member functions vector<int> num;cout<< "size () =" <<num.size () <<endl;cout<< "capacity () = "<<num.capacity () <<endl;for (Vector<int>::size_type i=0;i!=40;i++) {num.push_back (i+23);} cout<< "size () New 1=" <<num.size () <<endl;cout<< "Capacity () New 1=" <<num.capacity () < <endl;num.reserve (;cout<<) "size () New 2=" <<num.size () <<endl;cout<< "Capacity () New 2=" < <num.capacity () <<endl;//What happens if you add data? for (int i=0;i<60;i++) num.push_back (67+i) Num.insert (Num.begin (), 12334);cout<< "Add size () =" << Num.size () <<endl;cout<< "add capacity () =" <<num.capacity () <<endl; Return memory allocated out of memory Num.shrink_to_fit ();cout<< "size () =" <<num.size () <<endl;cout<< "after return capacity  () = "<<num.capacity () <<endl; The data in the data cout<< "container in the vector is read:"; for (int i=0;i<num.size (); i++) {cout<<num[i]<< "";} Extra action for string constChar *cp= "Hello world!"; Char nonull[]={' h ', ' I '};string S1 (CP); string S2 (cp,6,20);cout<<s1<< "" <<s2<< "" <<endl ; string S ("Hello world!"); String S3=s.substr (0,7); S.insert (S.size () -2,5, '? '); cout<<s<< "" <<endl;s.erase (S.size () -5,5);cout<<s<< "" <<endl;// Assgin method S.assign (cp,13);cout<<s<< "" <<endl;//append Method S2.append ("Ffh,sbx fy and FY");cout<< s2<< "" <<endl;s2.replace (11,4, "FFH,SBX");cout<<s2<< "" <<endl;//string Search operation string Name ("Anfsdkjhgjs dfngndfmgnd sdghsdfjg"), Nu ("Dhfhj"); auto Posl=name.find ("JS");//All matches cout<<posl<< Endl;auto po=nu.find_first_of (name);//either Match Cout<<po<<endl;system ("pause"); return 0;}


(End of full text)

Summary of C + + sequential container learning

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.