一些C++11语言新特性 - Uniform Initialization

1. Uniform Initialization

int values[] { 1, 2, 3 };
std::vector<int> v { 2, 3, 5, 7, 11, 13, 17 };
std::vector<std::string> cities {
"Berlin", "New York", "London", "Braunschweig", "Cairo", "Cologne"
};
std::complex<double> c{4.0,3.0}; // equivalent to c(4.0,3.0)

 

int i; // i has undefined value
int j{}; // j is initialized by 0
int* p; // p has undefined value
int* q{}; // q is initialized by nullptr

 

std::initializer_list<>

void print (std::initializer_list<int> vals)
{
    for (auto p=vals.begin(); p!=vals.end(); ++p) { // process a list of values
        std::cout << *p << "\n";
    }
}

// call func
print ({12,3,5,7,11,13,17}); // pass a list of values to print()

 

posted @ 2015-06-29 16:48  Master HaKu  阅读(342)  评论(0编辑  收藏  举报