Skip to content

Instantly share code, notes, and snippets.

@apfohl
Created June 26, 2015 20:49
Show Gist options
  • Save apfohl/8946b81e41b31b4ebb9f to your computer and use it in GitHub Desktop.
Save apfohl/8946b81e41b31b4ebb9f to your computer and use it in GitHub Desktop.
Sort array in C++
/*
* Save as "sort.c".
* Compile with "CXXFLAGS=-std=c++11 make sort".
*/
#include <iostream>
#include <algorithm>
struct Distance {
int index;
double distance;
};
int main()
{
Distance distances[5];
distances[0].index = 0;
distances[0].distance = 1.2;
distances[1].index = 1;
distances[1].distance = 0.3;
distances[2].index = 2;
distances[2].distance = 4.3;
distances[3].index = 3;
distances[3].distance = 2.7;
distances[4].index = 4;
distances[4].distance = 6.8;
std::sort(distances, distances + 5, [](Distance const &a, Distance const &b) { return a.distance < b.distance; });
for (int i = 0; i < 5; i++) {
std::cout << distances[i].distance << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment