将这段C语言风格的代码转化为C++风格,需要帮助。

4

嘿,我习惯使用C开发,现在想在一个项目中使用C++。有人能给我一个例子,说明如何将这个C样式的代码转换成C++代码吗?我知道它应该可以在C ++编译器中编译,但我想使用C ++技术(例如类,RAII)。

struct Solution {
        double x[30];
        int itt_found;
        double value;
};
Solution *NewSolution() {
        Solution *S = (Solution *)malloc(sizeof(Solution));
        for (int i=0;<=30;i++) {
                S->x[i] = 0;
        }
        S->itt_found = -1;
        return S;
}
void FreeSolution(Solution *S) {
        if (S != NULL) free(S);
}
int main() {
        Solution *S = NewSolution();
        S->value = Evaluate(S->x);// Evaluate是另一个返回double类型的函数
        S->itt_found = 0;
        FreeSolution(S);
        return EXIT_SUCCESS;
}

理想情况下,我想在main函数中做这样的事情,但我不确定如何创建类,我读了很多东西,但正确地将它们结合在一起似乎有点困难。

class Solution {
public:
    Solution(int size) {
        for (int i = 0; i < size; i++) {
            x[i] = 0;
        }
        itt_found = -1;
    }
    void Evaluate() {
        value = eval(x);
    }
    double value;
private:
    double x[30];
    int itt_found;
};
int main() {
    Solution S(30);
    S.Evaluate();
    cout << S.value << endl;
    return EXIT_SUCCESS;
}

如果需要更多信息,请问我,谢谢。

编辑:将eval更改为Evaluate,因为我认为eval是保留字,或者至少很令人困惑。在我的实际代码中,我有一个函数指针数组,我使用它来评估X数组并将结果存储在value中。我认为包含它们只会导致不必要的冗余,并且会遮盖我的问题。

3个回答

4
这是一种实现的方法:

这是一个示例:

class Solution
{
public:
    // This is the constructor for this class. Note that the constructor
    // must always have the same name as the class. The line following
    // the ':' is called the initializer list. It initializes the data
    // members to a known state.
    Solution(size_t size) : x(size, 0.0), found(false), value(0.0)
    {
        // The 'x(size, 0.0)' is a call to a constructor for the
        // std::vector class. It creates an array of a size equal to
        // the first argument, and initializes each element to whatever's
        // supplied in the second argument (0.0 in this case).
    }

    int Evaluate()
    {
        // x.begin() returns an iterator that points to the first element
        // in the array.
        value = eval(x.begin());
        found = true;
        return EXIT_SUCCESS;
    }

    // The 'const' means that this function won't change any (non-mutable)
    // variables in the class.
    double GetValue() const
    {
        return value;
    }

    bool FoundValue() const
    {
        return found;
    }

    // You may add some more functions to allow users to access/manipulate
    // the array, if needed.

    // It is a good idea to keep data members private.
private:
    // Use a vector for resizable arrays.
    std::vector<double> x;
    bool found;
    double value;
};

int main()
{
    Solution s(30); // The constructor will be called here.
    s.Evaluate();
    cout << S.GetValue() << endl;
}

谢谢,这非常接近我想要的。我最困惑的是初始化列表,你已经解释清楚了。 - Flamewires

3

C++中动态数组最简单的实现方式是使用std::vector,因为它可以帮助管理内存的分配和释放。例如,你可以像这样定义一个类:

#include <vector>

struct Solution
{
    Solution(unsigned n) : x(n) { }
    std::vector<double> x;
};

这将为您提供一个类,它具有一个构造函数,该构造函数以其参数作为元素数量来创建双精度数组,我们使用向量表示,并使用n个元素初始化向量,所有元素都将设置为零。
这应该至少让您开始了解; 我没有真正了解您想要使用eval函数做什么。

0

你不一定需要在C++中使用类(与Java相比)。你可以通过STL和自由函数来实现。大致如下:

#include <algorithm>
#include <iostream>
#include <vector>

/// Dynamic "array" of doubles
typedef std::vector<double> dvec;

/// Free function crunches given numbers
double eval_vector( const dvec& d )
{
    // example: computes the sum
    return std::accumulate( d.begin(), d.end(), 0 );
}

/// entry
int main()
{
    dvec v( 30 ); // creates vector of 30 default-initialized doubles

    std::cout << "result: " << eval_dvec( v ) << std::endl;

    return 0;
}

原始代码中的 itt_found 有什么作用?你的 eval() 如何知道数组中有多少项?


不,它实际上是我正在编写的一大段代码的一部分,评估具有不同参数的遗传算法的性能,itt found将用于存储找到此解决方案的迭代,以便我可以进行一些图形和分析,这也是我想要将解决方案保留为对象的原因,以便我可以添加更多方法等。谢谢!但我可能会制作一个解决方案向量。 - Flamewires

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接