Skip to content

Commit 442c24c

Browse files
committed
Create print_list.h
1 parent 2150b3c commit 442c24c

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Util/print_list.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*****************************************************************//**
2+
* \file print_list.h
3+
* \brief Template function to print std::list of any types.
4+
*
5+
* \author Xuhua Huang
6+
* \date October 2021
7+
*********************************************************************/
8+
9+
#ifndef PRINT_LIST_H
10+
#define PRINT_LIST_H
11+
12+
#include <iostream>
13+
#include <list>
14+
15+
#ifndef DEBUG
16+
#define DEBUG(arg_str) std::cout << arg_str << std::endl;
17+
#endif
18+
19+
namespace util {
20+
namespace list {
21+
22+
/* Template to print a std::list with O(n). */
23+
/* For sanity, using constant reference instead of iterators. */
24+
template<typename T>
25+
static void print_list(std::list<T> arg_list)
26+
{
27+
DEBUG("\n[fn]Util::list::print_list");
28+
29+
/* Loop through the vector with iterator. */
30+
typename std::list<T>::iterator iter; // keyword "typename" is required for iterator
31+
for (iter = arg_list.begin(); iter != arg_list.end(); ++iter)
32+
std::cout << *iter << " "; // dereference the iterator to print content
33+
34+
DEBUG("[fn]Finished printing the list.");
35+
return;
36+
}
37+
38+
} // end Util::list
39+
} // end Util
40+
41+
42+
#endif

0 commit comments

Comments
 (0)