1

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 << " "; } } 
1
  • num_list[endn] is an int which doesn't have any member functions, yet you try to call swap on it. What is it you are trying to do? Commented Jun 3, 2017 at 7:33

1 Answer 1

3

Use std::swap():

std::swap(num_list[endn], num_list[num]]; 

The vector member swap() is meant for swapping entire vectors. And your use of swap() tries to call a swap member of a vector item, that is an int : there's no swap() for this type.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.