Sorting nested vectors

I am solving the problem. "List all triangles with a perimeter of 15, with no repeating triangles(get rid of all permutations, basically). I have successfully stored all the triangles in a vector and using nested vectors, each has a vector of the sides. My problem now is sorting both the vectors inside the triangle vector that contains the sides, and then afterward I need to sort the vector of triangles so that if I had:

[4, 5, 6]
[3 6, 6]
[4, 4, 4]
[3, 5, 7]

if would sort by the first column, then the second, then the third so that it would turn into:
[3, 5, 7]
[3, 6, 6]
[4, 4, 4]
[4, 5, 6]

Any idea on how to do this? Here is my code so far.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <vector>
using namespace std;

int main() {
	vector<vector<int> > triangles; //vector of triangles containing vectors of sides

	//generate all triangles that sum up to 15. includes repeated triangles, will get rid of permutations later. 
	for(int i=13; i>=1; --i) {
		vector<int>sides;
		for(int j = 15-1-i; j >= 1; --j){
			sides.push_back(i);
			sides.push_back(j);
			sides.push_back(15 - i - j);
			triangles.push_back(sides);
			sides.clear();
		}
	}
	
	
	//print all triangles
	for(int i=0; i < triangles.size(); ++i) {
		for(int j=0; j < triangles[i].size(); ++j) {
			cout << setw(3) << triangles[i][j] << ' ';
		}
		cout << endl;
	}
	cout << endl << endl << endl << endl << endl;

}

Last edited on
Hello google. "C++ sorting a vector"

First result:
http://www.cplusplus.com/reference/algorithm/sort/

[Edit: By the way, when you present code that won't compile because it is erroneous, please state that that is the case and present the error you receive when you try to compile it.]
Last edited on
I googled extensively, and yes I came upon that, but I wasn't able to come up with a solution because I didn't quite understand it. I can sort a vector; I am having trouble with sorting the nested vector.

I intended to remove the erroneous line, but I left it there by mistake. I fixed it.
Last edited on
OK, so I feel very, very dumb right now. I have been using the sort function wrong, and didn't notice for some reason.
Topic archived. No new replies allowed.