Trying to understand two dimensional vector and was wondering if anyone could help me out on a sorting code for this so that it numbers 0-9 people, gets x number with each of them and then outputs the person (number) ate x pancakes - But in descending or ascending order.

/* The vector seems right if my output works,
   this doesn't sort it though by person or pancake. */ 

vector< vector<int> > pVec(10,vector<int>(2));
  for(int x=0; x < 10; x++){
    pVec[x][0]= x;       
  }

  sort(pVec[x][1].begin(), pVec[x][1].end());
  for (int x = 0; x < pVec.size(); ++x) 
     cout << "\nPerson "<< pVec[x][0] <<" ate " << pVec[x][1]<<" pancakes.";

This shouldn't compile (and it doesn't for me). x is out of scope on line 9. What is "less than" something else? Is {5,3} less than {3,4}? Or vice-versa? I don't know what the default "less than" is, for one array being "less than" another, but you can't sort unless there is one. Make sure it's what you want. You may have to provide it.

Anyway, here is what I THINK you probably want. If not, you'll need to provide a less than comparison.

#include <vector>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;


int main()
{
/* The vector seems right if my output works,
   this doesn't sort it though by person or pancake. */ 

  srand(time(0));
  
vector< vector<int> > pVec(10,vector<int>(2));
  for(int x=0; x < 10; x++){
    pVec[x][0]= rand() % 25;;
    pVec[x][1] = rand()  % 25;      
  }

  for (int x = 0; x < pVec.size(); ++x) 
     cout << "\nPerson "<< pVec[x][0] <<" ate " << pVec[x][1]<<" pancakes.";

  sort(pVec.begin(), pVec.end());
  cout << "Sorted\n";
  
  for (int x = 0; x < pVec.size(); ++x) 
     cout << "\nPerson "<< pVec[x][0] <<" ate " << pVec[x][1]<<" pancakes.";
     
  cin.get();
  return 0;
}

Sorry no this is not the entire code my question is
sort(pVec.bgein(), pVec.end) doesn't sort ONLY the pVec[x][1] row, I need the numbers in pVec[x][0] to stay the same.

:confused: You're really not making much sense.

It seems you want the "outer" vector sorted based on the contents of the "inner" vectors, and you want the contents of the inner vectors to be stable.

Is there a reason you are using vectors to store a specific "person"'s information? I think you may want to consider using a vector of structs instead of a 2-d vector:

struct Person {
  int myID;
  int amountEaten;
};

const int ATTENDEES = 5;

vector<Person> People(ATTENDEES);

for (int i = 0; i < ATTENDEES; ++i) {
  //prompt user for input
  (People[i]).myID = inputID;
  (People[i]).amountEaten = inputAmount;
}

//perform sort

That is perfect, now how would you sort .amountEaten only?
operator overload comparison for the struct?

Correct, you would have to write a custom less-than operator that returns true based on the result of a comparison of the values of Person::amountEaten.

>> Sorry no this is not the entire code my question is
sort(pVec.bgein(), pVec.end) doesn't sort ONLY the pVec[x][1] row, I need the numbers in pVec[x][0] to stay the same.

If pVec is a 2-D vector of integers, you're dereferencing it twice here, so it seems to me there is no pVec[x][1] row, there is a pVec[x][1] element. Hence only one number. Hence nothing to sort unless I'm missing something. You might want to show the before and after of what you want to do.

person [1][2][3][4][5][6]...
pancake [x][x][x][x][x][x]...

Just to clarify,
I need the pancake to be user input, the person to stay the same, sort only the second row. Your right Vernon, and im sorry, I should of been alot clearer in how I referenced the vector parts.

Thank you Fbody going to try structs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.