C++ linked-list traversal
by EdGr from LinuxQuestions.org on (#6J68W)
I am aware of four methods for traversing a doubly-linked list in C++.
Code:// plain c++
for (elem = list.next;
elem != &list;
elem = elem->next) {
}
// macro
FOREACH (elem, list) {
}
// STL
for (elem = list.begin ();
elem != list.end ();
++elem) {
}
// c++11
for (auto& elem : list) {
}I use the "plain c++" method because I have always used that. Consistency is my most important consideration. I am not feeling a need to change.
Which method do you use and why?
Ed
Code:// plain c++
for (elem = list.next;
elem != &list;
elem = elem->next) {
}
// macro
FOREACH (elem, list) {
}
// STL
for (elem = list.begin ();
elem != list.end ();
++elem) {
}
// c++11
for (auto& elem : list) {
}I use the "plain c++" method because I have always used that. Consistency is my most important consideration. I am not feeling a need to change.
Which method do you use and why?
Ed