关于如何通过引用将向量传递给构造函数的查询 我目前正在学习C++,并试图编写代码来查找函数的最小值。我已经将我的起点放在向量p0,p1,p2中,并将这些点集存储在一个我称之为pmatrix的向量中

关于如何通过引用将向量传递给构造函数的查询 我目前正在学习C++,并试图编写代码来查找函数的最小值。我已经将我的起点放在向量p0,p1,p2中,并将这些点集存储在一个我称之为pmatrix的向量中,c++,C++,在创建simplex类的对象时,我试图将此pmatrix作为参数传递。当我写下面的代码时,我希望我已经通过引用将pmatrix正确地传递到构造函数中,因为没有出现错误 但是,在调用更改了pmatrix的evaluate方法、打印了类定义内外的值之后,它在主范围内没有更改,并且存在两个单独的pmatrix副本。如何确保方法对pmatrix所做的更改作用于我在主范围中定义的更改 #include <iostream> #include <vector> class Sim

在创建simplex类的对象时,我试图将此
pmatrix
作为参数传递。当我写下面的代码时,我希望我已经通过引用将
pmatrix
正确地传递到构造函数中,因为没有出现错误

但是,在调用更改了
pmatrix
的evaluate方法、打印了类定义内外的值之后,它在主范围内没有更改,并且存在两个单独的
pmatrix
副本。如何确保方法对
pmatrix
所做的更改作用于我在主范围中定义的更改


#include <iostream>
#include <vector>

class Simplex {
    std::vector<std::vector<double>> pmatrix;
    double (*F)(double x_0,double x_1);

    public:
        Simplex(std::vector<std::vector<double>> &pmatrix,double (*func)(double,double)){
            this->pmatrix = pmatrix;
            F=func;
        }

        void evaluate(){
           for (int i=0; i<pmatrix.size();i++){
               std::vector<double> p_i;
               p_i=pmatrix[i];
               p_i[p_i.size()-1]=F(p_i[0],p_i[1]);
               pmatrix[i]=p_i;


            }
        }
        void test(){
            std::cout<<"pmatrix[i][2] elements inside the class def is "<<pmatrix[0][2]<<" "<<pmatrix[1][2]<<" "<<pmatrix[2][2]<<"\n";




        }

};
//defining rosenbrocks parabolic valley
double rpv (double x1, double x0){
    return 100*(x0-(x1*x1))*(x0-(x1*x1))+(1-x0)*(1-x0);
}
int main()
{
    std::vector<double> p0 = {0,0,1};
    std::vector<double> p1 = {2,0,2};
    std::vector<double> p2 = {0,2,3};
    std::vector<std::vector<double>> pmatrix = {p0,p1,p2};

    Simplex simplex(pmatrix,rpv);
    simplex.evaluate();

    std::cout<<"pmatrix[i][2] elements from pmatrix in main "<<pmatrix[0][2]<<" "<<pmatrix[1][2]<<" "<<pmatrix[2][2]<<"\n";
    simplex.test();



return 1;
}

Simplex::pmatrix
作为引用,并在构造函数的成员初始值设定项列表中初始化它。引用需要在构造时被分配它们的referent,所以当你在构造器的主体中时已经太晚了;所有成员都已默认构造

这就是成员初始值设定项列表的作用。你应该养成使用它的习惯,即使在这种情况下不是被迫的

class Simplex {
    std::vector<std::vector<double>>& pmatrix; // <-- this is a ref now
    double (*F)(double x_0,double x_1);

    public:
        Simplex(std::vector<std::vector<double>> &otherpmatrix,
                double (*func)(double,double))
        : pmatrix(otherpmatrix)
        , F(func)
        { }

