C ++ standard library (2)

Source: Internet
Author: User

13. Summary of various containers:

1). Vector Operation:

■ Vector deletion and insertion element performance:

◆ Install or remove elements at the end of the container

◆ Capacity should be large at the beginning

◆ Calling multiple elements at a time is faster than calling multiple times.

■ C ++ guarantees exceptions when calling a vector function:

◆ This function does not work if an exception occurs during push_back plugging.

◆ If the copy operation of an element does not throw an exception, the insert operation is either successful or ineffective.

◆ Pop_back never throws an exception

◆ If the element copy operation does not throw an exception, erase and clear do not throw an exception

◆ SWAp does not throw an exception

◆ If the element copy operation will never cause an exception, it is deemed that the operation is neither successful nor ineffective.

2) deques operation

■ Differences between deqeues and vector

◆ Deques does not provide capacity operations (capacity () and reserve ())

◆ Deques directly provides functions for inserting and deleting header elements (push_front () and pop_front ())

■ C ++ guarantees exceptions when calling the deques function:

◆ This operation has no effect if an exception occurs when an element is inserted with push_back or push_front.

◆ Pop_back and pop_front will not throw any exceptions

3) List Operation

■ Difference between list and vector and deques:

◆ List does not support random access. Therefore, it is slow to randomly traverse any element in the list.

◆ Inserting and removing elements at any position (not just at both ends) is fast.

◆ Inserting an element does not cause various pointers pointing to other elements, and references. iterators are invalid.

◆ List has such a processing method for a field, either successful or nothing happens.

The Remove Algorithm of list is faster than the Remove Algorithm of algorithm library. Use its built-in member functions whenever possible.

4). Set operation

■ Set is sorted by sorting criteria. The so-called sorting criteria have the following meanings:

◆ It must be. x <Y is true, and Y <X is false.

◆ It must be passed. x <Y, Y <Zè x <z

◆ It must be self-reversed: x <X is false

Note: <C ++ standard library> the set support = Operator, no matter whether its sorting criteria are the same. This statement cannot run successfully on vs2010 at least.

Set cannot use the remove operation. The cause is that overwriting cannot be completed. Therefore, you can only use the erase member function.

■ Difference between set and Multiset insert:

Pair <iterator, bool> insert (const value_type & ELEM) // return pair to indicate whether the insert is successful and where the new element is located.

Multiset deletes elements with the same value:

Iterator iter = contains. Find (ELEM );

While (ITER! = Contains. End)

{

Contains. Erase (ELEM );

}

■ Difference between set and sequence container

Erase: the former does not return values, and the latter returns the last element of the deleted element.

For details about erase, refer to the blog:

Http://blog.csdn.net/yuanweihuayan/article/details/6790516

5). Map operation:

■ Delete elements whose second is value:

Map <int, string> _ map_int_string;

_ Map_int_string.insert (pair <int, string> (1, "YSL "));

_ Map_int_string.insert (pair <int, string> (2, "LDH "));

_ Map_int_string.insert (pair <int, string> (5, "LDH "));

_ Map_int_string.insert (pair <int, string> (8, "LDH "));

_ Map_int_string.insert (pair <int, string> (4, "GFC "));

_ Map_int_string.insert (pair <int, string> (3, "ZXY "));

 

For (Map <int, string >:: iteratoriterpos = _ map_int_string.begin (); iterpos! = _ Map_int_string.end ();)

{

If (iterpos-> second = "LDH ")

{

_ Map_int_string.erase (iterpos ++ );

}

Else

{

++ Iterpos;

}

}

Example:

Class _ functortest

{

Public:

Enum cpm_enum {normal_cpm, nonormal_cpm };

_ Functortest (cpm_enum Param = nonormal_cpm): _ cvalue (PARAM ){}

Static
Bool cpm_fun (const
Char _ left, const
Char _ right)

{

Return toupper (_ left) <toupper (_ right );

}

Bool
Operator () (const string _ fleft, conststring _ fright)

{

If (_ cvalue = normal_cpm)

{

Return _ fleft <_ fright;

}

Else

{

Return lexicographical_compare (_ fleft. Begin (), _ fleft. End (), _ fright. Begin (), _ fright. End (), cpm_fun );

}

}

PRIVATE:

Const cpm_enum _ cvalue;

};

 

Int main ()

{

_ Functortestfun (_ functortest: nonormal_cpm );

Typedef Map <string, String, _ functortest> map_functor;

Map_functor_map_owner (fun );

/* Note the difference between this and the book */

_ Map_owner.insert (make_pair (
"Deutschland", "Germany "));

_ Map_owner.insert (make_pair (
"Deutsch", "German "));

_ Map_owner.insert (make_pair (
"Haken", "snags "));

_ Map_owner.insert (make_pair (
"Deutschland", "enterprise "));

For (map_functor: iterator iterpos = _ map_owner.begin (); iterpos! = _ Map_owner.end (); ++ iterpos)

{

Cout <"First:" <iterpos-> first <"Second:" <iterpos-> second <"\ n ";

}

System ("pause ");

Return 0;

}

Notes:

◆ Cpm_fun is a static member function

◆ Cpm_fun parameters: perform operations on atomic elements

◆ Difference between insert and =: insert does not overwrite the existing values, and = does.

◆ Use functortest as an imitation function (similar to less)

14. functions to be noted: reverse, reserve: the former is to reverse the container. The latter is the backup capacity, and the latter is only a vector, string operation.

15. Code doubts:

Istream_iterator <string> cinpos (CIN); // enter a character

Ostream_iterator <string> coutpos (cout, ""); // output a character, separated by Spaces

While (cinpos! = Istream_iterator <string> () // The input stream is not at the end.

{

Advance (cinpos, 2); // obtain cinpos + 2

If (cinpos! = Istream_iterator <string> ())

{

* Coutpos ++ = * cinpos ++; // output

}

}

The results of this program are normal from some point of view.

However, the problem is that the program outputs the specified string. Why do we need user input?

In addition, if I enter three characters, the program will not be able to output.

 

16. When using a function like a function or a single parameter, the same result should always be returned when you change the callback type. In other words, the callback type should not change its status because it is called.

17. Function adapter:

◆ Bind2nd: convert a binary imitation function to a one-dimensional imitation function. Generally, the second parameter is passed to the binary imitation function "pointed out by the first parameter" as the second parameter of the latter.

◆ Bind1lst converts a binary imitation function to a one-dimensional imitation function. Generally, the second parameter is passed to the binary inverse function "pointed out by the first parameter" as the first parameter of the latter.

Template <class T1>

Class lessfunctor: Public binary_function <t1, T1, T1>

{

Public:

T1 operator () (T1 comparevalue, T1 const_value) const

{

Return max (comparevalue, const_value );

}

};

Int main ()

{

Vector <int> _ vecint;

For (INT I = 0; I <10; ++ I)

{

_ Vecint. push_back (I );

}

Transform (_ vecint. Begin (), _ vecint. End (), ostream_iterator <int> (cout, ""), bind1st (lessfunctor <int> (), 3 ));

Cout <"\ n ";

Transform (_ vecint. Begin (), _ vecint. End (), ostream_iterator <int> (cout, ""), bind2nd (lessfunctor <int> (), 3 ));

System ("pause ");

Return 0;

}

18. The STL algorithm uses the overwrite mode instead of the insert mode.

19. When string involves an index, size_type should be converted to int... In addition, string reserve has the function of reducing elements (different from Vector)

 

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.