繁体   English   中英

select 使用交换在 C++ 中排序的问题

[英]Problem with select sort in C++ using swap

我正在尝试完成此代码,现在我面临选择排序算法的错误:

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

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

我必须按图的顶点度数对图进行排序。 有人可以帮助我吗? 谢谢!

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

更改签名

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

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

您之前已声明该向量const ,这意味着您不得更改其内容。 交换要求您能够将非 const指针传递给 int,但您将const指针传递给 int。

我建议你阅读一些关于const 正确性的文章

您的代码还存在其他一些问题。 您对交换的定义最好通过引用来获取整数。


#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;
}

现在编译。 我没有检查它的运行时正确性。

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

(ps我发现godbolt更方便尝试片段。YMMV)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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