Vector of Structs Question

This is a discussion on Vector of Structs Question within the C++ Programming forums, part of the General Programming Boards category; Say i have a vector of structs Code: struct Crap{ int number; int name; } and in main i have ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    230

    Vector of Structs Question

    Say i have a vector of structs
    Code:
    struct Crap{
        int number;
        int name;
    }
    and in main i have this vector

    Code:
     vector <Crap> ca
    Now ive called this to a function that wants to look through this vector of structs. Say i got to the 3rd vector which lets say had
    a number of 10 and a name of john. How do i delete this vector.
    I've seen pop_back() and erase() but i havent seen it used for a vector of structs and the syntax is diff because you have to do things like
    ca.name to access specific parts of the stuct of elements.
    I'm doing this for a program im writing but b/c of my code i hope this example explains what i mean.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    You can use erase on a vector. It takes an iterator. If all you have is an index, use erase(begin() + index); to erase the proper entry.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Right ive tried that. This is what i tried.

    Code:
    procVector.erase(procVector.begin() + j);
    procVector is my vector of course and J is the position it is in when i searched the loop.... but this is the error message i get.

    [/code]
    d:\Poly\CS1124\hw01\hw01\hw01.cpp(89): error C2663: 'std::vector<struct Process,class std::allocator<struct Process> >::erase' : 2 overloads have no legal conversion for 'this' pointer
    [/code]

    If i did erase will it erase everything for that vector like name and # cuz thats what i want...
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    It will only erase the element at the index j. So if you had ten structs in your vector, j is 4, and the one at index 4 had the name and number you want to get rid of then erase(begin() + j) will only erase that one and you will have 9 elements in your vector (everything after 4 will move down one space).

    As for your error, you might want to show a little more code - the declaration of procVector, the function definition (is it a const function?), the declaration of j, etc.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    okay i didnt want 2 cuz now i might have to explain the problem but here is my code.... the important parts.. FYI the vector fills correctly from the stream b/c i tested that.

    My Struct
    Code:
    struct Process //Structure will hold all process information from the procces file
    {	
    	
    	int procId; // process ID ... 1000.. 1001 etc
    	string name;  // holds the name of the process
    	int cpuTime; // Total CPU time needed for process
        int timeBurst; // Burst for each process
    	
    };
    and here is my function... i apologize if its distorted .. its hard to make it look normal here.
    Code:
    void procSched(const vector<Process>& procVector)
    {	
    	// totProc is the total number of Proccess that need to run
    	
    	
    	while (!procVector.empty()) // Continue to process until vector is empty
    	{
    		for (size_t j = 0; j < procVector.size(); j++) // Loop through the vecotr 
    		{  // doo scheduling things here.
    			//output the process information
    			cout << procVector[j].procId << ' ' << procVector[j].name << ' '
    				 << procVector[j].cpuTime  << ' ' << procVector[j].timeBurst << endl;
    			
    			if (procVector[j].cpuTime <= 0) // If time is below 0, the process is exhausted
    			{
    				
    				procVector.erase(procVector.begin() + j);
    			}
    		}	
    	}
    }
    Its the erase section at the end as u can c that i want out... i want to eliminate that vector incluing the time, the burst, the name etc.
    Last edited by gamer4life687; 09-09-2005 at 09:59 PM.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Your procVector variable is const (you have a const reference parameter to your function). You cannot erase from a const vector.

    If you want the function to be allowed to erase from the vector, just remove the const and make it a plain reference.

    If I understand you correctly everything else looks fine. The wording of your question is wrong, though. You want to erase an element in the vector. You don't want to erase a vector like you've been saying.

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Yea thx i just realized that ... i was going by what i wrote on paper first and just copying.. this is just a first rough run through but nice catch.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Structs question
    By BigFish21 in forum C Programming
    Replies: 25
    Last Post: 04-23-2008, 09:57 PM
  3. Replies: 5
    Last Post: 02-20-2004, 08:36 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

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