C++ - Fastest way to add an item to a sorted array

后端 未结 8 1103
孤城傲影
孤城傲影 2021-01-23 05:18

I\'ve got a database with approximately 200 000 items, which is sorted by username. Now when I add an item to end of array and call my quick sort function to sort that array it

8条回答
  •  被撕碎了的回忆
    2021-01-23 05:47

    the solution is to rewrite your code to use the stl, I don't understand why people write C code in C++.

    You need a vector of User

    std::vector users;
    //then you can keep it ordered at each insertion
    auto it = upper_bound(users.begin(), users.end(), user_to_insert, 
        [](auto& lhs, auto& rhs ) { /* implementation left to the reader */});
    users.insert(it, user_to_insert);
    

    You now have the same functionality in a much nicer and clean way

提交回复
热议问题