Sorting parallel vectors

This is a discussion on Sorting parallel vectors within the C++ Programming forums, part of the General Programming Boards category; I have some parallel vectors that need to be sorted. With my knowledge I set them up to be sorted ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    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.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    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]);

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Thank you Daved, again my textbook is wrong. It shows to swap you would do array[i].swap(array[j]).

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    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.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  4. How to input a data file to parallel vectors
    By Wink- in forum C++ Programming
    Replies: 13
    Last Post: 10-30-2007, 05:24 PM
  5. parallel sorting
    By akidamo in forum C Programming
    Replies: 7
    Last Post: 04-13-2006, 12:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21