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.
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>.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
Last edited by whiteflags; 04-04-2008 at 02:40 AM.
>> 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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.