vector representation

This is a discussion on vector representation within the C++ Programming forums, part of the General Programming Boards category; Code: std :: vector <QString, QString, std :: vector <QString, std :: vector <QString>, QString> > configFileDataVector; This results in: ...

  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,185
    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,185
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    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 10:33 AM.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    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,185
    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,255
    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%.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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

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