sorting - Print struct inputs in alphabetic order C++ -
i want print strings struct in alphabetic order, , have got thread how alphabetically sort strings?, sorting. problem when run compiler sorted output includes name struct. code looks this:
#include <iostream> #include <set> #include <algorithm> #include <string> using namespace std; const int antalshops = 2; const int antalworkers = 5; struct employ { string workername; int workerage; }; struct themall{ string shopname; string shoptype; int shopsize; employ workername; employ workerage; }; // declaration of structs themall shops[antalshops] = { {"gamestop","toy", 250,}, {"frandsen", "cloth", 300,}, }; employ workers[antalworkers] = { {"andrea valente", 41}, {"giovanni pirolli", 25}, {"marco cipolli", 33}, {"jensine jensen", 19}, {"andrea jensen", 99}, }; // functions sorting , printing names void print(const string& item) { cout << item << endl; } void printworkers(employ workers[]) { set<string> sortedworkers; for(int = 0; <= antalworkers; ++i) { sortedworkers.insert(workers[i].workername); } for_each(sortedworkers.begin(), sortedworkers.end(), &print); } void printshops(themall shops[]) { set<string> sortedshops; (int = 0; <= antalshops; ++i) { sortedshops.insert(shops[i].shopname); } for_each(sortedshops.begin(), sortedshops.end(), &print); } int main(int argc, const char * argv[]) { printshops(shops); }
so have structs workers , shops, when i try printing shop names printshops(shops)
function output:
andrea valente frandsen gamestop
i have been looking through code, can't find mistake is, can see error?
you go outside bounds of arrays in loops
for (int = 0; <= antalshops; ++i) { // problem here ^^
the above loop loop on indexes 0
, 1
and 2
, 1 many two-entry array. lead undefined behavior.
you have same problem other sorting loop.
Comments
Post a Comment