The problem is your vector definiton
vector<int> C(max + 1);
consider you have 3 items in vector A
10,20,100
Here you define a max value
int max = A[0];
for (int i = 1; i < A.size(); i++) {
if (A[i] > max)
{
max = A[i];
}
}
now size of vector is 3 , and the variable
max
contains 100
and your vector with 101 items (preallocated)
vector<int> C(max + 1);
and your code as following
vector<int> C(max + 1);
vector<int> B(A.size());
for (int i = 0; i < C.size(); i++)
{
C[A[i]] = C[A[i]] + 1;
}
size of C is 101 whereas size of A 3 , which causing access violation
you vector definition should be like below
vector<int> C(A.size());
vector<int> B(A.size()); </int>