仅通过代码中的某些函数传递向量 我在C++中的虚空环境函数中创建了一个向量调用核和胞元。然后我调用virus函数,它创建一个病毒点向量。然后我想访问病毒调用的另一个函数中的载体细胞核和细胞。是否可以调用病毒而不必通过病毒功能?如果不是,最好的方法是什么?附件是我的密码。提前谢谢。而且我没有在函数中添加所有代码 struct point { float x; float y; }; struct space{ point point1; point point2; point point3; point point4; }; int gelato(vector<point> & virus,int& times,int& iv, ofstream& data_file) { int a; int b; bool nc,cc,cmc; for(int i =0; i<virus.size(); i++) { a = virus[i].x; b = virus[i].y; nc = nucleus_check(nucleus,a,b); // **Need to call vector cell and nucleus** cc = cytoplasm_check(cell,a,b); cmc = cell_membrane_check(cell,a,b); } } int moves(char n) { int moved; int trial; if( n =='A') { moved = rand()%4; } else if(n=='B') { moved= rand()%3+1; } else if(n=='C') { trial= rand()%4; if(trial ==1) { moves('C'); } else { moved = trial; } } else if(n =='D') { trial = rand()%4; if(trial == 2) { moves('D'); } else { moved = trial; } } else if(n=='E') { moved = rand()%3; } return moved; } int v_move(vector<point>& virus, int& iv, ofstream& data_file) { gelato(virus,times,iv,data_file); } int rand_in(char a) {} void virus( ofstream& data_file) { v_move(virus, iv, data_file); } void cell_space(int r) { vector<point>cell; } void nucleus_space(int r) { vector<point>nucleus; } void environment() //**Where the vector nucleus and cell are created** { //cell cell_space(16) //nucleus nucleus_space(4); //cout<<"Environment"<<endl; } int main() { srand(time(NULL)); data_file.open("data.csv"); environment(); virus(data_file); return 0; } 结构点{ 浮动x; 浮动y; }; 结构空间{ 第1点; 第2点; 第3点; 第4点; }; int gelato(矢量和病毒、int和时间、int和iv、流和数据文件) { INTA; int b; bool nc、cc、cmc; 对于(int i=0;i

仅通过代码中的某些函数传递向量 我在C++中的虚空环境函数中创建了一个向量调用核和胞元。然后我调用virus函数,它创建一个病毒点向量。然后我想访问病毒调用的另一个函数中的载体细胞核和细胞。是否可以调用病毒而不必通过病毒功能?如果不是,最好的方法是什么?附件是我的密码。提前谢谢。而且我没有在函数中添加所有代码 struct point { float x; float y; }; struct space{ point point1; point point2; point point3; point point4; }; int gelato(vector<point> & virus,int& times,int& iv, ofstream& data_file) { int a; int b; bool nc,cc,cmc; for(int i =0; i<virus.size(); i++) { a = virus[i].x; b = virus[i].y; nc = nucleus_check(nucleus,a,b); // **Need to call vector cell and nucleus** cc = cytoplasm_check(cell,a,b); cmc = cell_membrane_check(cell,a,b); } } int moves(char n) { int moved; int trial; if( n =='A') { moved = rand()%4; } else if(n=='B') { moved= rand()%3+1; } else if(n=='C') { trial= rand()%4; if(trial ==1) { moves('C'); } else { moved = trial; } } else if(n =='D') { trial = rand()%4; if(trial == 2) { moves('D'); } else { moved = trial; } } else if(n=='E') { moved = rand()%3; } return moved; } int v_move(vector<point>& virus, int& iv, ofstream& data_file) { gelato(virus,times,iv,data_file); } int rand_in(char a) {} void virus( ofstream& data_file) { v_move(virus, iv, data_file); } void cell_space(int r) { vector<point>cell; } void nucleus_space(int r) { vector<point>nucleus; } void environment() //**Where the vector nucleus and cell are created** { //cell cell_space(16) //nucleus nucleus_space(4); //cout<<"Environment"<<endl; } int main() { srand(time(NULL)); data_file.open("data.csv"); environment(); virus(data_file); return 0; } 结构点{ 浮动x; 浮动y; }; 结构空间{ 第1点; 第2点; 第3点; 第4点; }; int gelato(矢量和病毒、int和时间、int和iv、流和数据文件) { INTA; int b; bool nc、cc、cmc; 对于(int i=0;i,c++,function,vector,C++,Function,Vector,),我假设您还没有学习/掌握面向对象编程(OOP),并且希望使用它实现程序。这很好,因为C++支持几种范例,并且不强迫您使用OOP。 由于“单元空间”和“核心空间”是作为一个实体一起使用的,您可以称之为“环境”,因此您应该定义一个结合了这两个元素的环境结构: struct environment { vector<point> cells; vector<point> nuclei; }; 在过程编程范例中,数据与对该数据进行操作的过程是分开的。数据作为

),我假设您还没有学习/掌握面向对象编程(OOP),并且希望使用它实现程序。这很好,因为C++支持几种范例,并且不强迫您使用OOP。 由于“单元空间”和“核心空间”是作为一个实体一起使用的,您可以称之为“环境”,因此您应该定义一个结合了这两个元素的
环境
结构:

struct environment
{
    vector<point> cells;
    vector<point> nuclei;
};
在过程编程范例中,数据与对该数据进行操作的过程是分开的。数据作为参数沿着辅助函数链不断传递是正常的。抵制将数据存储在全局变量中并让函数直接访问se全局变量。全局变量导致代码更难测试、更难读取和更难重用

您应该在程序执行的动作之后命名程序。名称中应该有某种动作动词,例如
\u environment
。如果程序对某种数据进行操作,则该数据的名称将成为该动词的对象,例如
prepare\u environment

相反地,您应该以结构所建模的实体命名结构。名称中应该有一个名词,例如
environment

一旦您理解了过程编程的原则,我想您会发现理解面向对象编程更容易

void prepare_environment(environment& env, // environment passed by reference
                         int cellCount, int nucleusCount)
{
    prepare_cells(env.cells, cellCount);
    prepare_nuclei(env.nuclei, nucleusCount);
};

void prepare_cells(std::vector<point>& cells, // cell vector passed by reference
                   int cellCount)
{
    cells.resize(cellCount);
    // Do other stuff to initialize cells
}

void prepare_nuclei(std::vector<point>& nuclei, int cellCount) {...}
int main()
{
    Environment env;

    prepare_environment(env, 16, 4);
    move_virusus(env);
}