Department of Computer Science | Institute of Theoretical Computer Science | CADMO

Theory of Combinatorial Algorithms

Prof. Emo Welzl and Prof. Bernd Gärtner

// Program: fill5.cpp // define and use a function template to fill a range #include #include // PRE: [begin, end) is a valid range // POST: *p is set to value, for p in [begin, end) template void fill (T* begin, T* end, const T value) { for (T* ptr = begin; ptr != end; ++ptr) *ptr = value; } int main() { int a[5]; fill (a, a+5, 1); for (int i=0; i<5; ++i) std::cout << a[i] << " "; // 1 1 1 1 1 std::cout << "\n"; std::string b[5]; fill (b, b+5, "bla"); for (int i=0; i<5; ++i) std::cout << b[i] << " "; // bla bla bla bla bla std::cout << "\n"; return 0; }