Q: Modifying value of map pair Can somebody explain why whenever I try to increment the pair nothing happens? I tried debugging it and although it goes to the incrementing line nothing happens.
EDIT: Here's the whole function
void VoteCollector::resultsBasedOnAge(std::vector<Voter>& voters)
{
    std::map<int,std::pair<int,int>> ageVoters;
    std::map<int,std::pair<int,int>>::iterator hasAge = ageVoters.begin();


    for(unsigned i = 0; i < voters.size(); i++)
    {
        if(ageVoters.find( voters.at(i).getAge() ) != ageVoters.end() )
        {
            if(voters.at(i).getVote() == "leave")
            {
                hasAge->second.first++;
            }
            else if(voters.at(i).getVote() == "stay")
            {
                hasAge->second.second++;
            }
            hasAge++;
        }
        else
        {
            if(voters.at(i).getVote() == "leave")
            {
                ageVoters.insert(std::make_pair(voters.at(i).getAge(),std::make_pair(1,0)));
            }
            else if(voters.at(i).getVote() == "stay")
            {
                ageVoters.insert(std::make_pair(voters.at(i).getAge(),std::make_pair(0,1)));
            }
            hasAge++;
        }
    }

    for(std::map<int,std::pair<int,int>>::iterator it = ageVoters.begin(); it != ageVoters.end(); it++)
    {
        std::cout << it->first << " years -- " << it->second.first << " leave.\t" << it->second.second << " stay\n";
    }
}


A: From what I see, your code does not work because your hasAge is pointing to, I don't know, somewhere you probably don't mean to. You want to assign to it the result of std::map::find.
Assuming you are using C++11, the code can also be simplified:
void VoteCollector::resultsBasedOnAge(const std::vector<Voter>& voters)
{
    std::map<int, std::pair<int, int>> ageVoters;

    for (const auto& v: voters)
    {
        int age = v.getAge();
        const auto& vote = v.getVote();

        auto it = ageVoters.find(age);
        if (it != ageVoters.cend())
        {
            if (vote == "leave")
            {
                ++it->second.first;
            }
            else if (vote == "stay")
            {
                ++it->second.second;
            }
        }
        else
        {
            if (vote == "leave")
            {
                ageVoters.insert(std::make_pair(age, std::make_pair(1, 0)));
            }
            else if (vote == "stay")
            {
                ageVoters.insert(std::make_pair(age, std::make_pair(0, 1)));
            }
        }
    }

    for (const auto& v: voters)
    {
        std::cout << v.first << " years -- "
                  << v.second.first << " leave.\t"
                  << v.second.second << " stay\n";
    }
}

