
Sorting parallel vectors
I have some parallel vectors that need to be sorted. With my knowledge I set them up to be sorted such as this:
Code:
void sortInventory(vector<int>& itemID, vector<string>& itemName, vector<int>& pOrdered, vector<double>& manufPrice, vector<double>& sellingPrice)
{
int i, j;
int min;
for (i = 0; i < itemID.size()1; i++)
{
min = i;
for (j = i + 1; j < noOfRows; j++)
if (itemName[j] < itemName[min])
min = j;
itemName[i].swap(itemName[min]);
itemID[i].swap(itemID[min]);
pOrdered[i].swap(pOrdered[min]);
manufPrice[i].swap(manufPrice[min]);
sellingPrice[i].swap(sellingPrice[min]);
}
}
Well, since I've only covered arrays, I setup as above using swap. As you guys know there is no swap in vectors. Can anybody give me a hint as to what to look for? I know there is a sort function but I need to sort the vectors in parallel.

That looks fine to me, you just need to use the proper swap. (That code wouldn't work for vectors or arrays.)
The syntax for swap is: Code:
swap(itemName[i], itemName[min]);

Thank you Daved, again my textbook is wrong. It shows to swap you would do array[i].swap(array[j]).

That might work for a vector that holds certain class types that have a swap function. For example, it would work for vector<string> because string has a swap function.

Surely, this should be kept in ONE vector holding a struct like this:
Code:
struct InventoryItem
{
int itemID;
string itemName;
int pOrdered;
double manufPrice;
double sellingPrice;
};
vector<InventoryItem> inventory;
Now you can even use the sort function for your vector [assuming you make a compare member for your class]. Or, if you wan to sort it yourself, you can just swap one block with the other.

Mats