Using C++ Standard library to generate custom Vectors?

This is a discussion on Using C++ Standard library to generate custom Vectors? within the C++ Programming forums, part of the General Programming Boards category; Hi, I have recently learnt how to use C++ vectors. I need to use a vector for the following structure ...

  1. #1
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    Munich, Germany
    Posts
    224

    Using C++ Standard library to generate custom Vectors?

    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    You would create a std::vector<coord>.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    Munich, Germany
    Posts
    224
    Oh, I thought this could only be done for the primitives. Thanks a lot!

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Quote Originally Posted by Zeeshan View Post
    Oh, I thought this could only be done for the primitives. Thanks a lot!
    lol, vectors would be quite useless mate! Too bad the author of your book wasn't a little inventive.

  5. #5
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    Munich, Germany
    Posts
    224
    and this can be done for Classes also, right?

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Well the STL and templates in general were made to support classes. Research demonstrates:
    [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;
    }
    [/snark]

    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 03:40 AM.

  7. #7
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    Munich, Germany
    Posts
    224
    Okay. thanks a lot.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> 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.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Daved View Post
    >> 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:
    Code:
    struct coord {
     double x;
    double y;
    double z;
    };
    and
    Code:
    class coord {
    public:
     double x;
    double y;
    double z;
    };
    are the same thing.

    Also
    Code:
    struct blah
    {
    private:
       int y;
    public:
       int x;
       void foo();
    and
    Code:
    class blah
    {
       int y;
    public:
       int x;
       void foo();
    are identical.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom Console Library
    By asbo60 in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2008, 02:03 AM
  2. Problem with custom shared library
    By soothsayer in forum C Programming
    Replies: 3
    Last Post: 05-24-2006, 01:48 PM
  3. Vectors and custom classes
    By cunnus88 in forum C++ Programming
    Replies: 16
    Last Post: 05-12-2006, 06:11 AM
  4. How To use vectors for custom classes
    By johnnyd in forum C++ Programming
    Replies: 14
    Last Post: 03-25-2003, 10:04 PM
  5. custom library
    By lambs4 in forum C Programming
    Replies: 1
    Last Post: 06-09-2002, 05:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21