1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <vector>
#include <initializer_list> // C++11 only
struct A
{
A() {} // 1. initialize to empty vector
// 2. initialize to a vector of size n, each element being v
A( std::size_t n, int v = 0 ) : sequence(n,v) {}
// 3. initialize from pointer to first element of int array and its size
A( const int* a, std::size_t n ) : sequence( a, a+n ) {}
// 4. initialize from a polymorphic sequenceuence abstracted away with iterators
template< typename ITERATOR >
A( ITERATOR begin, ITERATOR end ) : sequence(begin,end) {}
// 5. initialize from array via initializer list - C++11 only
A( std::initializer_list<int> ilist ) : sequence(ilist) {}
// 6. initialize from array with compatible type
template< typename T, std::size_t N > A( T(&a)[N] ) : sequence( a, a+N ) {}
std::vector<int> sequence ;
};
int main()
{
A a1 ; // 1
A a2( 10U, 99 ) ; // 2
const int a[] = { 0, 1, 2, 3, 4 } ;
A a3( a, sizeof(a)/sizeof(int) ) ; // 3
const short b[] = { 5, 6, 7, 8 } ;
A a4( b, b + sizeof(b)/sizeof(short) ) ; // 4
A a5 = { 10, 11, 12, 13, 14, 15, 16 } ; // 5
A a6(b) ; // 6
}
|