Hi,
I have recently learnt how to use C++ vectors. I need to use a vector for the following structure (using the readily-available vector library):
Code:struct coord {
double x;
double y;
double z;
};
Thanks a lot in advance.
Printable View
Hi,
I have recently learnt how to use C++ vectors. I need to use a vector for the following structure (using the readily-available vector library):
Code:struct coord {
double x;
double y;
double z;
};
Thanks a lot in advance.
You would create a std::vector<coord>.
Oh, I thought this could only be done for the primitives. Thanks a lot!
and this can be done for Classes also, right?
Well the STL and templates in general were made to support classes. Research demonstrates:
[snark]
[/snark]Code:#include <iostream>
#include <memory>
class Maybe
{
public:
Maybe ( ): m_works( true ) {}
bool isWorking( ) const { return m_works; }
private:
const bool m_works;
};
int main ( )
{
std::auto_ptr<Maybe> instance( new Maybe );
std::cout << ( instance->isWorking() ? "Yes." : "No." ) << std::endl;
}
What is key though is how they work, and if the STL is operating the way you need it to. If it isn't, by all means use something else.
Okay. thanks a lot.
>> and this can be done for Classes also, right?
Note that classes and structs are virtually the same in C++. The only difference is public versus private default access. citizen's Maybe class could be a struct without any other changes, and your coord struct would be the same as a class if you specify public: inside it.
So, for example:andCode:struct coord {
double x;
double y;
double z;
};
are the same thing.Code:class coord {
public:
double x;
double y;
double z;
};
Also
andCode:struct blah
{
private:
int y;
public:
int x;
void foo();
are identical.Code:class blah
{
int y;
public:
int x;
void foo();
--
Mats