    // other various Simplex things...
}
类单纯形{

std::vector&pmatrix;//这实际上取决于谁是
pmatrix
所有者。如果它只使用
pmatrix
,我会使用:

std::vector&pmatrix;

并将其作为引用传递到ctor中。还可以将其保留为指针,这意味着它可以是
nullptr
,而我们不拥有它

如果没有一个true所有者,并且应该将其共享,则应使用
std::shared\u ptr
,这意味着在
Simplex
中使用时不会将其销毁:

struct Point
{
    double x;
    double y;
    double z;
};

using PointList = std::vector<Point>;

using FunctionPtr = double(*)(double, double);

class Simplex 
{
    std::shared_ptr<PointList> pmatrix;
    FunctionPtr F; // find a better name for it!

public:
    Simplex(const std::shared_ptr<PointList>& otherpmatrix,
            FunctionPtr func) : 
        pmatrix(otherpmatrix), 
        F(func)
    {
    }
};

double rpv (double x1, double x0){
    return 100*(x0-(x1*x1))*(x0-(x1*x1))+(1-x0)*(1-x0);
}

int main()
{
    auto pmatrix = std::shared_ptr<PointList>(new PointList());
    pmatrix->push_back({0,0,1});
    pmatrix->push_back({2,0,2});
    pmatrix->push_back({0,2,3});

    Simplex simplex(pmatrix, &rpv);
}
结构点
{
双x;
双y;
双z;
};
使用PointList=std::vector;
使用函数ptr=double(*)(double,double);
类单纯形
{
std::共享_ptr pmatrix;
FunctionPtr F;//为它找到一个更好的名称!
公众:
单工(const std::shared_ptr和otherp矩阵,
功能(PTR func):
pmatrix(其他pmatrix),
F(职能)
{
}
};
双rpv(双x1,双x0){
返回100*(x0-(x1*x1))*(x0-(x1*x1))+(1-x0)*(1-x0);
}
int main()
{
auto pmatrix=std::shared_ptr(new PointList());
p矩阵->推回({0,0,1});
p矩阵->推回({2,0,2});
p矩阵->推回({0,2,3});
单工单工(pmatrix和rpv);
}

这表示你拥有这个对象的共享所有权。

与你的问题无关:如果你的点有3个坐标,那么考虑创建一个<代码>结构点{int x,y,z;}。
然后拥有
std::vector
。除了可读性之外,这样做还有什么好处吗?@V.Jain可读性是代码的一个重要部分。保持它的简单性,并且每当您可以将这些东西组合到一个结构中时,这样做是一个好主意。@V.Jain还可以使用以下语句:
使用PointsMatrix=std::vector
让它更整洁!@Kerek我不建议这样做。为什么
是Point.push_back()
?这没有意义。你能澄清一下你的语法吗?我试图按照你的建议更改我的simplex类,更改行:
std::vector pmatrix;
std::vector&pmatrix;
simplex(std::vector pmatrix,double)(*func)(双精度,双精度)
单纯形(标准::向量和其他p矩阵,双精度(*func)(双精度,双精度))
this->pmatrix=pmatrix;
this->pmatrix=otherpmatrix;
。这给了我一个错误。我很感激这条评论很难阅读,如果你能编辑你的答案,以便我可以编译有错误的代码,我将不胜感激。你有什么错误?请注意,我使用的是初始化列表(介于
{
在函数参数之后。名称更改为
otherpmatrix
只是为了让成员变量和参数在我的脑海中分开。我不喜欢函数参数与成员变量同名。如果愿意,可以将其保留为
pmatrix
。因为只有引用才能引用另一个变量le.您的
pmatrix
最初不是一个引用。它是一个
pmatrix
。a
pmatrix
不能引用
pmatrix
。只有
pmatrix&
可以引用
pmatrix
。另一个选项是让您的
Simplex
负责构建
pmatrix
,然后您就可以了可以在main中提取对它的引用。但是原始文件需要在某个地方。它们不能都是
pmatrix
,也不能都是
pmatrix&
。什么错误?
struct Point
{
    double x;
    double y;
    double z;
};

using PointList = std::vector<Point>;

using FunctionPtr = double(*)(double, double);

class Simplex 
{
    std::shared_ptr<PointList> pmatrix;
    FunctionPtr F; // find a better name for it!

public:
    Simplex(const std::shared_ptr<PointList>& otherpmatrix,
            FunctionPtr func) : 
        pmatrix(otherpmatrix), 
        F(func)
    {
    }
};

double rpv (double x1, double x0){
    return 100*(x0-(x1*x1))*(x0-(x1*x1))+(1-x0)*(1-x0);
}

int main()
{
    auto pmatrix = std::shared_ptr<PointList>(new PointList());
    pmatrix->push_back({0,0,1});
    pmatrix->push_back({2,0,2});
    pmatrix->push_back({0,2,3});

    Simplex simplex(pmatrix, &rpv);
}