vector sort

This is a discussion on vector sort within the C++ Programming forums, part of the General Programming Boards category; Here is a shortened version of my code (shortened and placed in one file). It's supposed to read in files ...

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

    vector sort

    Here is a shortened version of my code (shortened and placed in one file). It's supposed to read in files like:
    1234,smith,bob
    1234,smith,adam
    1234,jones, carl

    then does a bunch of validation whcih I haven't included.

    and spit out
    1234,jones, carl
    1234,smith,adam
    1234,smith,bob

    The problem is it doesn't like my record* in the less1 line.

    I know everything is based on std::vector<record*> schoolvector
    and I think the less1 sort issue needs the values for record, not hte pointer. I can't figure out how to give it what it wants.

    References and pointers work faster but I can't figure out how to work those in conjunction with my sort.

    -nearing my end. (I thought I was doing so well)

    Code:
    #pragma warning( disable : 4786)
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <functional>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <cstdlib> 
    
    class record
    {
    public:
    	std::string corp;
    	std::string lastname;
    	std::string firstname;
    	friend bool less1(const record& a, const record& b);
    };
    
    //exception struct
    struct read_exception { };
    
    //populate vector from file
    void initialize_vector(std::vector<record*>& vec);
    //defines sort order
    inline bool less1(const record& a, const record& b);
          
    void main()
    {
    
    	//identify schoolvector based on record class/struct
    	std::vector<record*> schoolvector;
    	//populate from file
    	initialize_vector(schoolvector);
    	std::cout << "initialized." << std::endl;
    
    	//sort by specific column order
    	std::sort(schoolvector.begin(), schoolvector.end(), less1);
    
    	//view output to show that it's sorted.
    	std::vector<record*>::iterator start = schoolvector.begin();
    	std::vector<record*>::iterator stop = schoolvector.end();
    	for( ; start != stop; ++start)
    	{
    		std::cout << (*start)->corp << "," 
    << (*start)->lastname << "," 
    << (*start)->firstname << std::endl;
    	}
    
    }
    
    
    
    void initialize_vector(std::vector<record*>& vec)
    {
    	
    	std::string sdetailline;
    	std::string tempstring;
    	int colcount = 0;
    
    	//assign tmp as pointer to record struct
    	record* tmp;
    	std::ifstream fin;
    	fin.open ("zz.csv", std::ios::in);    
    	if(fin.fail())
    		{
    		throw read_exception();
    		}
    	while (std::getline(fin, sdetailline))
    	{
    		int colcount = 0;
    		tmp = new record;
    		std::istringstream istrLine(sdetailline);
    		while (std::getline(istrLine, tempstring, ','))
    		{
    			if (tempstring == "")
    			{
    				tempstring = " ";
    			}
    			switch (colcount)
    			{
    			case 0:
    				tmp->corp = tempstring;
    				break;
    			case 1:
    				tmp->lastname = tempstring;
    				break;
    			case 2:
    				tmp->firstname = tempstring;
    				break;
    			
    			}
    		colcount++;
    		}
    		vec.push_back(tmp);
    	}
    	fin.close();
    }
    
    inline bool less1(const record& a, const record& b) 
           { 
    		int res = a.corp.compare(b.corp);
    		if (res < 0) return true;
    		if (res > 0) return false;
    
    		res = a.lastname.compare(b.lastname);
    		if (res < 0) return true;
    		if (res > 0) return false;
    
    		res = a.firstname.compare(b.firstname);
    		if (res < 0) return true;
    		if (res > 0) return false;
    
    
    		return false;
    		
    	}

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Can you convert the less1 function into one that takes pointers as parameters instead of references? Either that or you could change the program to use a vector<record> container instead of a vector<record*> container.
    "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

Popular pages Recent additions subscribe to a feed

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