Search This Blog

Friday, September 7, 2012

Loop for printing values of container

In the past my printing methods looks like that:

for (iterator it = begin(container), endIt = end(container); it != endIt; ++it) {
    if (it != begin(container)
        printDelimeter();
    printValue(it);
}

So, it's checking for begin to write delimeter looks ... uneffective. Reading stackoverflow.com i've notice that people use this method very often. Looking gtest code i've seen the more effective implementation:

if (container().empty())   
    exitHere();
iterator it = begin(container);
printValue(it);
for (iterator endIt = end(container); it != endIt; ++it) {
    printDelimeter();
    printValue(it);


It's so simple and easy.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.