Thread: Choosing the right container

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Choosing the right container

    Some Questions :
    1. Is there a STL container that holds data in 2 dimensions(I need features like removing or replacing a specific row or column)?
    2.When I do not need the flexibility of generic types..(i.e It only needs to hold objects of a specific class )..Is it better to design the container myself..to avoid template code ?
    3. If I use a
    Code:
    list<list<token>>
    ...... token being my class....would I be able to interchange rows with columns....i.e. find the transpose of a matrix....?.. without putting the data into a new one and returning it ?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) No. But you can still put, vectors inside vectors and lists inside lists to make it 2 dimensional.
    2) No. There is no reason to avoid template types at all, unless you can prove it really hurts performance.
    3) Doubtful. List was not made for that purpose.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by manasij7479 View Post
    Some Questions :
    1. Is there a STL container that holds data in 2 dimensions(I need features like removing or replacing a specific row or column)?
    Not specifically, no. A std::vector<std::vector<your_type> > will often suffice. The utility of this versus other approaches doesn't depend much on particular functions such as removing or replacing rows/columns - such things are not difficult to implement. Your decision to use a standard container versus some other approach tends to depend on other design criteria, such as performance against some defined metric. Since you haven't articulated any relevant metrics, you have no reason to prefer any type of container over another.

    In practice, if you are worrying about swapping rows and columns, you need ability to access random elements of the container. The std::vector<> is designed to support random access of individual elements of a one-dimensional vector in constant time (as long as you don't resize the vector too often). A std::vector<std:vector<whatever> > will also support random access, albeit in two-dimension.

    If you are doing lots of resizing of your containers (adding and removing elements) you might be better off using std::list (or another standard container) rather than std::vector. Normally, however, if you are interested in swapping rows and columns of a two-dimensional data structure, it is generally implied you will not be resizing the container all that often.

    Quote Originally Posted by manasij7479 View Post
    2.When I do not need the flexibility of generic types..(i.e It only needs to hold objects of a specific class )..Is it better to design the container myself..to avoid template code ?
    Not unless you have identified some objective measure against which standard containers are deficient for your program. Which you have not done.

    There will rarely be any advantage, and often a lot of disadvantage, in you writing a SomeObject_Vector of your own and using it instead of a std::vector<SomeObject>.

    Yes, the standard containers are template types. However, when you use a standard container, you are working with a container of objects of a specific type. A std::vector<int> is a completely different type of container from a std::vector<double>. Both are distinct, concrete, types of container.

    Generally speaking, designing useful containers (regardless of what type of object you want to store in them) is a difficult job. As a general answer, you are better off using containers that someone else has expended time and effort to design and implement. The standard containers (vector, list, etc) will tend to behave more reliably, and often offer better performance, than containers you might create to give you comparable functionality. Precisely because they are designed and optimised by professionals.


    Quote Originally Posted by manasij7479 View Post
    3. If I use a
    Code:
    list<list<token>>
    ...... token being my class....would I be able to interchange rows with columns....i.e. find the transpose of a matrix....?.. without putting the data into a new one and returning it ?
    You're essentially seeking to transpose a two-dimensional data structure in place.

    Is it possible to transpose a list<list<token> > in place? Yes.

    Is it a good idea? No. For the reasons I spelled out above.

    If the number of rows and columns are not equal (representing a rectangular matrix rather than a square one) then it is almost certainly easier to create a new container with the transposed results than to transpose in place. That is not specific to list<list<token> > though - most multi-dimensional containers require some significant runtime book-keeping logic in order to perform transposition operations in-place for rectangular matrices. For a list<list<token> > that will certainly be true.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    In case of swapping rows and columns: eg: in the vector<vector<token>>
    1 2 3
    4 5 6
    7 8 9
    TO
    4 5 6
    1 2 3
    7 8 9
    OR
    2 1 3
    5 4 6
    8 7 9
    One interchanges rows and the other changes columns.... but in a specific case using a
    vector<vector<token>> ....I'd be limited to only one of those..(I'd be really glad if you can show me an example
    of both in the same case )..
    Last edited by manasij7479; 04-10-2011 at 02:40 AM. Reason: Got Messy!

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Why don't you have a go, and show us what you have tried?

    I'll give you a hint: you need to be specific about what constitutes a "row" in a std::vector<std::vector<token> > and what constitutes a column.

    Once you've done that, swapping two rows or swapping two columns is trivial. However, if you can't unambiguously describe what a row or a column is, you will never be able to swap them.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Here is the basic framework... my token class is working without problems
    Code:
    #include "../Token/token.h"
    #include<list>
    using std::list;
    class token;
    typedef list<token> row;
    class matrix
    {
        public:
        ///Constructors
        //Copy Constructor here
        matrix(int x,int y)
        {
            r=x;
            c=y;
            //make l of appropriate size //Can't figure out how..
        };//forming an empty one
        
        int r; //row
        int c; //col
        list<row> l;
        
        ///OPerators
        
        ///IO
        //void input(void); //Would prompt user for info..
        //void input(token**);
        
        /// Other Functions
    };
    In the above code, how do I allocate l ("L"<-list<list<token>>)of a particular size
    from the given row and column data?...after it is done..can it be specified such that the individual sizes of the rows must remain constant...and changing one changes all of them ?

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by manasij7479 View Post
    In the above code, how do I allocate l ("L"<-list<list<token>>)of a particular size
    std::list supports a resize() member function. In a list<list<whatever> > you will need to use resize on both the outer list (the list of lists) and each of the individual lists it contains.

    Quote Originally Posted by manasij7479 View Post
    from the given row and column data?...after it is done..can it be specified such that the individual sizes of the rows must remain constant...and changing one changes all of them ?
    If you want that behaviour, you need to write code to achieve it. In your matrix class, that might be done within functions that set number of rows or number of columns (or both).

    It is not something you can do with a single call to any of list's member functions.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help choosing a C++ Book?
    By Nukem in forum C++ Programming
    Replies: 1
    Last Post: 07-04-2010, 07:14 PM
  2. Choosing a GPS
    By laserlight in forum General Discussions
    Replies: 14
    Last Post: 11-20-2009, 11:12 AM
  3. Replies: 1
    Last Post: 01-23-2006, 07:12 PM
  4. Replies: 4
    Last Post: 03-21-2004, 03:34 PM
  5. Choosing a compiler
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 06-10-2002, 04:00 PM

Tags for this Thread