954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Passing column of 2d vector to function as a reference

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.

kamos.jura
Newbie Poster
5 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

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 is not compatible with vector, and vector is not compatible with vector. 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.

Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 

just pass 2d vector and use only its column?

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 
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.

Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 
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?

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

Tom Gunn - unfortunatelly seems you are right. I tried to search on Google, but only helpful thing I found about type casting of vectors is this:
http://bytes.com/topic/c/answers/133936-convert-vector-float-vector-double
and it doesn't work for my case.

Anyway thanks for your help.

J.

kamos.jura
Newbie Poster
5 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You