C++ Primer of Chapter 9 sequential container

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

 Reference resources:

1.. The sequential containers: vector (fast random access); list (rapid insertion deletion); deque (double ended queue)

2.Sequential container adaptors: stack LIFO, last in first out stack; queue, priority_queue priority queue queue;

3. Initialization:

     a.  C<T> C: creates the empty container named C, applicable to all vessels

     b.  C c(c2): Create C2 as a copy of container, C and C2 have the same container type and the type of the elements in the container are also the same, applicable to all vessels

     c.   C c(b,e): Copy B and e iterator range, applicable to all vessels

     d.  C c(n,t): Using the n values to the elements of the T to initialize the C container, applies only to the sequential containers

     e.  C c(n): Using n value initialized elements container C, applies only to the sequential containers

   f.

Container: vector<vector<string> (there must be a space, otherwise it becomes the shift operators > > a) >

4.The iterator operations

a.*iter: Value

b.->:Dereference, MEM is equivalent to it->(*it).mem

c.++ --

d.== !=

e.The following applies only to vector and deque container

(1). iter +/- n

(2). iter +/-=n

(3). iter1-iter2

(4). >,>=,<,<=

5.Containers define a typedef:

a.The length of size_type

b.iterator, const_iterator,reverse_iterator,The const_reverse_iterator sequence, a sequence, reverse, a reverse iterator

c.The difference_type iterator difference

d.The value_type element type

e.The reference element type lvalue types, equivalent to value_type &

f.Const_reference constants are lvalues, namely const_value_type &

6.Container operation

a.begin() end() rbegin() rend():If the container is const, it returns const_iterator

b.Adding elements

(1). push_back(t): Adding value to the elements of the t at the end of the C container, return type void

(2). push_front(t): Adding value to the elements of the T in front of the C container, void return type, applies only to list and deque container

(3). insert(p,t): The front c.insert P element into a new element with value T, return the iterators to add new elements

(4). insert(p,n,t): Before c.insert P elements into the n elements with value T, return type void

(5). insert(p,b,e): The front c.insert P elements into the scope of the iterator B and e labeled elements within the. Return type void

c.The size of the operation

(1). c.size() Returns the number of elements in the container C, c: return type: size_type

(2). c.max_size() Returns the container C can accommodate the number of elements, the return type is c:: size_type

(3). c.empty() Returns a Boolean labeled container size if the value 0

(4). c.resize(n) Adjust the container size is n, if n is less than the original size extra element, remove or add value, the initialization of new elements

(5)Adjust the size of the container, period can contain n elements, all of the new elements to add value for t

  d.To access the element

  (1). c.back() Reference finally returns an element of the container C, if the C is empty, the operation is not defined

  (2). c.front() Returns a reference to the first element of the C, if the C is empty, the operation is not defined

   (3). c[n],c.at(n)Returns a reference to the N element. If the index out of bounds, the operation is not defined

  f.The delete operation

   (1) c.erase(p) Remove the element referred to by the iterator P, returns an iterator pointing to the rear element deleted elements; if the P itself is point to the last element in the container, iterator is returned to the container off the end iterator position. If the P itself is point one past the end of the position of the iterator, the function is not defined

  (2) c.erase(b,e) Removes all of the elements of B and e mark the iterator range, returns an iterator, just want to be deleted element behind element. If the e itself is point one past the end of the location of the iterator returned to the container, iterator or the off the end iterator position

  (3) c.clear() Delete all the elements in the container C, return void

  (4) c.pop_back() Removes the last element in the container C, return void. If the C is empty containers, the operation is not defined

  (5) c,pop_front()  Delete the C container the first element, return void. If the C is empty containers, the operation is not defined, applies only to list and deque container

  (6) Delete operation failure happened iterator

  e.Assignment andswap

   (1)All elements of the C1 = C2. Delete the C1 container, and then C2 the original elements assigned to the c1.c1 and C2 types (including container types and elements must be the same type)

  (2). c1.swap(c2) Exchange content: call play the function, C1 is stored in the original C2 elements, C2 stored in the C1 is the original elements. Type C1 and C2 must be the same

   (3). c.assign(b,e) To set the C element: the iterator B and e markers of Nene all elements are copied to the C. B and e must not only think iterator of the elements of the C

  (4). c.assign(n,t) The C container to store n values to the elements of the T

7.vectorSelf growth

a.Capacity and reserve

b.The value of size is the current size, capacity refers to the total number of new storage space must be allocated before can be stored elements

c.capacity>=size

8. Container using ( vector are generally the best choice)

a.Random access: vector or deque

b.Insert or delete: list

c.Both insert and delete

d.When reading in the middle to insert, and then require random access, can choose list, and then copy it to a vector

