Multi dimensional Vector/Array

okay so this method reads in the integer values one, two, three multiple times with different values. (think of it as passing in different permutations!)
Below is my attempt to print the lowest value...Im getting a little confused with multi dimensional vectors..i want to compare each integer one to all other integer ones and if they match compare integer two. Could use some help finishing..I believe I am close

1
2
3
4
5
6
7
8
9
10
  vector<int[3]> hold;
	int addOnToVector[3] = { one, two, three };
	hold.push_back(addOnToVector);
	for (int i = 0; i < hold.size(); i++) {
		if (hold[0][0] >= hold[i][0]) {
			if (hold[0][0] >= hold[i][i]) {
				hold[0][0] = hold[i][i];
			}
		}
	}
Last edited on
some help? a Hint, reference..something please
I won't speak for others, but I have no idea what you're trying to do based on your description and your code. The code you provided won't even compile. You say that the code is your attempt to print the lowest value, but I see no cout's or printf's or anything of the sort. I'm confused as to what you want your code to accomplish.

Here are some thoughts though.

Line 2: wont compile - do you mean int addOnToVector[3] = { 1, 2, 3};
Line 6: hold[i][i]is going to crash with index out of bounds if you push_back onto hold more than 3 times.
Hi,

Can you use the std::min algorithm?

http://www.cplusplus.com/reference/algorithm/min/
http://en.cppreference.com/w/cpp/algorithm/min


Also, maybe a detailed explanation of the overall problem might help. There might be some other ideas about the design which could be different to yours :+)
this printSolution method is being called within a permutation; so as each permutation is being created it is read into this printSolution method. What I was trying to do and failed was read each permutation into an array[3] and push that array into a vector. then go through my vector and print only one of the permutations(the lowest one)
for ex of permutations:
7 6 13
1 4 5
2 1 3
1 2 3
9 1 10
etc.
In this case I only want{ 1 2 3} to be printed, Help please
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void printSolution(int one, int two, int three, string operand) {
	//pre: Reads in 3 integer values that each represent a string the user entered
	//post: Prints each number that corresponds to each string and inputs them in equation form; proving the equation is true
	vector<int>solutionHolder;
	//solutionHolder.push_back(one);
	vector<int[3]> hold;
	int addOnToVector[3] = { one, two, three };
	//hold.push_back(addOnToVector);
	/*for (int i = 0; i < hold.size(); i++) {
		if (hold[0][0] >= hold[i][0]) {
			if (hold[0][0] >= hold[i][i]) {
				hold[0][0] = hold[i][i];
			}
		}
	}*/
		cout << "Solution " << ": ";
		cout << one << " " << operand << " " << two << " = " << three;
		cout << endl;
	
}
Hi,

I found this:
http://en.cppreference.com/w/cpp/algorithm/min_element


You will either need to overload operator< or provide a comparison function.

if you use std::tuple rather than an array, there is already operator< The example shows sorting, not sure if that would be less efficient or the same as std::min_element
http://en.cppreference.com/w/cpp/utility/tuple/operator_cmp


It's late (early?) 06:00AM at my end, otherwise I would have written some example code for you. You could well find more examples on Google :+)

Regards :+)

Edit: There are other std containers that implement operator< , std::array is one.
Last edited on
TheIdeasMan wrote:
The example shows sorting, not sure if that would be less efficient or the same as std::min_element


Don't know what I was thinking: of course minimum is quicker than sort :+)
Topic archived. No new replies allowed.