When I try to use swap() in in the function partitionk(), I get the error
" error: request for member 'swap' in '(& num_list)->std::vector<_Tp, _Alloc>::operator[] >(((std::vector::size_type)endn))', which is of non-class type '__gnu_cxx::__alloc_traits >::value_type {aka int}'| " `
enter code here
#include <iostream>
#include <vector>
#include <unordered_map>
#include <cstdlib>
using namespace std;
int partitionk(vector<int>& num_list , int start, int endn ) {
int pindex = start;
int rand_num = rand( ) % endn;
num_list[endn].swap(num_list[rand_num]); // getting error
for (int i = 1 ; i < endn ; i++){
if ( num_list[i] < num_list[endn] ){
num_list[i].swap( num_list[pindex] ); // getting error
pindex += 1;
}
}
num_list[endn].swap(num_list[pindex]); // getting error
return pindex;
}
void quick_sort( vector<int>& num_list , int start, int endn ){
if (start >= endn) return ;
else{
int index = partitionk( num_list , start, endn ) ;
quick_sort( num_list , start, index );
quick_sort( num_list , index+1, endn );
}
}
int main()
{
vector <int> nums= {4,7,1,3,9,5};
quick_sort(nums , 0 , nums.size()-1 );
for (auto i : nums){
cout << i << " ";
}
}
num_list[endn]
is anint
which doesn't have any member functions, yet you try to callswap
on it. What is it you are trying to do? – DeiDei