Skip to content
  • resorting ONE value in a sorted vector

    Unsolved General and Desktop qvector algorithms sort int qstring
    10
    0 Votes
    10 Posts
    1k Views
    Chris KawaC
    Btw. to put my money where my mouth is this is my proposal for this problem. Yes, I'm assuming the format is fixed and search operations won't return -1. If that isn't the case you can add one if to range check the indices. std::ranges::sort(vec, [](QStringView a, QStringView b) { int pos_a = a.lastIndexOf(' '); int pos_b = b.lastIndexOf(' '); int int_a = a.sliced(pos_a).toInt(); int int_b = b.sliced(pos_b).toInt(); return int_a < int_b; }); Do you consider this unreadable? And no, this is not fully optimized either, because it does the same int conversions multiple times, but I consider something like this to be a "good enough starting point" and in-depth optimization is possible if need arises e.g. by caching the conversions or changing the data structure. EDIT Just after posting I realized you can do the QStringView creation right in the params, so even simpler.
  • c++ quetion string- int get the last character

    Solved General and Desktop c++ string int
    2
    0 Votes
    2 Posts
    668 Views
    JonBJ
    @RuWex Iterate along the characters in the string till you find a digit; or QString::indexOf(QRegularExpression("m[0-9]")) probably does this. The last character before the digit is the index minus 1.
  • 0 Votes
    3 Posts
    518 Views
    JonBJ
    @jsulm OoI, why did Qt6 QString feel the need to introduce sliced() when we have had mid() for years? EDIT Oh, mid() docs now say If you know that position and n cannot be out of bounds, use sliced() instead in new code, because it is faster.