生活随笔
收集整理的這篇文章主要介紹了
C++11列表初始化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
列表初始化: 1.舊語法中定義了初始化的幾種不同形式,如下: int data = 0; //賦值初始化 int data = {0}; //花括號初始化 int data(0); //構造初始化 int data{0}; //花括號初始化
2.C++11以舊語法中花括號初始化形式為基礎,設計了列表初始化語法,統一了不同的初始化形式。 數據類型 變量{初始化列表}
#include <iostream>
#include <iterator>
using namespace std; struct Student
{ char name[
256 ]; struct Date{ int year; int month; int day;}bday;
}; class Complex
{
public :Complex( double r =
0 ,
double i =
0 ) :m_r(r), m_i(i) {}friend ostream &
operator <<(ostream &os, Complex
const &
that){ return os << that.m_r <<
" + " << that.m_i <<
" i " ;}Complex(Complex const &
that) :m_r(that.m_r), m_i(that.m_i){cout <<
" 拷貝構造函數 " << &that <<
" -> " <<
this <<
endl;}
private : double m_r, m_i;
}; int main()
{ int a{
123 };cout << a <<
endl; double b{
3.4567 };cout << b <<
endl; int c[]{
100 ,
200 ,
300 };copy(c, c +
sizeof (c) /
sizeof (c[
0 ]), ostream_iterator<decltype(*c)>(cout,
" " ));cout <<
endl;Student d{ " 張飛 " ,{
2000 ,
1 ,
1 } };cout << d.name <<
" , " << d.bday.year <<
" - " << d.bday.month <<
" - " << d.bday.day <<
endl;Complex e{ 1.2 ,
3.4 };cout << e <<
endl;Complex *f =
new Complex{
1.2 ,
3.4 };cout << *f <<
endl; delete f;f =
new Complex[
3 ]{ {
1.1 ,
2.2 },{
2.2 ,
3.3 },{
3.3 ,
4.4 } };copy(f, f +
3 , ostream_iterator<decltype(*f)>(cout,
" " ));cout <<
endl; delete [] f;cout << Complex{
1.2 ,
3.4 } <<
endl; // Complex const (&h)[3]{{ 1.1, 2.2 }, { 2.2, 3.3 }, { 3.3, 4.4 }}; Complex
const h[
3 ]{ {
1.1 ,
2.2 },{
2.2 ,
3.3 },{
3.3 ,
4.4 } };copy(h, h +
3 , ostream_iterator<decltype(*h)>(cout,
" " ));cout <<
endl;Complex i =
e; // Complex i = Complex(1.2, 3.4); cout << i <<
endl; return 0 ;
} 3. 變長初始化表,initializer_list
#include <iostream>
#include <iterator>
#include <vector>
#include <map>
using namespace std; class A
{
public :A(initializer_list <
int >
li){ for (auto it = li.begin(); it != li.end(); ++
it){m_vi.push_back( *
it);}}friend ostream &
operator <<(ostream &os, A
const &
that){copy(that.m_vi.begin(), that.m_vi.end(), ostream_iterator <decltype(that.m_vi[
0 ])>(os,
" " )); return os;}
private :vector <
int >
m_vi;
}; int average(initializer_list<
int >
scores)
{ if (!
scores.size()) return 0 ; int sum =
0 ; for (auto it = scores.begin(); it != scores.end(); ++
it)sum += *
it; return sum /
scores.size();
} int main()
{ char const *a[]{
" 張飛 " ,
" 趙云 " ,
" 關羽 " ,
" 黃忠 " ,
" 馬超 " };copy(a, a +
sizeof (a) /
sizeof (a[
0 ]), ostream_iterator<decltype(a[
0 ])>(cout,
" " ));cout <<
endl;vector <
const char *> b{
" 張飛 " ,
" 趙云 " ,
" 關羽 " ,
" 黃忠 " ,
" 馬超 " };copy(b.begin(), b.end(), ostream_iterator <decltype(b[
0 ])>(cout,
" " ));cout <<
endl;map <
const char *,
int > c{ {
" 張飛 " ,
100 },{
" 趙云 " ,
50 },{
" 關羽 " ,
25 } }; for (auto it = c.begin(); it != c.end(); ++
it)cout << it->first <<
" : " << it->second <<
endl; /* for (map<const char *, int>::iterator it = c.begin(); it != c.end(); ++it)cout << it->first << ":" << it->second << endl; */ A a1{ 1 ,
3 ,
5 ,
7 ,
9 };cout << a1 <<
endl;A a2{ 2 ,
4 ,
6 ,
8 ,
10 };cout << a2 <<
endl; int d =
60 , e =
70 , f =
80 ;cout << average({ d,e,f }) <<
endl;cout << average({
50 ,d, e, f,
90 }) <<
endl;getchar(); return 0 ;
} ?
4.聚合類型 (4.1)任意類型的數組 (4.2)滿足特定條件的類: a 無自定義的構造函數 b 無私有或者保護的非靜態成員變量 c 無基類 d 無虛函數 e 無通過“=”或者“{}”在類聲明部分被初始化的非靜態成員變量 (4.3)聚合類型的元素或者成員可以是聚合類型也可以是非聚合類型 (4.4)對聚合類型使用列表初始化,相當于對其中的元素逐一初始化, 而對非聚合類型使用列表初始化,相當于用列表初始化的值作為參數,調用相應的構造函數。
5.initializer_list作為輕量級的列表容器,不但可以用在構造函數中, 也可以作為普通函數的參數,傳遞不定數量的實參,相對于傳統標準容器, 效率更高(輕量級列表容器,僅保存初始化列表元素的引用,而非其副本)
#include <iostream>
#include <iterator>
#include <list>
using namespace std; // 輕量級列表容器內部存放初始化列表元素的引用而非其拷貝
initializer_list<
int > light(
void )
{ int a =
1000 , b =
2000 , c =
3000 ; // 返回局部變量的引用將在函數返回以后失效 return { a,b,c };
} // 重量級容器內部存放初始化列表元素的拷貝而非其引用
list<
int > heavy(
void )
{ int a =
1000 , b =
2000 , c =
3000 ; // 所返回局部變量拷貝在函數返回后繼續有效 return { a, b, c };
} int main()
{ // 可以接受任意長度的初始化列表,但列表中元素的類型必須相同 initializer_list<
int > initlist{
10 ,
20 ,
30 ,
40 ,
50 ,
60 }; // initilizer_list只有三個公有成員:begin,end,size copy(initlist.begin(), initlist.end(), ostream_iterator<decltype(*initlist.begin())>(cout,
" " ));cout <<
" [ " << initlist.size() <<
" ] " <<
endl; // 迭代器為只讀類型,其目標元素不可修改 /* for (auto it = initlist.begin(); it != initlist.end();++it){*it *= 100;} */ // 可以對容器整體賦值 initlist = {
100 ,
200 ,
300 };copy(initlist.begin(), initlist.end(), ostream_iterator <decltype(*initlist.begin())>(cout,
" " ));cout <<
" [ " << initlist.size() <<
" ] " <<
endl; // 提供缺省構造函數,用于實例化空容器 initlist =
{};copy(initlist.begin(), initlist.end(), ostream_iterator <decltype(*initlist.begin())>(cout,
" " ));cout <<
" [ " << initlist.size() <<
" ] " <<
endl;initlist =
light();copy(initlist.begin(), initlist.end(), ostream_iterator <decltype(*initlist.begin())>(cout,
" " ));cout <<
" [ " << initlist.size() <<
" ] " <<
endl;list <
int > li =
heavy();copy(li.begin(), li.end(), ostream_iterator <decltype(*li.begin())>(cout,
" " ));cout <<
" [ " << li.size() <<
" ] " <<
endl; return 0 ;
} ?
?
6.列表初始化可以防止類型收窄,即對可能造成信息損失的類型轉換,提示警告或者直接報錯 long double ld = 3.1415926; int a{ld},b{ld}; //error,轉換未執行,因為存在丟失信息的危險 int a(ld),b(ld); //true,轉換執行,且確實丟失了部分值
轉載于:https://www.cnblogs.com/LuckCoder/p/8467656.html
總結
以上是生活随笔 為你收集整理的C++11列表初始化 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。