[ create a new paste ] login | about

Link: http://codepad.org/THFGFCCk    [ raw code | output | fork ]

C++, pasted on Nov 20:
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>

using namespace std; 
class A { 
    int i; 
public: 
    A() { 
    }; 
    A(const A& a) { 
        i = a.i; 
        cout << "copy constructor invoked" << endl; 
    }; 
    A(int num) { 
        i = num; 
    }; 
    A& operator = (const A&a) { 
        i = a.i; 
//      cout << "assignment operator invoked" << endl; 
        return *this;
    }; 
    ~A() { 
        cout << "destructor called" << endl; 
    }; 
    friend ostream& operator << (ostream & out, const A& a); 
    friend istream& operator >> (istream &in, A&a); 
}; 
 
ostream & operator << (ostream &out, const A& a) { 
    out << a.i; 
    return out; 
} 
 
istream & operator >> (istream & in, A&a) { 
    in >> a.i; 
    return in; 
 
} 
int main() { 
    vector<A> vA; 
    copy(istream_iterator<A>(cin), istream_iterator<A>(), back_inserter(vA)); 
//  copy(vA.begin(), vA.end(), ostream_iterator<A>(cout, "\t")); 
    return 0; 
} 


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked
copy constructor invoked
destructor called
destructor called
destructor called
destructor called
destructor called
destructor called
destructor called
destructor called
destructor called
destructor called


Create a new paste based on this one


Comments: