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 7897165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:05:33+00:00 2026-06-03T08:05:33+00:00

I try to make this implementation of a multimap in C++, but I run

  • 0

I try to make this implementation of a multimap in C++, but I run into a seg fault when trying to access the vector “values”, even though values.size() returns the correct answer.

I know the seg fault happens on line 22, but I don’t know why. Would appreciate any help.

#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
template<class DT1,class DT2>
class Pair
{
public:
  Pair(DT1 key_in, DT2 value_in) 
  {
     key = key_in;
     values.push_back(value_in);
  }
  DT1 key;
  vector<DT2> values;
  int insert_value(DT2 item)
  {
     bool value_found = false;
     cout << "values size "<<values.size() << endl;

     cout << "test " << values[0] << endl;

     for (unsigned int i = 0; i < values.size(); i++)
     {
         cout <<"i " << i << endl;
         if (values[i] == item)     
            value_found = true;
     } 

  if (value_found == false)
  {
      cout<<"not found"<<endl;
      values.push_back(item);
  }

  return 0;
  }
};

template<class T1, class T2>
class MultiMap
{
public:
   MultiMap() {};
   vector<Pair<T1, T2> > pair_container;
   int insert(T1 key_in, T2 value_in)
   {
       bool key_found = false;
       unsigned int i;
       for (i = 0; i < pair_container.size(); i++)
       {
           if (pair_container[i].key == key_in)
       key_found = true;
       }   
       if (key_found == false)
           pair_container.push_back(Pair<T1,T2>(key_in, value_in));
       if (key_found == true)
       {
            pair_container[i].insert_value(value_in); // seg fault
       }
   return 0;
   }

   };

int main()
{
MultiMap<char, string> Map1;
Map1.insert('a', "anchor");
cout << Map1.pair_container[0].values[0] << endl;
Map1.insert('a', "application"); // seg fault
cout << "hello!"<<endl;
Map1.insert('b', "boolean");
return 0;
}
  • 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-03T08:05:34+00:00Added an answer on June 3, 2026 at 8:05 am

    You need to break out of your for loop once you’ve found the correct vector.

    I.e.:

       unsigned int i;
       for (i = 0; i < pair_container.size(); i++)
       {
           if (pair_container[i].key == key_in)
           {
               key_found = true;
               break; // If you don't break out, i will always be one larger than size().
           }
       }   
    

    If you don’t break out then key_found will be true but i will be referencing one past the end of the vector and so you’ll get memory corruption problems leading to a segmentation fault.

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

Sidebar

Related Questions

I will try to make this as clear as I can, but if you
I want to make this 1332251639632 to this 1332251639 I try this code, but
Ok, I'm going to try to make this more clear because my last question
I am starting this question to try and make a central point developers can
Packaging operation failed - this message now showing while i try to make .ipa
This is my first question here so try to make my best so you
I try to make an animation with an image in my view but it
I try to make in app purchase on my app. But I have not
I'm trying to use this trie implementation for ocaml: http://www.lri.fr/~filliatr/ftp/ocaml/ds/trie.ml.html This is my implementation
Been a tough week, will try to make this as clear as possible. Appreciate

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.