got stuck with vector(s) processing

Hello Everyone!,

I learn C++ by myself, therefore I have some blank pages in some areas, eg. some operations on containers, names of different operations etc.
Therefore please help me out with this problem.
(I would also appreciate sending me to good resources that would help me to chose right container and methods to process them. Real implementation examples would be big help, as it's easier to get it for me this way.)

This is my first 'real' program. I made it in PHP but now rewriting to C++ as I learn it (and I can say it's more challenging).

In short, file is read and 3 different vectors are created containing corresponding elements (person's name, item's name and item's amount).
So for example (I know it's not way to assign values, its just to illustrate contents):
1
2
3
vector<string> vectorOfNames = {"Adam", "Eva", "Adam", "Adam", "Bruce"};
vector<string> vectorOfItems = {"Apple", "Apple", "Orange", "Pear", "Melon"};
vector<int> vectorOfAmount = {1, 9, 2, 4, 1};


Now, I want to sort (by person and item) and count (by amount) those vectors, eg. to print something like:
All persons:
Adam
Eva
Bruce

All items:
Apple - 10
Orange - 2
Pear - 4
Melon - 1

Adam have:
Apple - 1
Orange - 2
Pear - 4

Eva have:
Apple - 9

Bruce have:
Melon - 1


In PHP i used array_keys(array_flip()) to get unique names and items.
In C++ i found something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
vector< string >::iterator r , w ;

set< string > tmpset ;

for( r = vectorOfNames.begin() , w = vectorOfNames.begin() ; r != vectorOfNames.end() ; ++r )
{
    if( tmpset.insert( *r ).second )
    {
        *w++ = *r ;
    }
}

vectorOfNames.erase( w , vectorOfNames.end() );

It works well but problem is that it modifies original vectorOfNames.
Should I copy this vector to new one before applaying this or is there another approach?


As for the rest of desired processing in PHP I used foreach and if statements.
I was trying different approaches for C++ but nothing works. I'm completely lost....
Also I know that there are some functions in Boost Library, but for time being I don't want to go there, prefer to learn basics first.


On another hand, maybe i should use other container, like map or something else, to make this processing easier?

So if you still know what I mean and you did not fall asleep, please push me in right direction ;)
Last edited on
You just want to sort?

//Sort the names
std::sort(vectorOfNames.begin(),vectorOfNames.end(),less<std::string>);

With your example, the 3 vectors, how do you know what items belong to whom and at what amount? (Maybe you should use a struct/class for this, and use a container of that type) *Edit* I looked at your data example and understand this before I just quickly looked over your data without truly reading it.

e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
struct Item{
    string ItemName;
    int Amount;
};

struct Customer{
    string Name;
    vector<Item> Items;
};

main(){
//...
vector<Customer> customers;


Then you can sort (using the method from above), on the different criteria.
Last edited on
No, I need much more than just sort. I need to create new containers (unless there is other way).

As i said in first post, "file is read and 3 different vectors are created containing corresponding elements (person's name, item's name and item's amount)".
That mean that vectorOfNames.at(0) is connected to vectorOfitems.at(0) and vectorOfAmount.at(0) etc.


In PHP, having those 3 arrays
$names[];
$item[];
$amount[]:


I do
1
2
3
4
5
6
foreach ($item as $id => $itemName)
{
	$items[$itemName] += $amount[$id]; // adding amount for each item to $items array
	$persons[$names[$id]][$itemName] += $amount[$id]; // adding every item with amount to each persons
	$loots[$itemName][$names[$id]] += $amount[$id];
}

to get what I need.... sorry, don't know how to explain it better...
If you cant modify the original vector then you can create a copy of the container(s) in the sorted order(s).
Why you are so stubborn with sorting?
Unless I do not understand something, sorting will do nothing to achieve what i need.
Why you are so stubborn with sorting?

I don't understand, you've posted a question, I'm trying to help you to a solution.

Now, I want to sort (by person and item) and count (by amount) those vectors, eg. to print something like:

This is why sorting has come up.

It works well but problem is that it modifies original vectorOfNames.
Should I copy this vector to new one before applaying this or is there another approach?


Then don't use the original vector of names? Make a copy.

My apologies for using that expression, to avoid confusion I should had said:
Now, I want to present data (by person and item) and count (by amount) those vectors, eg. to print something like:


Sorting as i understand it is rather something different than i need, like sorting elements for binary search.

Since this is more of a learning exercise, let me give you my two cents. I would scratch the 3 vectors entirely. Start with the structure of your data. Which currently consists of 3 vectors. It seems like a headache to work with the data in such a fashion. May I present to you...

Using a map or set.

Set implementation:
set<Person>

Where person is:
1
2
3
4
struct Person{
   string Name;
   map<string, int> Items;
};


Now each person contains it's own data, there is no shared container that you have to parse through and count items etc. If you want to sort the items, you can do that internally for each Person, if you want to present the data in some fashion, this can be done at the Person level.

Map implementation, similar to Set:
map<string, Items>

Where Items is:
typedef map<string, int> Items

Same kind of idea, it will be ordered by persons name as is the set, except now presentation will be implemented outside of "Person" in that Items is not an object but a typedef for a container of type map.

How will you go from 3 vectors to this? Simple, create your structured data. Then when you read your file instead of pushing onto the container just insert into your structures. Set/Map can only have 1 key so you will not have repeat Names of people or Items for a particular person.

If the file is structured in such the way that you have to keep track of the order of the appearance of names/items/amounts in the file as you read it then you have to track that and insert appropriately, but once inserted you don't have to agonize over the implications of having 3 vectors.
Thanks a lot. I will try to implement this(new to me) approach.
Topic archived. No new replies allowed.