Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:24:56+00:00 2026-06-10T23:24:56+00:00

I am trying to pass a vector of vectors by reference. I have typdefed

  • 0

I am trying to pass a vector of vectors by reference. I have typdefed the datatype and it seems to me that I am getting a copy, not a reference. I couldn’t find figure out any valid syntax to do what I want here. Suggestions?

#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)
{

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:24:58+00:00Added an answer on June 10, 2026 at 11:24 pm

    Assuming your typedef remains:

    typedef std::vector< std::vector<float> > points;
    

    Your prototype for passing by reference would look like this:

    void vectorFunction(float t0, float tf, points& data);
    void tangentVectorFunction(float t0, float tf, points& data);
    

    Your points type is just a value type, and is equivalent to std::vector< std::vector<float> >. Assignment to such a variable makes a copy. Declaring it as a reference type points& (or std::vector< std::vector<float> >&) uses a reference to the original.

    It certainly doesn’t make a difference in the scope of your problem, but you might consider simply using a one dimensional vector. You save a little bit on memory allocations, deallocations, and lookups this way. You’d use:

    point_grid[width * MAX_HEIGHT + height] // instead of point_grid[width][height]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a main.cpp test.h and test.cpp> I am trying to pass my vector
Trying to pass something to something else. Says undefined. Not sure how or why.
I am trying to pass data around the numpy and boost::ublas layers. I have
While trying to experiment with copy construction by 'pass by value' and the followed
I'm trying to write a functor that I can pass to std::sort that will
I'm fairly new to C++ especially STL. I'm trying to pass a vector as
So I basically have a vector args with 1 argument per array that I'm
I'm trying to pass Vector of complex objects as a result of a WS
I am trying to find a way to pass derived-type objects with allocatable components
I am trying pass class as value in hashmap. I need to get the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.