CafeM0ca

[C++]std::initializer_list 본문

Programming/C++

[C++]std::initializer_list

M0ca 2018. 6. 24. 14:49
반응형
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
#include <initializer_list>
#include <vector>
#include <iostream>
 
class EvenSequence
{
    public:
        EvenSequence(std::initializer_list<double> args)
        {
            if(args.size() % 2 != 0){
                throw std::invalid_argument("initializer_list should "
                        "contain even number of elements");
            }
            mSequence.reserve(args.size());
            for(auto value : args) {
                mSequence.push_back(value);
            }
        }
        void dump() const
        {
            for(auto value : mSequence) {
                std::cout << value << ", ";
            }
            std::cout << std::endl;
        }
    private:
        std::vector<double> mSequence;
};
 
 
int main()
{
    EvenSequence p1{1.02.0,3.0,4.0};
    p1.dump();
    return 0;
}
 
cs


반응형

'Programming > C++' 카테고리의 다른 글

[C++] this 포인터  (0) 2018.06.30
[C++] smart pointer  (0) 2018.06.27
[딥러닝]역전파  (0) 2018.06.10
[딥러닝] 인공신경망  (0) 2018.06.10
[C++] 디폴트 매개변수  (0) 2018.05.04
Comments