C + + 11

Source: Internet
Author: User

1.forward_list one-way linked list member function constructors
Constructor Forward_list (public member function)
Destructors
destructor Forward_list (public member function)
= Operation operator
Assigning content (public member functions)
Iterators
Before_begin
returns the pointer to the iterator before the start (Public member function)
Begin
returns the pointer to the beginning of the iterator (public member type)
End
returns the pointer to the end of the iterator (public member function)
Cbefore_begin
returns a pointer to a constant iterator before the start (public member function)
Cbegin
returns an iterator pointer to the beginning of a point (public member function)
Cend
returns a pointer to the end of a constant iterator (public member function)
Capacity
Empty
determine if NULL (public member function)
Max_size
returns the maximum value of the capacity (public member function)
Acquisition of elements
Front
get the first element value (public member function)
Modifier
Assign
Assigning content (public member functions)
Emplace_front
construct and insert elements to the first position (public member functions)
Push_front
insert element to first position (public member function)
Pop_front
Delete the first position element (public member function)
Emplace_after
constructing and inserting elements (public member functions)
Insert_after
Insert Element (public member function)
Erase_after
Erase Element (public member function)
Swap
Interchange Content (public member function)
Resize
Change capacity size (public member function)
Clear
Clear Content (public member function)
Operation
Splice_after
move an element from another forward linked list (public member function)
Remove
Delete an element of a specific value (public member function)
Remove_if
Delete a qualifying element (Public member template function)
Unique
Delete duplicate values (member functions)
Merge
Merge Sort list (public member function)
Sort
ordering elements in a container (public member functions)
Reverse
reverse the order of elements (public member functions)
Observers
Get_allocator
Get Allocator (public member function)
Global functions
Operators (Forward_list)
Global relational operational functions for linked lists (function templates)
Swap (Forward_list)
swapping the contents of two forward linked lists (function templates)
..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ..... ...
.. ..... .........
2.The erase function is prototyped as follows:
(1) string& erase (size_t pos = 0, size_t n = NPOs);
(2) Iterator erase (iterator position);
(3) Iterator erase (iterator first, iterator last);
That means there are three ways to use:
(1) Erase (pos,n); Deleting n characters starting from POS, such as erase (0,1), deletes the first character
(2) Erase (position); Deletes a character at position (position is a string type of iterator)
(3) Erase (first,last); Delete the characters from first to last (both first and last are iterators)

The function remove_if () Removes all elements in the sequence [start, end] that are applied to the predicate p to return true.

This function returns a pointer to the last element iterator of the trimmed sequence.

remember that remove_if () does not actually remove elements from the sequence [start, end], and if remove_if () is applied on a container, the length of the container does not change (remove_if () is not possible to change the container's properties only through iterators), All the elements are still inside the container. As a practical practice, remove_if () moves all elements that should be removed to the end of the container and returns a delimited iterator. All of the removed elements can still be accessed through the returned iterator. In order to actually remove the element, you must call erase () on the container itself to erase the element that needs to be removed. This is also the origin of Erase-remove idiom name:

Container.erase (Remove_if (Container.begin (), Container.end (), pred), Container.end ());

Remove_if () is similar to partition (), but has a difference of two points: 1) They use a predicate condition that is exactly the opposite. 2) The remove_if only emphasizes the previous part (the second part is no longer needed).

Remove_if () runs at linear time (linear times).

REMOVE_IF () cannot be used for associative containers such as set<> or map<>.

3.swap

A non-member version of the swap operation is provided in the new standard, which swaps the value of the element for the array container, and for other containers, swaps only the internal structure of the container and does not copy the element value, so it is very fast in this case.

Because of this, when the swap array, the iterator on the original array still points to the original element, but the value of the element has changed;
When swap is not an array container, the iterator on the original container will point to the element on the other container, and the value of the pointed element remains unchanged.

For vectors, the pointer is exchanged, but the contents of the container do not change, although it6_2 = C6.begin () when traversing, but It6_2 is actually a pointer to 7.
1#include <vector>2#include <iostream>3#include <algorithm>4#include <functional>5#include <forward_list>6 using namespacestd;7 intMain ()8 {9vector<int> c6 = {0,1,2,3,4};Tenvector<int> c7 = {5,6,7,8,9}; OneAuto It6_1 =C6.begin (); AAuto It7_1 =C7.begin (); - Swap (C6, C7); -  for(Auto It6_2 = C6.begin (); It6_2! = C6.end (); it6_2++) thecout<<*it6_2<<'\ t'; -cout<<Endl; -  -  for(Auto it7_2 = C7.begin (); It7_2! = C7.end (); it7_2++) +cout<<*it7_2<<'\ t'; -cout<<Endl; +  Acout<< (It6_1 = = C7.begin ()) <<'\ t'<< (It7_1 = = C6.begin ()) <<Endl; at  -array<int,5> C8 = {0,1,2,3,4}; -array<int,5> C9 = {5,6,7,8,9}; -Auto It8_1 =C8.begin (); -Auto It9_1 =C9.begin (); - swap (C8, C9); incout<< (It8_1 = = C8.begin ()) <<'\ t'<< (It9_1 = = C9.begin ()) <<Endl; -}
View Code

