std:sort() returns all zero

I tried to sort a large numbers of vector of random integers with std::sort(), but when the number increases over 10M, std::sort returns all zero in values.
Does std::sort have a limitation of input numbers?
Last edited on
std::sort doesn't return anything. It operates on existing values.

Perhaps you could supply a short code sample that reproduces the problem you are experiencing?

Does the following exhibit the same issue?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <random>
#include <iostream>
#include <vector>
#include <cstdint>
#include <limits>
#include <algorithm>

int main()
{
    std::mt19937 engine { std::random_device()() };
    std::uniform_int_distribution<std::uint64_t> dist { 1, std::numeric_limits<std::uint64_t>::max() };

    std::vector<std::uint64_t> vals;
    vals.reserve(11000000);
    for (unsigned i = 0; i < 11000000; ++i)
        vals.push_back(dist(engine));

    auto val_to_check = vals[dist(engine, { 0, vals.size() - 1 })];
    unsigned n_before_sort = 0;

    for (auto val : vals)
        if (val == val_to_check)
            ++n_before_sort;

    std::cout << "There are " << n_before_sort << " occurrences of " << val_to_check << " prior to the sort.\n";

    std::sort(vals.begin(), vals.end());

    auto it = std::lower_bound(vals.begin(), vals.end(), val_to_check);

    unsigned n_after_sort = 0;
    while (*it == val_to_check)
        ++n_after_sort, ++it;

    std::cout << "There are " << n_after_sort << " occurrences of " << val_to_check << " after the sort.\n";
}

Topic archived. No new replies allowed.