【问题标题】:I'm getting a conversion error in my C++ solution for TSP我在 TSP 的 C++ 解决方案中遇到转换错误
【发布时间】:2019-10-30 01:45:18
【问题描述】:

我正在尝试使用多维数组在 C++ 中解决 TSP,并收到有关类型转换的错误消息。

我已经对 C++ 生疏了几年没有使用它,所以为了重新学习,我决定尝试一些 Traveling Salesman 解决方案。我使用的第一个使用多维数组来存储随机分配的点。那部分工作得很好,所以我继续使用距离公式。我为基本距离公式创建了一个辅助函数,该函数将 2 个数组作为其输入,这些数组本身运行良好,然后创建了一个函数来查找整个城市数组的总距离。它接受一个双精度数组和一个表示城市总数的整数,然后遍历数组,找到每个点的距离并将它们加在一起。

这里是变量声明和随机点赋值

    int numCities = 10;
    double cities[numCities][2];

    //Creates random(unseeded) points
    for(int i = 0; i < numCities; i++){
        for(int j = 0; j < 2; j++){
            cities[i][j] = (rand() % 100) + 1;
        }
    }

这是用于调用函数的行

    cout << distTotal(cities, numCities) << endl;

这是函数和辅助函数

    //basic distance formula
    double cityDist(double cityA[], double cityB[]){
        return sqrt(pow((cityB[0]-cityA[0]), 2.0)+
                    pow((cityB[1]-cityA[1]), 2.0));
    }

    //calculate total distance of group of cities
    double distTotal(double* points[], int num){
        double total = 0;
        for(int i = 0; i < num-1; i++){
            total=total+cityDist(points[i], points[i+1]);
        }
        return total;
    }

所以理想情况下,这应该给我这里给出的基本顺序中所有点之间的总距离。但是,我目前收到以下错误:

错误:无法将参数 '1' 的 'double (*)[2]' 转换为 'double**' 到 'double distTotal(double**, int)'

如果我没记错的话,这可能与指针有关,但老实说,我对 C++ 指针的记忆还不够,不知道如何修复它。

感谢任何帮助,谢谢

【问题讨论】:

  • 帮自己一个忙,把它设为std::array&lt;std::array&lt;double, 2&gt;, 10&gt; cities;。然后,您就有了一个可用于两个维度的 size() 函数,并且每当您将数组作为函数的参数提供时,都不需要传递伴随变量。
  • 好的,试试看。使用这种格式,如何避免上述错误?
  • 你把函数的签名改成:double distTotal(std::array&lt;std::array&lt;double, 2&gt;, 10&gt;&amp; points);
  • 你需要适当地更改所有函数的签名double cityDist(const std::array&lt;double, 2&gt;&amp; cityA, const std::array&lt;double, 2&gt;&amp; cityB);
  • 关闭。 &amp; 表示该函数将收到对该对象的引用(而不是复制整个事物)。 const 部分表示该函数承诺不会更改对象。

标签: c++ codeblocks traveling-salesman


【解决方案1】:

您的声明应该是double* points,因为 C 数组会衰减为指针。如果使用 c++,您可以考虑使用 std::vector&lt;double&gt;&amp; 作为输入。

编辑:如果你最终使用 c 数组,你将不得不在堆上分配它们并释放资源。

【讨论】:

  • 好的,我试试向量。为了我自己的启迪,在 C++ 中,向量通常比数组更可取吗?
  • @JordanLejman 通常是的,因为他们处理自己的分配并且是动态的。在某些情况下需要使用 c 数组,但这种情况很少见。 STL 还有许多其他集合类型,您可能会觉得有用geeksforgeeks.org/the-c-standard-template-library-stl
  • 等等,二维向量是分开的?
  • @JordanLejman A vector 是一维的,但如果向量包含的类型是另一个向量(如std::vector&lt;std::vector&lt;double&gt;&gt;),则可以将其称为二维。
【解决方案2】:
int numCities = 10;
double cities[numCities][2];

不要让一对匿名的doubles 包含每个城市的xy 位置,而是为它创建一个class/struct。这将使以后扩展您的解决方案变得更加容易。如果您想存储城市名称及其位置,例如:

struct position_t {
    double x;
    double y;
};

struct city_t {
    position_t position;
    std::string name;
};

然后,考虑使用vector,而不是在数组中包含固定数量的城市,它可以在运行时动态增长和缩小:

std::vector<city_t> cities;

添加了一些辅助函数:

#include <cmath>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>

struct position_t {
    position_t(double X, double Y) : x(X), y(Y) {}

    double dist(const position_t& other) const {
        return std::sqrt(std::pow(x - other.x, 2.) + std::pow(y - other.y, 2.));
    }

    // a function to print the position
    friend std::ostream& operator<<(std::ostream&, const position_t&);

    double x;
    double y;
};

std::ostream& operator<<(std::ostream& os, const position_t& p) {
    return os << '{' << p.x << ',' << p.y << '}';
}

struct city_t {
    city_t(const position_t& p, const std::string_view& n) : position(p), name(n) {}

    double dist(const city_t& other) const {
        // this distance function just calls the function in the position_t
        return position.dist(other.position);
    }

    // a function to print the values of the city    
    friend std::ostream& operator<<(std::ostream&, const city_t&);

    position_t position;
    std::string name;
};

std::ostream& operator<<(std::ostream& os, const city_t& c) {
    return os << c.position << ' ' << c.name;
}

int main() {
    std::vector<city_t> cities = {{{10., 20.}, "Ankeborg"},
                                  {{11., 12.}, "Gothenburg"}};
    for(const auto& c : cities) {
        std::cout << c << "\n";
    }
    std::cout << "distance: " << cities[0].dist(cities[1]) << "\n";
}

输出:

{10,20} Ankeborg
{11,12} Gothenburg
distance: 8.06226

【讨论】:

    猜你喜欢
    • 2015-06-14
    • 2021-09-11
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多