4. Emplace

Class TestData
{
Public
TestData (string name, int age, double salary): name, age, salary (salary)
{}
Private
String name;
int age;
Double salary;
};

Vector<testdata> C10;

C10.emplace_back ("Yubo", 26, 100000000000.0);
C10.push_back ("Laowang", 56, 10.5); Wrong. No 3 params push_back
C10.push_back (TestData ("Laowang", 56, 10.5));
Cout<<c10.size () <<endl;
}

You don't have to pass the object.

Shrink_to_fit

A generally variable-length container allocates a portion of the memory in advance so that it does not need to request memory each time the element is added later. So there is the size and capacity of the points. The size is the number of elements that exist in the current container, while capacity is the maximum number of elements currently available without re-applying memory. Shrink_to_fit, in turn, returns the excess part of the capacity back to size. However, the specific effect of this function depends on the implementation of the compiler ...

vector<int> c11; for(int i = 0;  I < ; I+ +) C11. Push_back(i); cout<<c11. Size()<<' \ t '<<c11. Capacity()<<Endl; C11. Shrink_to_fit(); cout<<c11.< Span class= "CRAYON-E" >size (<<< Span class= "crayon-s" > ' t ' <<c11.< Span class= "CRAYON-E" >capacity (<< endl

For the fixed-length container is not applicable array<int 5>...size is 5,.

Unordered associative container:

#include <map>
#include <iostream>
#include <algorithm>
#include <functional>
#include <forward_list>
#include <unordered_map>

using namespace Std;
int main ()
{
Unordered_map<string, int> C12;
Map<string, int> C13;
String String_keys[5] = {"AAA", "BBB", "CCC", "ddd", "Eee"};
for (int i = 0; i < 5; i++)
{
C12[string_keys[i]] = i;
C13[string_keys[i]] = i;
}
for (Auto it13 = C13.begin (); it13! = C13.end (); it13++)
cout<<it13->first<< ': ' <<it13->second<< ' \ t ';
cout<<endl;
cout<< "Unordered map:\n";
for (Auto it12 = C12.begin (); It12! = C12.end (); it12++)
cout<<it12->first<< ': ' <<it12->second<< ' \ t ';
cout<<endl;
}

SHARED_PTR Smart pointer

Shares_ptr<string> ptr=make_shared<string> ("Dfsdf");

Shared_ptr can be initialized with a pointer returned by a new expression, but cannot be assigned a value with the returned pointer, and shared_ptr can reset to another object by resetting the method, at which point the reference count of the original object is reduced by one.

SHARED_PTR uses reference counting to manage the objects that are pointed to. When a new shared_ptr points to the same object (copy shared_ptr, etc.), the reference count is added to 1. The reference count is reduced by 1 when shared_ptr leaves the scope. When the reference count is 0 o'clock, the managed memory is freed.

The advantage of this is that it frees up the programmer's pressure to manually release memory. Before, in order to deal with exceptions in the program, it is often necessary to manually encapsulate the pointer into the class, through the destructor to release the dynamically allocated memory; Now this process can be handed over to shared_ptr.

using namespace Std;
int main ()
{
cout<< "Test shared_ptr base usage:" <<endl;
shared_ptr<string> P1 = make_shared<string> ("");
if (P1 && p1->empty ())
*P1 = "Hello";

Auto P2 = make_shared<string> ("World");
cout<<*p1<< ' <<*p2<<endl;

cout<< "Test shared_ptr use_count:" <<endl;
cout<< "P1 cnt:" <<p1.use_count () << "\TP2 cnt:" <<p2.use_count () <<endl;

Auto P3 = p2;
cout<< "P1 cnt:" <<p1.use_count () << "\TP2 cnt:" <<p2.use_count () << "\TP3 cnt:" << P3.use_count () <<endl;
P2 = p1;
cout<< "P1 cnt:" <<p1.use_count () << "\TP2 cnt:" <<p2.use_count () << "\TP3 cnt:" << P3.use_count () <<endl;
}

Copy construction:

Class TestClass
{
Public
TestClass () = default;
TestClass (const int i, const char c): Member_i (i), Member_c (c) {}
TestClass (const int i): TestClass (i, 0) {member_c = ' T ';} (Commissioned construction)
TestClass (const testclass&) = default;
TestClass operator= (const testclass&);

int Member_i;
Char member_c;
};


int main ()
{

TestClass TC2 (2);
cout<< "Test =default class copy construct:\n";
TestClass TC3 (TC2);
TestClass TC4 = TC2;
cout<<tc3.member_i<< ' \ t ' <<tc3.member_c<<endl;
cout<<tc4.member_i<< ' \ t ' <<tc4.member_c<<endl;
}



C + + 11

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.