Find element in SET container

This is a discussion on Find element in SET container within the C++ Programming forums, part of the General Programming Boards category; I am not sure how to find an element in a SET and report back that it was found. Using ...

  1. #1
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36

    Find element in SET container

    I am not sure how to find an element in a SET and report back that it was found. Using the code below, it always returns false.

    Specialty.h
    Code:
    #include <set>
    
    using namespace std;
    
    class Specialty	{
    
    private:
    	set<char*> SpecialtySet;
    	set<char*>::iterator it;
    	void SetSpecialty();
    
    public:
    	bool VerifySpecialty(char *);
    };
    Specialty.cpp
    Code:
    #include "Specialty.h"
    #include <set>
    #include <iostream>
    using namespace std;
    
    void Specialty::SetSpecialty ()	{
    	SpecialtySet.insert("CARD");	// Cardiology
    	SpecialtySet.insert("DERM");	// Dermatology
    	SpecialtySet.insert("GP");	// General Practice
                           :
                           :
    	SpecialtySet.insert("PSY");	// Psychiatry
    	SpecialtySet.insert("SUR");	// Surgeon
    }
    
    bool Specialty::VerifySpecialty(char *spec) { 
    	bool result;
    
    	it = SpecialtySet.find(spec);
    	cout << "Test: ";
    	cout << *it << endl;
    	system ("pause");
    	if (it == SpecialtySet.end())
                             	result = false;
    	else
    		result = true;
    	return result;
    }
    Main.cpp, If I enter "DERM" or any other code then "GP" gets set every time.
    Code:
                    Specialty Spec;
    	char *sCode = new char [5];
                             :
                             :
    
    	cout << "\n Enter Specialty Code: ";
    	cin >> sCode;
    
    	if (Spec.VerifySpecialty(sCode))
    		room[number-1].SetCode(sCode);
    	else
    		room[number-1].SetCode("GP");

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your set contains pointers. This way the set is ordered just by comparing pointers.
    You insert just string literals into that set but you call the find function with a char *. An element that has the same address will never be found.

    Use a set of std::string instead.
    Kurt

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    To do it your way, you would have to move through the set container from beginning to end and do a strcmp until you found what you were looking for (return true) or reached the end (return false). This negates the benefits of the container's normally quick lookup functionality. You would not be able to use the find member. I second the use of a set<string> instead.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Well, you could define a custom comparator ...

    Nah, just use std::string.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to find the middle element of a bulk array?
    By void_mehboob in forum C Programming
    Replies: 4
    Last Post: 04-19-2009, 11:37 PM
  2. Replies: 4
    Last Post: 01-05-2008, 10:30 PM
  3. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM
  4. find the k-th smallest element in O(logk) time
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-22-2001, 03:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21