for Syntax

I am looking into a code snippet for the Dancing Links (DLX) algorithm in its pure, general form; not for Sudoku specifically. I found what I'm looking for here: https://github.com/jlaire/dlx-cpp/blob/master/dlx.cpp. At line 176, I came across the code "for (auto& row : rows){". I've never seen this "for" syntax before. My compiler (VC++ 2010) doesn't like and hence it doesn't compile. Can anybody explain what it means? Any work around? Any help is greatly appreciated.

Thanks,

dominicanjb
Last edited on
closed account (Dy7SLyTq)
its the c++ 11 range based for loop. what it does is iterate through each element in an array. i dont use vc++ but ive heard it doesnt work well with c++11. however it doesnt hurt to try and compile with -std=c++11, which is what you need to use c++11 features
Windows programs use / for switches, not -
Anyway, I didn't find that switch here http://msdn.microsoft.com/en-us/library/fwkeyyhe.aspx
i dont use vc++ but ive heard it doesnt work well with c++11.


VS2010 barely handles it at all.

VS2012 is significantly better (and handles range based for loops just fine)... though it still is missing some areas.

VS2013 I'm sure is even better but I haven't tried it yet.

@ dominicanjb: update your version of VS.

however it doesnt hurt to try and compile with -std=c++11,


That's a commandline switch for gcc and will do nothing in VS which uses a totally different compiler.

VS also has C++11 features enabled by default so if it's not working then it's just not supported.
Thanks everybody for the quick responses. From your comments above, if sticking to VS 2010 which appears not to have the range based for loop, the equivalent of "for (auto& row : rows)" would be "for(int row = 0; row < rows.size(); row++)" where rows is a vector int??

Thanks again,

dominicanjb
Last edited on
Close but not quite. 'row' would actually be a reference to the element. There isn't a 100% direct translation. Closest would be this:

1
2
3
4
5
6
7
8
// assuming 'rows' is a vector<int>
for( vector<int>::iterator iter = rows.begin(); iter != rows.end(); ++iter )
{
    int& row = *iter;

    //... for(auto& row : rows) starts here

}


EDIT: or if you're not comfortable with iterators... I suppose this would work for vectors:

1
2
3
4
5
6
for( size_t i = 0; i < rows.size(); ++i )
{
    int& row = rows[i];

   // ... for starts here
}
Last edited on
closed account (Dy7SLyTq)
Windows programs use / for switches

thats only if its deigned to. i use mingw which uses -'s which brings me to

That's a commandline switch for gcc and will do nothing in VS which uses a totally different compiler

sorry my mistake. i only use gcc (or gcc like compilers)so i forgot that other compilers use different switches
Topic archived. No new replies allowed.