Template array initialization with a list of values

前端 未结 5 1270
一向
一向 2021-01-14 14:14

In standard c++ we can write :

int myArray[5] = {12, 54, 95, 1, 56};

I would like to write the same thing with a template :



        
5条回答
  •  天命终不由人
    2021-01-14 14:34

    Yet another solution which doesn't need adder class template. Now you can do this:

    int main() {
    
            Array array;
            array = 1,2,3,4,5,6,7,8,9,10;
            for (size_t i = 0 ; i < array.Size() ; i++ )
               std::cout << array[i] << std::endl;
            return 0;
    }
    

    Output:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    

    Here is the complete solution: http://www.ideone.com/I0L1C

提交回复
热议问题