Thread: vector - struct - one last time

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    128

    vector - struct - one last time

    I tried using a class based vector for sorting more than one column. Let's just say I have a lot of code that uses that struct and going to class-based caused a lot of problems.

    My next thought was make the first column in the struct a "catch all" in which to hold my data in the order I wanted sorted.

    I've tried that in the below code to no avail. It runs fine but not sorted.

    Code:
    	struct record
    	{
    		std::string sortvalue;
    	    std::string corp;
    	    std::string lastname;
    	    std::string firstname;
    	    std::string lwi;
    	    std::string grade;
    	    std::string tanf;
    	    std::string ssn;
    	};
    
    	std::vector<nsSVector::record*>::iterator start = schoolvector.begin();
    	std::vector<nsSVector::record*>::iterator stop = schoolvector.end();
    
    	while (start != stop) 
    	{
    		if (VD.validateCorp((*start)->corp) == 0)
    		{
    			VD.validateNames("LAST",(*start)->lastname);
    			VD.validateNames("FIRST",(*start)->firstname);
    			VD.validateLWI((*start)->lwi);
    			VD.validateGrade((*start)->grade);
    			VD.validateTANF((*start)->tanf);
    			VD.validateSSN((*start)->ssn);
    			(*start)->sortvalue = (*start)->corp + 
    (*start)->lastname + 
    (*start)->firstname + 
    (*start)->grade + 
    (*start)->ssn;
    			++start;
    		}
    		else
    		{
    			start = schoolvector.erase(start);
    		}
    	}
    
    
    	
    	std::sort( schoolvector.begin(), schoolvector.end() );

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    never mind, I'm trying that class-based vector again.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    structs and classes are the same thing. Any problems you have with the struct based version will be the same with the class based version. Any solutions you have for the class based version can be applied to the struct based version.

    In the code above, the vector holds pointers, and the sort is using the default sort for the items in the vector. By default, pointers are sorted by their pointer value. You need a specialized sort function that sorts nsSVector::record pointers.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    structs and classes are the same thing.
    Basicly. structs are public by default and classes are private by default.
    Code:
    (*start)->sortvalue
    Are you sure that's what you want? Is is declared as **start?
    Code:
    	struct record
    	{
    		std::string sortvalue;
    	    std::string corp;
    	    std::string lastname;
    	    std::string firstname;
    	    std::string lwi;
    	    std::string grade;
    	    std::string tanf;
    	    std::string ssn;
    	};
    That creates a structure definition. It doesn't actually create a struct.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 02:03 AM
  2. Replies: 11
    Last Post: 03-29-2009, 12:27 PM
  3. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  4. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM