c++ - Organise output data according to an order -
i organise output of following code. please advise on how can procede.
stringstream ss; (i = 0; < vecdb.size(); i++) { (j = 0; j < vecuser.size(); j++) { if (vecdb[i].accesslevel == vecuser[j].accesslevel) { ss << vecuser[j].accesslevel << endl; } else if (vecdb[i].accesslevel < vecuser[j].accesslevel) { ss << vecuser[j].accesslevel << endl; } else if (vecuser[j].accesslevel < vecdbi].accesslevel) { ss << vecuser[j].accesslevel << endl; } } } cout << ss.str();
current output
level0 level3 level2 level4 level0 level0 level1 level4 level2 level3
desired output
level0 level0 level0 level1 level2 level2 level3 level3 level4 level4
first of all, logic messed up:
- if
vecuser[j]
==vecdbi[i]
outputvecuser[j].accesslevel
- if
vecuser[j]
<vecdbi[i]
outputvecuser[j].accesslevel
- if
vecuser[j]
>vecdbi[i]
outputvecuser[j].accesslevel
- if nothing of above output
vecuser[j].accesslevel
no matter output vecuser[j].accesslevel
.
in other words can replace this:
if (vecdb[i].accesslevel == vecuser[j].accesslevel) { ss << vecuser[j].accesslevel << endl; } else if (vecdb[i].accesslevel < vecuser[j].accesslevel) { ss << vecuser[j].accesslevel << endl; } else if (vecuser[j].accesslevel < vecdbi].accesslevel) { ss << vecuser[j].accesslevel << endl; }
with following , still same result.
ss << vecuser[j].accesslevel << endl;
i don't know want achieve logic, it's broken.
if wish output strings in order why not sort them?
std::vector<std::string> sorted{std::begin(vecuser), std::end(vecuser)}; std::sort(std::begin(sorted), std::end(sorted)); (const auto& s : sorted) { std::cout << s << std::endl; }
Comments
Post a Comment