swap() 函数不适用于通过引用 c++ 传递的向量对象

swap() function not working on a vector object passed by reference c++

当我尝试在函数 partitionk() 中使用 swap() 时,出现错误

" 错误:在'(& num_list)->std::vector<_Tp, _Alloc>::operator[] >(((std::vector ::size_type)endn))',它是非 class 类型的 '__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 << " ";
    }

}

使用std::swap():

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

矢量成员 swap() 用于交换 entire vectors。并且您对 swap() 的使用试图调用向量项的交换成员,即 int :这种类型没有 swap()