Vector and classes

M

Michael

Hi,

I am new to C++ and have run into the following problem. I have written
some functions which return a vector e.g

vector<double> myfunction(double arg1,.......,.....)
{

}


I woud like to create a class out of this functions, however I dont
know how to write the header file correctly, when i use:

#ifndef MYCLASS_H
#define MYCLASS_H

vector<double> myfunction(double arg1,.......,.....);

#endif

I get the error the vector is not template. Would be great if somebody
could help.

Happy x-mas to everyone

Michael
 
G

Gianni Mariani

Michael said:
Hi,

I am new to C++ and have run into the following problem. I have written
some functions which return a vector e.g

vector<double> myfunction(double arg1,.......,.....)
{

}


I woud like to create a class out of this functions, however I dont
know how to write the header file correctly, when i use:

#ifndef MYCLASS_H
#define MYCLASS_H

#include said:
vector<double> myfunction(double arg1,.......,.....);

std::vector said:
#endif

I get the error the vector is not template. Would be great if somebody
could help.

Firstly you need to include <vector> and second you need to specify
which namespace. Since you're using it in a header, you should not do a
"using namespace std". My preference is to fully quality the namespace
on each use in the header.
 
M

Mirek Fidler

Michael said:
Hi,

I am new to C++ and have run into the following problem. I have written
some functions which return a vector e.g

vector<double> myfunction(double arg1,.......,.....)
{

}


I woud like to create a class out of this functions, however I dont
know how to write the header file correctly, when i use:

#ifndef MYCLASS_H
#define MYCLASS_H

vector<double> myfunction(double arg1,.......,.....);

std::vector<double> myfunction(double arg1,.......,.....);

Also note that this is quite fragile performance-wise, if RVO is not
used to optimize out the copy of vector (-> because of inferior
compiler or specific situation). Perhaps the better solution here is

myfunction(std::vector<double>& , ....);
 
B

Bo Persson

Mirek said:
std::vector<double> myfunction(double arg1,.......,.....);

Also note that this is quite fragile performance-wise, if RVO is
not used to optimize out the copy of vector (-> because of inferior
compiler or specific situation). Perhaps the better solution here is

myfunction(std::vector<double>& , ....);

Could be, but perhaps we should save that until we see that there is a
performance problem? Michael didn't say how many millions of doubles the
vector is expected to contain.

Until we know that, returning a vector is perfectly good, especially if it
makes the code easier to read and understand.



Bo Persson
 
R

Rolf Magnus

Gianni said:
Firstly you need to include <vector> and second you need to specify
which namespace. Since you're using it in a header, you should not do a
"using namespace std". My preference is to fully quality the namespace
on each use in the header.

Unfortunately, there are no typedef templates yet. Otherwise, I'd suggest to
typedef std::vector into an own namespace and then use that in all headers.
 
D

Daniel T.

Bo Persson said:
Could be, but perhaps we should save that until we see that there is
a performance problem? Michael didn't say how many millions of
doubles the vector is expected to contain.

Until we know that, returning a vector is perfectly good, especially
if it makes the code easier to read and understand.

I don't like premature optimization, but let's not prematurely pessimize
our program either. Because this particular issue requires an interface
change, it bears more scrutiny up front.

Mirek's suggestion not only is likely to perform better, it is also not
likely to perform any worse, and it is more idomatic, if a little C like.

Some other possible solutions:

const vector<double>& myfunction( /*params*/ ) {
static vector<double> result;
result.clear();
// fill result
return result;
}

The above has the benefit of being pratically a drop-in replacement for
the OPs function signature, but has the drawback of keeping a possible
extra copy of the result indefinitely.

Another solution which fits better with the rest of the library would be:

template < typename OutIt >
void myfunction( OutIt begin, /*params*/ );

This solution allows the caller to choose the container that best suits
his needs rather than having to use vector.

Another:

struct MyGenerator {
MyGenerator( /*params*/ );
double operator()(); // returns the next double in the list
};

The above can be used with either the generate or generate_n functions,
but would require the caller to know how many elements should exist.

Lastly, we can make an iterator:

class MySequence: public std::iterator< std::forward_iterator_tag,
double >
{
public:
MySequence(); // sentinel
explicit MySequence(/*params*/); // start the sequence

double operator*(); // return the current value in the sequence

MySequence& operator++(); // go to the next value in the sequence
MySequence operator++(int);

friend bool operator==(const MySequence& lhs, const MySequence& rhs);
// return true if the both objects point to the same element
// in the sequence.
};

bool operator!=(const primes& lhs, const primes& rhs) {
return !(lhs == rhs);
}

This can be used directly in other algorithms without having to ever
create a container holding all the elements. (I have, for example,
something like the above for generating primes and fibonacci numbers.)
But it may be too heavy weight for the OPs situation.
 
M

Mirek Fidler

Daniel said:
Some other possible solutions:

const vector<double>& myfunction( /*params*/ ) {
static vector<double> result;
result.clear();
// fill result
return result;
}

Beware of this soltution. Think e.g. about

add(myfunction(...), myfunction(...));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top