@RamilH/iterator test
C++

No description

fork
loading
repl talk
(games)
Files
  • main.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <list>
#include <string>

template<class T>class Foo;
template<class T>std::ostream& operator <<(std::ostream& sout, const Foo<T>& f);

template<class T>
class Foo {
public:
	Foo();
	Foo(T a, T b);
	friend std::ostream& operator << <>(std::ostream& sout, const Foo<T>& f);
private:
	T left;
	T right;
};

int main()
{
	std::list<double> d;
	d.push_back(3.14);
	d.push_back(1.21);
	d.push_back(14);

	for (std::list<double>::iterator it = d.begin(); it != d.end(); ++it) {
		std::cout << *it << "\n";
	}
	std::cout << std::endl;

	std::list<Foo<std::string>> f;
	f.push_back(Foo<std::string>("Hello", "World"));
	f.push_back(Foo<std::string>("Hi diddly-ho", "Neighborino"));
	f.push_back(Foo<std::string>("Check", "Check"));

	// The only changes here are the use of auto and the actual list I iterate through
	for (auto it = f.begin(); it != f.end(); ++it) {
		std::cout << *it << "\n";
	}
	std::cout << std::endl;

	return 0;
}

template<class T>
Foo<T>::Foo()
	: left(T())
	, right(T())
{ }

template<class T>
Foo<T>::Foo(T a, T b)
	: left(a)
	, right(b)
{ }

template<class T>
std::ostream& operator <<(std::ostream& sout, const Foo<T>& f)
{
	sout << "(" << f.left << ", " << f.right << ")";

	return sout;
}
gcc version 4.6.3