template<class C> void print(const C& bla) { for(auto& elmt : bla) { std::cout << elmt << std::endl; } std::cout << std::endl; } struct Int { Int(int i) : m_i(i) { std::cout << "creating " << m_i << std::endl; } Int(const Int& j) : m_i(j.m_i) { std::cout << "copying " << m_i << std::endl; } ~Int() { std::cout << "deleting " << m_i << std::endl; } operator int() const { return m_i; } int m_i; }; int main(int argc, char* argv[]) { std::cout << "|\n|\n"; std::string s1 = "hello "; std::string s2 = "world "; std::string&& s3 = s1 + s2; s3 += "friend "; std::cout << s3 << std::endl; s2 = "my "; std::cout << s3 << std::endl; { std::vector<Int> V = { 1, 2, 3, 4, 5, 6 }; print(V); std::list<Int> L = { 7, 8, 9 }; print(L); std::set<Int> S = { 10, 11, 12 }; print(S); auto it = std::copy(L.begin(), L.end(), V.begin()); print(V); std::copy_if(S.begin(), S.end(), it, [](const Int& a){ return a % 2; }); print(V); } }
produces
creating 1 creating 2 creating 3 creating 4 creating 5 creating 6 copying 1 copying 2 copying 3 copying 4 copying 5 copying 6 deleting 6 deleting 5 deleting 4 deleting 3 deleting 2 deleting 1 1 2 3 4 5 6 creating 7 creating 8 creating 9 copying 7 copying 8 copying 9 deleting 9 deleting 8 deleting 7 7 8 9 creating 10 creating 11 creating 12 copying 10 copying 11 copying 12 deleting 12 deleting 11 deleting 10 10 11 12 7 8 9 4 5 6 7 8 9 11 5 6 deleting 12 deleting 11 deleting 10 deleting 7 deleting 8 deleting 9 deleting 7 deleting 8 deleting 9 deleting 11 deleting 5 deleting 6