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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:34:37+00:00 2026-06-14T11:34:37+00:00

I’m working on a project that includes a handmade Set of sorts using a

  • 0

I’m working on a project that includes a handmade Set of sorts using a template class with two types and vectors (which I may change to pointers but not at the moment).

Adding and outputting values works fine, and I’m able to access the key and values using eachother as well.

However I’m having problems with removing values. This is the code I have for it:

bool Remove(Tkey key)
{
    for (int i = 0; i < size(); i++)
    {
        if (keyPtr[i] == key)
        {
            keyPtr.erase(keyPtr.begin() + (i-1));
            valuePtr.erase(valuePtr.begin() + (i-1));
            return true;
        }
    }
    return false;
}

So that when I want to use it it will look like this:

cout << "Remove Value 6" << endl;
list.Remove(6);

What I expect from vectors is when that calls, the data with the key as 6 would be removed. However I get this as my output:

Key : 0 Value : 0

Key : 1 Value : 3

Key : 2 Value : 6

Key : 3 Value : 9

Key : 4 Value : 12

Key : 5 Value : 15

Key : 6 Value : 18

Key : 7 Value : 21

Key : 8 Value : 24

Key : 9 Value : 27

Find value 5: 15

Remove Value 6

Key : 0 Value : 0

Key : 1 Value : 3

Key : 2 Value : 6

Key : 3 Value : 9

Key : 4 Value : 12

Key : 0 Value : 0

Key : 6 Value : 18

Key : 7 Value : 21

Key : 8 Value : 24

Key : 9 Value : 27

All the class code is in a header because templates classes can’t be split between header and cpp without having the main method inside the cpp file. Is there something I should check or is it something to do with the code being inside the header?

EDIT: This is the code I’m using to get the output. It’s in a file called “main.cpp”

#include <iostream>
#include <vector>
#include "DictionaryList.h"

using namespace std;


void main()
{
DictionaryList<int,int> list;

for (int i = 0; i < 11; i++)
{
    list.Add(i, i*3);
}

for (int i = 0; i < 10; i++)
{
    cout << "Key    : " << list.Exists(i*3) << "    Value   : " << list.Get(i) << endl;
}

cout << "Find value 5: " << list.Get(5) << endl;

cout << "Remove Value 6" << endl;
list.Remove(6);

for (int i = 0; i < 10; i++)
{
    cout << "Key    : " << list.Exists(i*3) << "    Value   : " << list.Get(i) << endl;
}

system("pause");
}
  • 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-14T11:34:39+00:00Added an answer on June 14, 2026 at 11:34 am

    You get the observed output because:

    • You are removing element 5 rather than 6. Having found the index i corresponding to the requested key, you then erase element i-1. Change the erase arguments to begin() + i to remove the expected key/value pair.

    • Your output loop prints a line for each possible key, whether or not it’s there, hence the line saying Key : 0 Value : 0 where the removed element was. I guess List.Exists() looks up a value, returning a key if it’s found and zero if it’s not, and List.Get() returns zero if the key is not found. That behaviour will lead to errors: there’s no way to distinguish missing elements from zero-valued elements.

    Even if you don’t want to use the standard map containers for some reason, I suggest giving your container a similar interface; as well as being familiar to people who use the standard containers, a lot of thought went into making their interfaces difficult to misuse.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace

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.