Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8626077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:56:15+00:00 2026-06-12T07:56:15+00:00

I learn C++ by myself, therefore I have some blank pages in some areas,

  • 0

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

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 represent it (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:

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 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T07:56:16+00:00Added an answer on June 12, 2026 at 7:56 am

    The first step, i.e., getting all unique names in a std::vector<std::string> I would probably do like this:

    std::vector<std::string> tmp(original);
    std::sort(tmp.begin(), tmp.end());
    std::unique_copy(tmp.begin(), tmp.end(),
                     std::ostream_iterator<std::string>(std::cout, "\n"));
    

    An alternative solution could use an auxiliary std::set<std::string> but wouldn’t print the names in sorted order:

    std::set<std::string> mark;
    std::copy_if(original.begin(), original.end(),
                 [&](std::string const& value) { return mark.insert(value); });
    

    (making use of C++ 2011 features)

    The other operations don’t directly map easily to the way C++ algorithms work. To deal with these I would probably use a different data layout, e.g., storing the data in a std::vector<std::tuple<std::string, std::string, int> > rather than three separate vectors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on some C# application to help myself learn C# a bit. I've
I am trying to learn assembly my self, and I have been reading different
I'm trying to build a Chinese flashcards program in Java to help myself learn
In my latest quest to learn some assembly language I'm finding myself constantly going
I'm trying to learn myself some C++ from scratch at the moment. I'm well-versed
I am creating a number guessing game to help myself learn how to use
Partly to learn, partly to help myself I'm trying to write an app with
I have taken it upon myself to learn python. As it is my second
I'm beginning to learn about to how to use DI containers. I'm finding myself
To help myself learn Python, I'm writing a simple issue tracker using Django. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.