C++ 通过引用传递向量的typdef向量

C++ 通过引用传递向量的typdef向量,c++,vector,pass-by-reference,typedef,C++,Vector,Pass By Reference,Typedef,我试图通过引用传递一个向量。我已经定义了数据类型,在我看来,我得到的是一个副本,而不是一个引用。我在这里找不到任何有效的语法来执行我想要的操作。建议 #include <iostream> #include <fstream> #include <vector> #include <string> #include <cmath> #define __DEBUG__ using namespace std; //Define cu

我试图通过引用传递一个向量。我已经定义了数据类型,在我看来,我得到的是一个副本,而不是一个引用。我在这里找不到任何有效的语法来执行我想要的操作。建议

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

#define __DEBUG__

using namespace std;

//Define custom types and constants
typedef std::vector< std::vector<float> > points;
//Steup NAN
float NaN = 0.0/0.0;  //Should be compiler independent

//Function prototypes
void vectorFunction(float t0, float tf,  points data );

//Global constants
string outFilename = "plotData.dat";
int sampleIntervals = 10000; //Number of times to sample function.

int main()
{
    ofstream plotFile;
    plotFile.open(outFilename.c_str());

    points data;

    vectorFunction( 0, 1000, data );

#ifdef __DEBUG__
    //Debug printouts
    cout << data.size() << endl;
#endif

    plotFile.close();
    return 0;
}

void vectorFunction(float t0, float tf, points data )
{
    std::vector< float > point(4);
    float timeStep = (tf - t0)/float(sampleIntervals);
    int counter = floor(tf*timeStep);

    //Resize the points array once.

    for( int i = 0; i < counter; i++)
    {
        point[0] = timeStep*counter;
        point[1] = pow(point[0],2);
        point[2] = sin(point[0]);
        point[3] = -pow(point[0],2);
        data.push_back(point);
    }

#ifdef __DEBUG__
    //Debug printouts
    std::cout << "counter: " << counter
              << ", timeStep: " << timeStep
              << ", t0: " << t0
              << ", tf: " << tf << endl;
    std::cout << data.size() << std::endl; 
#endif

}

void tangentVectorFunction(float t0, float tf, points data)
{

}
#包括
#包括
#包括
#包括
#包括
#定义调试__
使用名称空间std;
//定义自定义类型和常量
typedef std::vector点;
//斯提普南
浮点数NaN=0.0/0.0//应该是独立于编译器的
//功能原型
无效向量函数(浮点t0、浮点tf、点数据);
//全局常数
字符串outFilename=“plotData.dat”;
int-sampleinterval=10000//对函数进行采样的次数。
int main()
{
流式绘图文件;
plotFile.open(outFilename.c_str());
点数据;
矢量函数(0,1000,数据);
#ifdef_uu调试__
//调试打印输出

cout假设您的typedef仍然存在:

typedef std::vector< std::vector<float> > points;
您的
类型只是一个值类型,相当于
std::vector
。对此类变量的赋值将生成一个副本。将其声明为引用类型
点&
(或
std::vector&
)将使用对原始变量的引用

它当然不会影响问题的范围,但是你可以简单地考虑使用一维向量。这样你就可以节省内存分配、释放和查找。你可以使用:

point_grid[width * MAX_HEIGHT + height] // instead of point_grid[width][height]

假设您的typedef仍然存在:

typedef std::vector< std::vector<float> > points;
您的
类型只是一个值类型,相当于
std::vector
。对此类变量的赋值将生成一个副本。将其声明为引用类型
点&
(或
std::vector&
)将使用对原始变量的引用

它当然不会影响问题的范围,但是你可以简单地考虑使用一维向量。这样你就可以节省内存分配、释放和查找。你可以使用:

point_grid[width * MAX_HEIGHT + height] // instead of point_grid[width][height]

+完整(但遗憾的是不是最小)示例程序为1。完整(但遗憾的是不是最小)示例程序见+1示例程序。看。这解决了我的问题。我将在切向向量函数中以一种奇怪的方式改变结构的尺寸,这就是为什么我不考虑尺寸。也许使用单一尺寸仍然有效。当我完成代码时,我将看看我是否可以进行这种优化。建议的优化如果必须更改维度,则会失去效率,因此最好保持原样,直到您确定它会起作用。这解决了我的问题。我将在切线向量函数中以一种奇怪的方式更改结构的维度,这就是为什么我保留维度自由。也许使用单个维度仍然有效k、 当我完成代码时,我将看看是否可以进行这种优化。如果必须更改维度,建议的优化将失去效率,因此最好保持原样,直到您确定它可以工作为止。