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

  • SEARCH
  • Home
  • 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 7014373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:30:46+00:00 2026-05-27T22:30:46+00:00

I just deleted an earlier post with a similar question because my example was

  • 0

I just deleted an earlier post with a similar question because my example was not very clear, So I am trying again.

I have created a simple class called SportsSchedules.cpp The class stores 4 items; sportType, TeamName, city and number of wins. I have created a “sports vector” of SportsSchedules objects. I want to run the sportsVector through a loop and for each sport type I want to create a new vector. Each created sub vector should contain only the unique sportType.

Ideally, this sportsVector would run in a loop and would pop each object into its repsective subVector until it was empty(I guess)

Here is the code from my main:

#include <iostream>
#include "SportsSchedules.h"
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <functional>

bool sportType( std::string type);

int main (int argc, const char * argv[])
{

    SportsSchedules *theSport;
    theSport = new SportsSchedules("Football", "Chicago", "Bears", 7);
    std::vector<SportsSchedules*> *sportsVector = new std::vector<SportsSchedules*>();
    sportsVector->push_back(theSport);
    theSport = NULL;
    theSport = new SportsSchedules("Football", "Chicago", "Bears", 7);
    sportsVector->push_back(theSport);
    theSport = NULL;
    theSport = new SportsSchedules("Baseball", "Boston", "RedSox", 62);
    sportsVector->push_back(theSport);
    theSport = NULL;
    theSport = new SportsSchedules("Football", "GreenBay", "Packers", 15);
    sportsVector->push_back(theSport);
    theSport = NULL;
    theSport = new SportsSchedules("Basketball", "Portland", "Trailblazers", 60);
    sportsVector->push_back(theSport);
    theSport = NULL;
    theSport = new SportsSchedules("Football", "Seattle", "Seahawks", 7);
    sportsVector->push_back(theSport);
    theSport = NULL;
    theSport = new SportsSchedules("Baseball", "Oakland", "A's", 67);
    sportsVector->push_back(theSport);

    std::cout<<"Test the SportsSchedules Vector "<<std::endl;

    std::vector<SportsSchedules*>::iterator itr;
    for(itr = sportsVector->begin(); itr != sportsVector->end(); ++itr ){
        std::cout<<(*itr)->getSportType()<<"  "<<(*itr)->getCity()<<"  "<<(*itr)->getTeamName()<<"  "
        <<(*itr)->getNumWins()<<std::endl;

    }
    return 0;

}


bool trackType( std::string type){

    SportsSchedules * sptPtr;

    if(sptPtr->getSportType()== type)
        return true;
    else
        return false;

}

The bool function was from an earlier attempt to try remove_copy_if. I kept getting a compiler error about no int pointer or function pointer. Not sure if that what I need as it creates a blue print of all the vector indexes. I want something that would push – pop if possible Someone also suggested using map or multi map but I didn’t quite understand it

Thanks

  • 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-05-27T22:30:47+00:00Added an answer on May 27, 2026 at 10:30 pm

    Someone suggested you a map because they are associative containers. Ie. instead of looking for a certain value using a positional index (0, 1, 2, …) you look up using an arbitrary value, which can be a string, for example.

    So, how can it be useful for you? See this example:

    #include <map>
    #include <string>
    #include <vector>
    #include <iostream>
    
    int main() {
            std::map< std::string, std::vector<SportsSchedules*> > uniques;
    
            // Initialization code here...
    
            std::vector<SportsSchedules*>::iterator itr;
            for(itr = sportsVector->begin(); itr != sportsVector->end(); ++itr ) {
                uniques[(*itr)->getSportType()].push_back((*itr));
            }
    
            return 0;
    }       
    

    uniques[(*itr)->getSportType()] retrieves from uniques a std::vector<SportsSchedules*> indexed by the value of (*itr)->getSportType(). If the key doesn’t exist in the map (first time you see the sport in the sportsVector), it will create a new one before doing it – otherwise, you get the previously created vector.

    To retrieve the info once it’s there, you can either look it up by name:

    std::vector<SportsSchedules*> vec = uniques["Football"];
    

    or iterating over it to get the (key, value) pairs. Have a look to map’s API for more info.

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

Sidebar

Related Questions

Sorry I missed and deleted earlier question on accident again. I have a situation,
I just deleted my Debug.keystore because an application displayed an error on the project
Just when I was about to post this question, I came up with an
I just deleted two days of work because I though I had a backup,
I just deleted my question to reformulate it a better way, this question didn't
I just want to see what files were modded/added/deleted between 2 arbitrary revisions. How
I've been trying to get a delete to cascade and it just doesn't seem
I discovered earlier tonight that files and folders I have removed from my C#
I am a git noob and git just deleted a bunch of important files.
How do I delete the earlier array value if similar values exist? Here's the

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.