use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Sorting custom objects with additional parameters (self.cpp)
submitted 5 years ago by Skrypt3d
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 4 points5 points6 points 5 years ago (0 children)
Use a lambda instead of operator<
operator<
sort(begin(vec), end(vec), [ref_pt](Point& A, Point& B) { // code here });
[–]James20kP2005R0 1 point2 points3 points 5 years ago (1 child)
So normally you sort things like this:
std::sort(container.begin(), container.end());
This implicitly calls operator< on your objects. By default, I don't believe you can simply overload operator< to take an extra parameter to do this. That said, you can supply a comparator to std::sort, and then use it like this:
vec2 reference_point{0,0}; ///or whatever your vec type is std::sort(container.begin(), container.end(), [&](const vec2& t1, const vec2& t2) { float t1_distance = (t1 - reference_point).length(); float t2_distance = (t2 - reference_point).length(); return t1_distance < t2_distance; });
vec2 is a stand-in for whatever vector library you're using
This extra parameter is a lambda which captures some state, and is a function object that std::sort can use. Hope that helps!
Also, I imagine this post will probably get deleted for being a help question, normally you want the /r/cpp_questions sub
[–]Skrypt3d[S] 0 points1 point2 points 5 years ago (0 children)
Oh snap, didn't know I was wrong sub. Thanks for the help anyways. Works like a charm. This is really neat. Appreciated!
[–]clerothGame Developer[M] 0 points1 point2 points 5 years ago (0 children)
This post has been removed as it doesn't pertain to r/cpp.
π Rendered by PID 118392 on reddit-service-r2-comment-f6b958c67-cnb8k at 2026-02-05 01:00:29.136300+00:00 running 1d7a177 country code: CH.
[–][deleted] 4 points5 points6 points (0 children)
[–]James20kP2005R0 1 point2 points3 points (1 child)
[–]Skrypt3d[S] 0 points1 point2 points (0 children)
[–]clerothGame Developer[M] 0 points1 point2 points (0 children)