Thread: vector representation

  1. #1
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115

    vector representation

    Code:
    std :: vector <QString, QString, std :: vector <QString, std :: vector <QString>, QString> > configFileDataVector;
    This results in:
    Code:
    error: wrong number of template arguments (3, should be 2)
    
    /usr/include/c++/4.4/bits/stl_vector.h:170: error: provided for ‘template<class _Tp, class _Alloc> class std::vector’
    What I am missing here now, please point out, is it totally wrong to represent vectors this way?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, yes. Generally you only get to have one thing in your pointy brackets for a vector. You can make that type a compound type if you want, i.e., you can declare a class/struct that contains some strings, and then make a vector of those if that's what you need.

  3. #3
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by tabstop View Post
    Well, yes. Generally you only get to have one thing in your pointy brackets for a vector.
    Thanks for replying, but why did you mention Generally? Isn't it always?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, as the error message shows, you can provide a second template argument which is the memory allocator you wish to use (i.e., as a replacement to new). I've never used it, nor have I felt a need to, but it's there.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The question is: what are you trying to do here?
    Code:
    std :: vector
    <
    	QString,
    	QString,
    	std :: vector 
    	<
    		QString, 
    		std :: vector <QString>,
    		QString
    	>
    > configFileDataVector;
    This isn't right.
    A vector takes a type, and optionally, an allocator. You are passing it three types, of which the last two are not allocator types.
    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.

  6. #6
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by Elysia View Post
    A vector takes a type, and optionally, an allocator. You are passing it three types, of which the last two are not allocator types.
    I am thankful to you for the enlightenment :hattip: Actually recently I had used map and pair (they take multiple arguments) so I was trying to use vector that way!

    Thanks for the clarification tabstop.
    Last edited by AnishaKaul; 12-17-2010 at 11:33 AM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, obviously it doesn't make sense for vector to take two or more types, since it only stores one type. A map maps one type to another, so it makes sense to have two types.
    A vector is a dynamic array and an array is simply one type.
    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.

  8. #8
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    I was confused unnecessarily for no obvious reasons Thanks.

  9. #9
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Well, can the structure I showed here be represented with a combination of vectors, maps and pairs in any way?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by AnishaKaul View Post
    Well, can the structure I showed here be represented with a combination of vectors, maps and pairs in any way?
    You're assuming that there is such a thing as "the structure [shown] here", which there isn't. None of us have any idea what, exactly, you're looking for. A vector of three-component objects? A map from the first object to the other two? A triple? Something else?

  11. #11
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by tabstop View Post
    You're assuming that there is such a thing as "the structure [shown] here", which there isn't. None of us have any idea what, exactly, you're looking for. A vector of three-component objects? A map from the first object to the other two? A triple? Something else?
    Thanks for bothering

    One incomplete rough way of representing the structure shown in my first post is by using "structures":
    Code:
    struct x
    {
    	string name;
    	int id;
    };
    
    struct y
    {
    	string name;
    	int id;
    	vector <x> xVector;
    };
    
    class z
    {
           vector <y> yVector;
    }
    My question is: Can I avoid using structures somehow and put every thing in the class itself?

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by AnishaKaul View Post
    My question is: Can I avoid using structures somehow and put every thing in the class itself?
    No, realistically, it is not.

    "Avoiding structures" is, in itself, a ridiculous requirement.
    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.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by AnishaKaul View Post
    Thanks for bothering

    One incomplete rough way of representing the structure shown in my first post is by using "structures":
    Code:
    struct x
    {
    	string name;
    	int id;
    };
    
    struct y
    {
    	string name;
    	int id;
    	vector <x> xVector;
    };
    
    class z
    {
           vector <y> yVector;
    }
    My question is: Can I avoid using structures somehow and put every thing in the class itself?
    Yeah, that's actually a pretty complete and finished way of representing what you want to have happen. (I would never have gotten there from what you posted in the original post, though.)

Popular pages Recent additions subscribe to a feed