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 ;)