c++ - Im trying to sort these nodes by alphabetical order -
void primaryindex::sortbytitle(){ p_node* temp; (int temp = 0; temp < size(title); temp++) { temp = null; if (temp->title > temp->next->title) { swap(); } } }
i know wrong, i'm using nodes program. here primaryindex
header file function declared
class p_node { friend class primaryindex; private: std::string title; int pos; p_node * next; p_node * prev; public: p_node() : title("hi"), pos(0) { next = prev = null; } p_node(std::string my_title, int my_pos) : title(my_title), pos(my_pos) { next = prev = null; } };
class primaryindex { friend class p_node; private: p_node * head; p_node * tail; int size; public: primaryindex() : size(0) { head = new p_node(" ", -1); tail = new p_node("~", -1); head->next = tail; tail->prev = head; } // functions using ~primaryindex() { killlist(); } bool set_title_key(std::string my_title, int my_key); void writeprimary(); void readprimary(); void change_title(std::string new_title, int key); int matchtitle(std::string intitle, int key); void killlist(); void deletebytitle(); void sortbytitle(); void revsortbytitle(); };
i have lot of these functions implemented , trying hang of these nodes. i'm hoping help me out this
following suggestion of paulmckenzie's comment: forget of code , utilize std::list
. storage type -- doubly linked list -- class trying achieve. utilize std::sort
.
std::list<std::string> mylist{"title1", "title2","titlen"}; std::sort(mylist.begin(),mylist.end());
if isn't sufficient you, utilize own custom class , pack in list, utilize std::sort
custom predicate:
struct myclass { std::string title; //... more }; std::list<myclass> mylist; std::sort(mylist.begin(), mylist.end() , [] (myclass const& a, myclass const& b) { homecoming a.title<b.title;});
c++ sorting
cool stuff you have and you keep overhaul every one of us
ReplyDeletetrig identities formulas
cool stuff you have and you keep overhaul every one of usa3trading
ReplyDelete