C++11 the list of new features.

Source: Internet
Author: User

Directory

1. Initialize the list 2. Initialize class {} is not a feature that is exclusive to the built-in types, arrays, and containers in the standard Template library, you can initialize the custom Class 3. The list of parameters for the function can also be used with the initialization list

1. Initializing the list

In c++98, the standard allows you to use the {} curly braces to set the set initializer for a uniform collection (set) of array elements. Such as:

Char arr[10] =  {0};        The 10 element value in the array is 0
char buf[] = {1,3,5,7,9};   The array size is 5, respectively: 1,3,5,7,9        

However, for some custom data types, it cannot be compiled, and the following initialization of the {} list to initialize the vector is an error.

Vector<int> VEC = {1,23,45,55,66,77};   c++11 the new attribute  list initialization is
equivalent to:
vector<int> {1,23,45,55,66,77};

(1) c++11 Start, you support the use of {} curly braces to initialize the custom type, called: List initialization

/************************************************************************* * File Name:newMap.cpp * author:the ANSW ER * function:other * mail:2412799512@qq.com * Created time:2018 year April 24 Wednesday 01:00 03 sec ************** /#include <iostream> #include <map> #include
<string> #include <list> #include <set> using namespace std; int main (int argc,char **argv) {map<int,string> _map{{1, "Hello"},{2, "Wrold."}; 
    = = Map<int,string> _map = {1, "Hello"},{2, "Wrold."}; for (Auto it = _map.begin (); it!= _map.end (); ++it) {cout<< "" "<<it->first<<" "
        ;< "Second:" <<it->second<<endl;
         /*first:1 Second:hello *first:2 Second:world.
    */} list<int> _list{1,2,3,4,5};
        for (auto val:_list) {cout<<val<< ",";   /*1,2,3,4,5*/}

    Set<string> _set = {"Li", "Xiao", "Gang"};
        for (auto Val:_set) {cout<<val<< ",";
/*gang, Xiao, Li Note: not Li,xiao,gang;set will default sort (small---> Large)/} return 0;
 }

As you can see from the above code, C++11 this list initialization makes the code more clear and concise, such as the initialization of map maps, without this "list initialization", you have to use the insert one to insert elements into the map, this is a tedious repetitive work. Obviously the new features of C++11 's list initialization have brought a lot of benefits.

(2) initialization of automation variables and global variables is enriched in c++11
① equals = plus an assignment expression (assignment-expression).
② equals = The initialization category with curly braces {}.
An expression list (expression-list) for ③ parentheses ().
④ the initialization list for the curly brace {} type.
The following 4 variables are initialized in different ways: ①②③④

/*************************************************************************
 * File Name:init.cpp
 * Author: The    answer
 * Function:  Other        
 * Mail:      2412799512@qq.com 
 * Created time:2018 Year, Wednesday, April 24 01:12 06 seconds
 ************************************************************************/

#include < Iostream>
using namespace std;
int main (int argc,char **argv)
{
    int a = 1+2;        ①     3 
    int b = {3+4};      ②     7 
    int C (5+6);         ③
    int d{7+8};         ④
    cout<<a<< "," <<b<< "," <<c<< "," <<d<<endl;
    return 0;
}

③④ These two initialization variable methods can also be used to get the heap memory new operator.

  int *f = new int (a);
  Double *g = new double{66.0f};
2. Initialize class {} is not a feature that is exclusive to the built-in types, arrays, and containers in the standard Template library, you can initialize a custom class

In c++11, standards always tend to support new features in a more general way. The container's support for the initialization list in the standard Template Library originates from the support of the Initialize_list class template in the:<initializer_list> header file. As long as we #include<initializer_list> the header file in the program and declare a constructor that takes the Initialize_list<t> template class as a parameter, you can also use the initialization list {} To initialize the custom class.

/************************************************************************* * File Name:Person.cpp * author:the ANSW ER * function:other * mail:2412799512@qq.com * Created time:2018 year April 24 Wednesday 01:23 46 sec ************** /#include <iostream> #include <vector> #

Include <initializer_list> using namespace std;
        Class Person {Public:person () = default;
            The constructor of person (initializer_list<int> _vec)//initializer_list {Auto it = _vec.begin ();
                while (it!= _vec.end ()) {m_vec.push_back (*it);
            ++it;
        } ~person () {if (0!= m_vec.size ()) m_vec.clear ();
                } void Printdata () {if (0!= m_vec.size ()) {for (auto Val:m_vec) {COUT&LT;&LT;VAL&LT;< ",";
}} private:vector<int> M_vec;
};
    int main (int argc,char **argv) {person per = {1,2,3,4,5,6,7};
    Per.printdata ();
return 0; Output Result: 1,2,3,4,5,6,7,
3. The list of parameters for the function can also be used to initialize the list
/*************************************************************************
 * File Name:param.cpp
 * Author : The    answer
 * Function:  Other        
 * Mail:      2412799512@qq.com 
 * Created time:2018 Year, Wednesday, April 11 07:20 43 seconds
 ************************************************************************/

#include < iostream>
#include <initializer_list>//can contain, or may not contain, a
using namespace std;
void Addfunc (initializer_list<int> param)
{
    Auto it = Param.begin ();
    int sum = 0;
    for (; it!= param.end (); ++it)
    {   
        sum = *it;
    }   
    cout<< "sum =" <<sum<<endl;    sum = +
}

int main (int argc,char **argv)
{
    addfunc ({1,2,3,4,5,6,7,8});    
    return 0;
}

member functions in structs and classes can also use initialization lists, including overloaded functions for some operators, such as: [] = et cetera

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.