简体   繁体   中英

Problem with select sort in C++ using swap

I'm trying to finish this code, and right now I am facing a error with the Selection Sort Algorithm:

error: no matching function for call to 
‘swap(const std::vector<int>*, const std::vector<int>*)’
       swap(&adj[min_idx], &adj[j]);

and

invalid conversion from ‘int’ to ‘int*’ [-fpermissive]
    b = temp;

I have to sort the graph by the number of it's vertices degree. Can someone can help me? Thank you!

void swap(int *a, int *b) {    
    int temp;
    temp = a;
    a = b;
    b = temp;
}

void sortGraph(vector<int> const *adj, size_t count) {
    int size = count;
        
    //sort by degree, number of children
    for (int j = 0; j < size - 1; j++) {
        int min_idx = j;
        for (int i = j + 1; i < size; i++) {
            if (adj[i].size() < adj[min_idx].size())
                min_idx = i;
        }
        swap(&adj[min_idx], &adj[j]);
    }
}

void printGraph(std::vector<int> const* adj, size_t count) {
    std::vector<size_t> indices;
    for (size_t i = 0; i != count; ++i)
    {
        indices.push_back(i);
    }

    for (auto index : indices)
    {
        std::cout << "Vertex " << index << ", degree " << adj[index].size() << '\n';
    }
}
    
void addEdge(vector<int> adj[], int u, int v) {
    adj[u].push_back(v);
    adj[v].push_back(u);
}
   
int main() {
    int V = 5;
    vector<int> adj[V];
    addEdge(adj, 0, 2);
    addEdge(adj, 0, 1);
    addEdge(adj, 0, 3);
    addEdge(adj, 0, 4);
    addEdge(adj, 2, 1);
    addEdge(adj, 4, 1);
    //printGraph(adj, V);
    sortGraph(adj, V);
    return 0;
}

https://ideone.com/mgffaj

Change the signature of

void  sortGraph(vector<int> const* adj, size_t count)

to

void  sortGraph(vector<int> * adj, size_t count)

You have previously declared that the vector is const which means you are not allowed to change it's contents. Swap requires you to be able to pass non const pointers to int but you are passing const pointers to int.

I suggest you read some articles on const correctness

You also had a couple of other problems with the code. Your definition of swap would be better to take the integers by reference.


#include<bits/stdc++.h>
#include <stdio.h>
using namespace std;

void swap(int&a, int&b) {    
   int temp;
   temp = a;
   a = b;
   b = temp;
}


void  sortGraph(vector<int> * adj, size_t count){
  int size = count;
    
    //sort by degree, number of children
         for (int j = 0; j < size - 1; j++) {
            int min_idx = j;
            for (int i = j + 1; i < size; i++) {
              if (adj[i].size() < adj[min_idx].size())
                 min_idx = i;
            }
            swap(adj[min_idx], adj[j]);
    
          }
            

}


void printGraph(std::vector<int> const* adj, size_t count)
{
    std::vector<size_t> indices;
    for (size_t i = 0; i != count; ++i)
    {
        indices.push_back(i);
    }

    for (auto index : indices)
    {
        std::cout << "Vertex " << index << ", degree " << adj[index].size() << '\n';
    }
}

void addEdge(vector<int> adj[], int u, int v)
{
    adj[u].push_back(v);
    adj[v].push_back(u);
}


int main()
{
int V = 5;
vector<int> adj[V];
addEdge(adj, 0, 2);
addEdge(adj, 0, 1);
addEdge(adj, 0, 3);
addEdge(adj, 0, 4);
addEdge(adj, 2, 1);
addEdge(adj, 4, 1);
//printGraph(adj, V);
sortGraph(adj, V);
return 0;
}

compiles now. I have not checked it for runtime correctness.

https://godbolt.org/z/4r1o9bjez

(ps I find godbolt more convienient for trying out snippets. YMMV )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM