-
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;
}
-
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.