1.11M Members

Sorting a vector

 
0
 

Hi guys,

I'm banging my head against the wall with this one.
I want to sort a <string> vector which contains the following:

"Pen 0.99"
"Pen 0.99"
"Paper 1.50"
"Pad 1.99"
"Paper 1.50"

Into a vector which would sort it as follows:

"Pen 1.98"
"Paper 3.00"
"Pad 1.99"

I know how to add up all the strings/converting to a number etc but I'm having trouble eliminating the 'duplicates' and tieing in the digit to the relative word.

 
0
 

Forgive me if im wrong but wouldn't this be a hell of a lot easier in an array?

string itemsArray[x][y]

itemsArray["pen"][1.99]

or something to this effect?
without writing some code to parse your alphanumeric string you're going to struggle how your doing it right now i think (imo)

 
0
 

hi there, cheers for the reply.

I've never used a two-dimensional container before so never considered it but will give it a shot.

 
0
 

Forgive me if im wrong but wouldn't this be a hell of a lot easier in an array?

string itemsArray[x][y]

itemsArray["pen"][1.99]

or something to this effect?
without writing some code to parse your alphanumeric string you're going to struggle how your doing it right now i think (imo)

Wrong. You can't have string and a float, in a regular array.

@OP, how do you wan't it to be sorted, aside from deleting
the duplicates, lexicographically?

 
0
 

I'm not quite sure what lexicographically means even after Googling it so please forgive my understanding!

I basically want to add up all the digits and assign it to it's relative word. Sounds simpler than it is because I'm still stuck :-(

 
0
 

Forgive me if im wrong but wouldn't this be a hell of a lot easier in an array?

string itemsArray[x][y]

itemsArray["pen"][1.99]

or something to this effect?
without writing some code to parse your alphanumeric string you're going to struggle how your doing it right now i think (imo)

That's an associative array. They exist in some languages (i.e. PHP), but they don't exist in C++. A "map" would be a C++ alternative to an associative array.

http://www.cplusplus.com/reference/stl/map/

It looks like you're trying to emulate a database-type query like "GROUP". You have two fields per record (product and price) and you are grouping by product to get the sum of all the prices. Easy in SQL, not so easy in C++.

I'd say that you need to create a class or a struct organize something like this:

class merchandise
{
    string product;
    double price;
};

Or

class merchandise
{
    string product;
    vector <double> price;
};

You might have some larger container like this:

vector <merchandise> allProducts;

It all depends on how you want to store everything.

 
0
 

I'm not quite sure what lexicographically means even after Googling it so please forgive my understanding!

I basically want to add up all the digits and assign it to it's relative word. Sounds simpler than it is because I'm still stuck :-(

you have :

"Pen 1.98"
"Paper 3.00"
"Pad 1.99"

Do you wan't it to sort by the value it has, ex Pen has a value
of 1.98 and is the lowest so its the minimum?

 
0
 

hi there and thanks for the reply.

If i did opt to go for the Vector of a class then created loads of objects, how would I go about tieing in all these objects together? for example:

Objects of class 'merchandise'
"BallPointPen 0.99"
"AnotherPen 1.99"
"BallPointPen 0.99"

to get
"BallPointPen 1.98"
"AnotherPen 1.99"

 
0
 

you have :

"Pen 1.98"
"Paper 3.00"
"Pad 1.99"

Do you wan't it to sort by the value it has, ex Pen has a value
of 1.98 and is the lowest so its the minimum?

Oh no, there does not have to be any sorting. Just adding the numbers up together before pairing it with it's relative name.

 
1
 

hi there and thanks for the reply.

If i did opt to go for the Vector of a class then created loads of objects, how would I go about tieing in all these objects together? for example:

Objects of class 'merchandise'
"BallPointPen 0.99"
"AnotherPen 1.99"
"BallPointPen 0.99"

to get
"BallPointPen 1.98"
"AnotherPen 1.99"

My advice is to create both classes that I suggested:

class Merchandise
{
    string product;
    double price;
};

class GroupedMerchandise
{
    string product;
    vector <double> price;
};

Or if you don't care about the individual prices, just the total, do this instead:

class GroupedMerchandise
{
    string product;
    double totalPrice;
};

You go through each string and create a Merchandise object from each string.

Then you go through all the Merchandise objects one by one. If you already have a a GroupedMerchandise object with that product name, don't create another one. Instead either add another price to that object's vector or add the price to the total member (depending on which GroupedMerchandise class you decide to implement). Either way, you need some way to search through a vector of GroupedMerchandise objects and see if one already exists, so write this function:

bool contains (vector <GroupedMerchandise>, string productName)

Oh no, there does not have to be any sorting. Just adding the numbers up together before pairing it with it's relative name.

It's a terminology issue. In post 1, you mention the word "sorting", which implies that there is an order. You meant "grouping", as in all records with a certain characteristic in common are grouped together as one combined record, so five records in effect turn into three.

 
0
 

It's a terminology issue. In post 1, you mention the word "sorting", which implies that there is an order. You meant "grouping", as in all records with a certain characteristic in common are grouped together as one combined record, so five records in effect turn into three.

Apologies for being unclear. Thank you for your help, I will get working on this now.

 
0
 

Thinking about my earlier mention of "map", while I don't think that "map" would work for you here, I'm wondering whether multimap or perhaps something else from STL might be useful to you here. I've never done much with STL so I'm not sure, but it might fit well here.

http://www.cplusplus.com/reference/stl/
http://www.cplusplus.com/reference/stl/multimap/

From the link above:

Maps are a kind of associative containers that stores elements formed by the combination of a key value and a mapped value, much like map containers, but allowing different elements to have the same key value.

Keys would be "Pen", "Paper", "Tape", etc. mapped values could be 0.99, 1.50, 1.67, etc. "Pen" could map to more than one value. Not sure whether this will work here or not (i.e. can you have two "Pen" mappings, each mapping to 1.00? I don't know the answer to this), but it could be worth the research.

 
0
 

Thinking about my earlier mention of "map", while I don't think that "map" would work for you here, I'm wondering whether multimap or perhaps something else from STL might be useful to you here. I've never done much with STL so I'm not sure, but it might fit well here.

http://www.cplusplus.com/reference/stl/
http://www.cplusplus.com/reference/stl/multimap/

From the link above:

Keys would be "Pen", "Paper", "Tape", etc. mapped values could be 0.99, 1.50, 1.67, etc. "Pen" could map to more than one value. Not sure whether this will work here or not (i.e. can you have two "Pen" mappings, each mapping to 1.00? I don't know the answer to this), but it could be worth the research.

Thanks for the links, although after hours and hours I've gotten somewere (kind of).

I've implemented a 'Grouping' class with string productName and a vector<double> of prices. I've also created a vector of type 'Grouping' in order to hold all these objects.

I've then checked to see whether the product of 'Merchandise' already exists in the grouping vector and if so, then do not create a new object, instead, add its price to the double<vector> of prices.

This works fine in this scenario:
1. User adds merchandise to the system
2. Iterate through and all goes smoothly
However, if I add the following step, everything gets messed up and the prices are assigned to the wrong thing, e.g. 0.99 for a pad instead of a pen.
3. User adds more merchandise to the system

I haven't tried yet (as it's late now) but I think a solution would be to clear the vectors entirely upon each iteration and re-populating it each time however, I know this will cause performance issues and is not very elegant at all. :(

 
0
 

Appologies for my last response, i can see why you can't mix the two types in an array (but frustrating i have to say)

Does this have to be completely written in c++?

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article