2

Condition based for loop selection.

if(valid)
for (std::multimap<int,int>::reverse_iterator rit=id_count.rbegin(); mcount<10 && rit!=id_count.rend();++rit)
else
for (std::multimap<int,int>::iterator rit=id_match.begin(); mcount<10 && rit!=id_match.end();++rit)

{
    //this is common for both for loop
}

how to achieve this in C++?

Lundin
  • 195,001
  • 40
  • 254
  • 396
deep
  • 35
  • 1
  • 5

5 Answers5

3

You have no choice but putting the common part in a function, very roughly like this:

void somefunction(...)
{
    //this is common for both for loops
}

if (valid)
{
  for (std::multimap<int,int>::reverse_iterator rit=id_count.rbegin(); mcount<10 && rit!=id_count.rend();++rit)
    somefunctiuon(...);
}
else
{
  for (std::multimap<int,int>::iterator rit=id_match.begin(); mcount<10 && rit!=id_match.end();++rit)
    somefunctiuon(...);
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
1

This is arguably most useful as an illustration that it's not worth combining the loop logic, though it does work. Provided here for interest value...

#include <iostream>
#include <map>

int main()
{
    std::multimap<int,int> id_count = { {1,2}, {9, -2}, {1,44}, {2,3}, {3,5}, {7,34} };

    for (int valid = 0; valid < 2; ++valid)
    {
        std::cout << "valid " << valid << '\n';
        int mcount = 0;
        for (std::multimap<int,int>::iterator it = valid ? id_count.rbegin().base()
                                                         : id_count.begin();
             mcount<10 && (valid ? it--!=id_count.begin() : it!=id_count.end());
             (valid ? it : ++it), ++mcount)
        {
            std::cout << "[mcount " << mcount << "] "
                << it->first << ',' << it->second << '\n';
        }
        std::cout << '\n';
    }
}
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
1

You can create a template function:

#include <map>
#include <iostream>

template<typename I> void func(I begin, I end) {
    int mcount = 0;
    for (I it = begin; mcount < 10 && it != end; ++it) {
        ++mcount;
        std::cout << "[mcount " << mcount << "] "
            << it->first << ',' << it->second << '\n';
    }
}

int main() {
    std::multimap<int,int> id_count = { {1,2}, {9, -2}, {1,44}, {2,3}, {3,5}, {7,34} };
    for (int valid = 0; valid < 2; ++valid) {
        std::cout << "valid " << valid << '\n';
        if (valid) {
            func(id_count.rbegin(), id_count.rend());
        } else { 
            func(id_count.begin(), id_count.end());
        }
        std::cout << '\n';
    }
}

But IMHO this solution is a bit complicated, so consider other ways (like placing the loop body in a function).

artyom_ban
  • 121
  • 3
0

You can try "#if valid", like:

#if 0 for(i=1;i<10;++i) #else for(i=2;i<9;++i) #endif { cout << i << endl; }

0

In C++14 you also have an option to use generic lambdas:

auto common_code = [/* Capture state if needed */] ( auto& Iter )
{
    // Your common code
};

if ( valid )
    for ( std::multimap<int, int>::reverse_iterator rit = id_count.rbegin(); mcount < 10 && rit != id_count.rend(); ++rit )
        common_code( rit );
else
    for ( std::multimap<int, int>::iterator rit = id_match.begin(); mcount < 10 && rit != id_match.end(); ++rit )
        common_code( rit );