Thread: Newbie needs help removing elements from an array

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    9

    Newbie needs help removing elements from an array

    I was wondering if anyone could tell me how to remove elements from an array after a specific search criteria is met. For example, if the following data is contained in an array

    Serial Number TV Brand Screen Size
    1293837136 Sony 32
    0987654321 Samsung 27
    5432167890 JVC 19
    0011234567 Sony 32


    I want to remove the tv with the serial number 5432167890. So I know I will have a loop that determines if serial number is equal to the input value of 5432167890, but I don't know how to remove all of the information associated with that serial number (5432167890 JVC 19).

    If anyone can help I'd really appreciate it.

    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use a std::vector, which has an erase() member function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    9
    Is there a way to convert my array of data structures to an vector? I have to use data structures because there are multiple data types.

    My code looks like:

    Code:
    struct tv_in{
     int serialNum;
     string tvBrand;
     int screenSize;
    }new_tv [IN_TV];

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You would then use a std::vector<tv_in>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Maybe you can create a vector of tv_ins and have a loop that passes

    Code:
    void erase_record(const int& serial, std::vector<tv_in>& records)
    {
       for(std::vector::iterator i = 0; i < records.end(); i++)
       {
          if(i->serialNum == serial)
             records.erase(i);
             i--;
       }
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe you can create a vector of tv_ins and have a loop that passes
    Erasing element by element can be rather slow.

    Considering that the serial number should probably be a string rather than an int, I would suggest:
    Code:
    struct tv_in{
    	std::string serialNum;
    	std::string tvBrand;
    	int screenSize;
    };
    
    class serialNumIsEqual
    {
    public:
    	serialNumIsEqual(const std::string& serialNum) : serialNum_(serialNum) {}
    	bool operator()(const tv_in& x) const
    	{
    		return x.serialNum == serialNum_;
    	}
    private:
    	std::string serialNum_;
    };
    Then the actual erasing would be:
    Code:
    new_tv.erase(
    	std::remove_if(new_tv.begin(), new_tv.end(), serialNumIsEqual("5432167890")),
    	new_tv.end());
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    9
    I am trying to erase the elements from the vector; however, I am receiving the following error:
    error C2228: left of '.erase' must have class/struct/union

    Am I referencing the wrong pointer?

    My code is as follows:

    Code:
    void deleteTV()
    {
    	string deleteSerialNum;
    	int d = 0;
    
    	cout << "Please select the serial number you would like to search:" << endl;
    	cin >> deleteSerialNum;
    	
    	for (int d= 0; d < IN_TV; d++)
    	{
    		if (deleteSerialNum == new_TV[d].inSerialNum)
    		{
    			      new_tv.erase(new_tv[d].inSerialNum,new_tv[d].inTvBrand,new_tv[d].inScreenSize);
    		}
    	}
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is new_tv? In my example it is a std::vector<tv_in>

    Then, the erase member function either takes a single iterator, or a pair of iterators to denote a range.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    9
    This is the error I receive when I use the tv_in as my pointer.


    error C2143: syntax error : missing ';' before '.'
    error C2143: syntax error : missing ';' before '.'

    The new_tv is the pointer I was using for that array when it was an array.

    My data structure looks like this:

    Code:
    :
    
    struct tv_in{
     int serialNum;
     string tvBrand;
     int screenSize;
    }new_tv [IN_TV];
    This is the code declaring that struct a vector
    Code:
    std::vector<tv_in>;
    This is the function I have defined to erase the elements using the tv_in
    Code:
    void deleteTV()
    {
    	string deleteSerialNum;
    	int d = 0;
    
    	cout << "Please select the serial number you would like to search:" << endl;
    	cin >> deleteSerialNum;
    	
    	for (int d= 0; d < IN_TV; d++)
    	{
    		if (deleteSerialNum == new_TV[d].inSerialNum)
    		{
    			     tv_in.erase(new_tv[d].inSerialNum,new_tv[d].inTvBrand,new_tv[d].inScreenSize);
    		}
    	}
    }
    Last edited by daki76; 06-20-2006 at 09:30 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    My data structure looks like this:
    It is more than a definition of a struct, but also creates an array named new_tv with IN_TV number of tv_in objects.

    This is the code declaring that struct a vector
    One cannot declare a struct to be a vector. One can create a vector of structs. In this case you have a declaration that does not really declare anything, but if you did provide a name, then it would be a vector of vehicles_in.

    This is the function I have defined to erase the elements using the tv_in
    1. new_tv is an array. Arrays do not have an erase member function.
    2. new_tv[d] is the dth element of that array, and is a tv_in. A tv_in is not an iterator.
    3. the erase member function of std::vector takes either one or two arguments, not three.

    You may find the reference material on cppreference.com and SGI's Standard Template Library Programmer's Guide useful.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Using a vector is a better idea, but if you must use a C style array you cannot "erase" from it. You have to work around that by either moving up all the entries after the one you want to erase, and remembering that the last entry is not valid, or by dynamically creating your array and copying everything but the erased element into a new array. Neither of those are good choices compared to the ability to erase from a vector.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 11:26 AM
  2. reversing elements in array
    By dantestwin in forum C++ Programming
    Replies: 6
    Last Post: 07-08-2004, 08:24 PM
  3. funtion dealing with positive array elements
    By dantestwin in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2004, 05:20 PM
  4. Comparing a 2d Array (newbie)
    By Cockney in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2001, 12:15 PM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM