Here is some code with the error messages at the end. I can't figure out why I get compiler errors. If I comment out the sort statement, the compile error goes away. I just don't see anything wrong with it: (BTW, I just wanted to get to the lambdas to try it out, so the rest of it is not necessarily the way I'd normally code it up) Oh I'm using free download version of Visual Studio 2013.
Error 4 error C2676: binary '-' : 'std::_List_iterator<std::_List_val<std::_List_simple_types<name>>>' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm 3157 1 Lambda 1
You cant't use std::sort on a std::list because it takes random access Iterators and a std::list only supplies bidirectional Iterators. Use std::list::sort().
Error 4 error C2676: binary '-' : 'std::_List_iterator<std::_List_val<std::_List_simple_types<name>>>' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm 3157 1 Lambda 1
could you elaborate on the code? I feel like I just need 2 args to compare, why do I need to capture any var refs in the enclosing scope?
--------------------------------
When I use the std::List::sort, should it be like this with a lambda?
nameList.sort( [] (const name &a, const name &b)->bool{return a.lastName < b.lastName; });
This compiles but i get a really weird sort - What is going on behind the scenes? Results:
Name list by last name
Xiang, chi, Fu
Roberto, Alonze, Morris
john, t, Smith
bo, c, jackson
Derek, charles, mito
Alice, in, wonderland
> I feel like I just need 2 args to compare, why do I need to capture any var refs in the enclosing scope?
You do not need to capture anything.
> When I use the std::List::sort, should it be like this with a lambda?
> nameList.sort( [] (const name &a, const name &b)->bool{return a.lastName < b.lastName; });
Yes. You can omit the -> bool, it is inferred.
> but i get a really weird sort - What is going on behind the scenes?
There is nothing weird about it; default std::string comparisons are case-sensitive.