Thread: Using C++ Standard library to generate custom Vectors?

  1. #1
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    London, United Kingdom
    Posts
    226

    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
    28,412
    You would create a std::vector<coord>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    London, United Kingdom
    Posts
    226
    Oh, I thought this could only be done for the primitives. Thanks a lot!

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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
    London, United Kingdom
    Posts
    226
    and this can be done for Classes also, right?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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 02:40 AM.

  7. #7
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    London, United Kingdom
    Posts
    226
    Okay. thanks a lot.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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, 01:03 AM
  2. Problem with custom shared library
    By soothsayer in forum C Programming
    Replies: 3
    Last Post: 05-24-2006, 12:48 PM
  3. Vectors and custom classes
    By cunnus88 in forum C++ Programming
    Replies: 16
    Last Post: 05-12-2006, 05: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, 04:58 AM