9.Talk aboutstring

a.The other way of constructing string

(1). string s(cp,n)Initialization for CP before n copy

(2). string s(s2,pos2)The character copy, create S2 starting at pos2 if pos2> s2.size (), the operation is not defined

(3). string s(s2,pos2,len2) Create a S2 from the subscript pos2 length as a copy of len2

b.The modification of other methods of string

(1) s.insert(pos,n,c)The subscript POS before element inserted n character c

(2) s.insert(pos,s2)Insert copy of the string S2 object in the subscript POS before element

(3) s.insert(pos,s2,pos2,len) Insert the S2 from the subscript pos2 start len characters in the subscript POS before element

(4) s.insert(pos,cp,len)Insert the CP array pointed to by the first len characters for POS elements before.

(5) s.insert(pos,cp)Insert copy of null terminated string pointed to by CP in the subscript POS before element

(6) s.assign(s2)Replace s with a copy of S2

(7) s.assign(s2,pos2,len) Len string copy with S2 from pos2 to replace the S.

(8) s.assign(cp,len)Before the len character copy using the array pointed to by CP to replace s

(9) s.assign(cp) Copy of null terminated string pointed to by CP to replace s

(10) s.erase(pos,len) Delete from POS len characters.

Note: the above operation except special declaration, return s references

c.Is only used for string type of operation

(1) s.substr(pos,n) Returns a string of type string, which contains the s from the table POS to n characters

(2) s.substr(pos) Returns a string of type string, which contains all the characters from the subscript to s at the end of the

(3) Copy s.substr to return s

(4) s.append(args)

(5) s.replace(pos,len,args) Remove s from the subscript POS start len characters, using the args formulation character replacement. Return s.

(6) s.replace(b,e,args) All the characters in B and e to delete the iterator range marker, args replacement, return s

(7) Args description:

 s2 : The string S2

  s2, pos2, len2 : S2 string from pos2 to len2 characters.

  cp : The end of the array to a null pointer CP.

  cp, len2: Null terminated array pointed to by CP in the first len2 characters

n, c: The character c n copies

b2, All the characters are in the range E2 iterator B2 and E2 markers in

d.stringType of lookup operations, return typesize_type

(1) find(args) Find the first occurrence of args in S

(2) rfind(args) Find last occurrence of args in S

(3) find_first_of(args) Any character to find args first appeared in S

(4) find_last_of(args) The last time an arbitrary character to find args in S

(5) find_first_not_of(args) Find the first does not belong to the args characters in S

(6) find_last_not_of(args) Find the last does not belong to the args characters in S

(7) Args description:

c, pos: Start subscript positions of the POS markers in the s, look for the character C. The default value for POS is 0

s2, pos: In s, start from the table below the position of the POS mark, the default for string object s2.pos has the value 0

cp, POS s, from the time table POS marked position, the end of the string to be null lookup pointer pointed to by CP c style, the default value for POS is 0

cp, pos, n: In S: start from the subscript POS marked position, seek pointer array pointed to by CP n characters. POS and n have no default value

(8) String operation case sensitive

e. stringThe comparison operation: the compare function

(1)  s.compare(s2) Comparison of S and S2

(2)  s.compare(pos1,n1,s2)Let s from POS subscript N1 at the start of the characters compared with S2

(3)  s.compare(ps1,n1,s2,pos2,n2) Lets start with pos1 subscript position s of N1 characters and S2 from the pos2 subscript position N2 character comparison

(4)  s.compare(cp) The end of the string to be null in comparison to s and CP to

(5)  s.compare(pos1,n1,cp) Reference resources(2)

(6)  s.compare(pos1,n1,cp,n2) Reference resources(3)

10. Container adaptors:

   a. Specify a sequential container into second type arguments adapter, can override the default base container, e.g. stack<string, vector<string> > str_stk;

   b.Stack adapter

    (1) empty() If the stack is empty, it returns true, otherwise it returns false

    (2) size()  Returns the number of elements in the stack

    (3) pop()  Remove the top element on the stack, but does not return to its value

    (4) top()   Returns the top element on the stack but calcium is not deleted

    (5) push(item)  In the top of the stack is pressed into the elements

   c.Queue

    (1) empty()  The same stack

    (2) size()  The same stack

    (3) pop(): Delete the first team

    (4) front(): Return to the first team, does not remove

    (5) back(): Returns the tail, not to delete

    (6) push(item), The tail of the queue insertion

   d.Priority queue

    (1) empty() The same queue

    (2) size():  The same queue

    (3) pop():  The same queue

    (4) top(): Returns the highest priority element

    (5) push(item):Insert a new element in the appropriate position based on priority

 

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Herman at November 12, 2013 - 6:51 PM