Hi!
Can you tell me, how to pass a reference to the column in 2d vector to function with following declaration: void SetData( const std::vector<float> &xs,const std::vector<float> &ys); This function is from wxMathPlot.

My vector's declaration looks like this:

vector<vector <double> > sekce(8);
vector<vector<time_t> > cas_t_all(8);

I would like to do something like this(doesn't work): plot->SetData( &(float)cas_t_all[1], &sekce[5]); = pass second column of vector cas_t_all and fifth column of vector sekce to funkcion SetData(...). Issue here is also type casting - this I can avoid by my working code - see lower.

Current situation is that I have to copy whole column from vector cas_t_all to new vector xs and column from vector sekce to new vector ys:

std::vector<float> xs,ys;
for ( int i = 0; i < sekce[0].size(); i++ )
        xs.push_back(cas_t_all[0][i]);
ys.assign(sekce[0].begin(),sekce[0].end());
plot->SetData( xs, ys);

Thanks in advance.

J.

Recommended Answers

All 5 Replies

Normally you just pass the object and the compiler does the rest. No special syntax is needed:

#include <iostream>
#include <vector>

void PrintAll(std::vector<int> const& v)
{
    std::vector<int>::const_iterator x = v.begin();

    while (x != v.end()) std::cout << *x++ << '\t';

    std::cout << '\n';
}

int main()
{
    std::vector<std::vector<int> > v;

    for (int x = 0; x < 3; ++x)
    {
        v.push_back(std::vector<int>());

        for (int y = 0; y < 5; ++y)  v.back().push_back(y);
    }

    for (int x = 0; x < 3; ++x) PrintAll(v[x]);
}

The problem you have is incompatible types. vector<double> is not compatible with vector<float>, and vector<double> is not compatible with vector<time_t>. There is no way around copying the values into a new vector with whatever explicit or implicit conversions are needed. Your working code is the answer if you cannot change the types of the vectors to match.

just pass 2d vector and use only its column?

just pass 2d vector and use only its column?

The function being called is from wxMathPlot, and wxMathPlot is a third party library. It is a good assumption that he either does not have the code to change, or rightfully thinks that changing the code is a really bad idea.

The function being called is from wxMathPlot, and wxMathPlot is a third party library. It is a good assumption that he either does not have the code to change, or rightfully thinks that changing the code is a really bad idea.

Should have read the rest of his comment/questions, if it even says
it there. Didn't know there was a 3rd party. In which I wasn't invited?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.