How to print a set and a list with the same print() function in C++

1 Answer

0 votes
#include <iostream>
#include <set>
#include <list>
#include <iterator>

using std::set;
using std::list;
using std::cout;
using std::endl;

template <typename T>
inline void print(const T &data)
{
	for (const auto &element : data) {
		cout << element << ' ';
	}
	cout << endl;
}

int main()
{
	set<int> st{ 1, 2, 3, 4 };
	print(st);

	list<int> lst{ 7,8,9 };
	print(lst);
}


/*
run:

1 2 3 4
7 8 9

*/

 



answered Jan 23, 2018 by avibootz
edited Jan 24, 2018 by avibootz
...