Thread: maximum objects in vector

  1. #1
    Shadow12345
    Guest

    maximum objects in vector

    I have a SensorVector that holds objects of the type sensor. I was wondering if you can set a maximum amount in a vector so that you can only push back a certain amount.

    typedef vector<sensor*> SV;
    SV SensorVector;
    SensorVector.push_back(new sensor);

    ya...

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    There is a max_size() function of vector that returns the maximum size allowable of a vector. I highly doubt you've reached it though.

    Code:
    #include <vector>
    #include <iostream>
    
    class fairlyLarge {
    	char chunk[100];
    };
    
    int main() {
    	std::cout << std::vector<int>().max_size()         << std::endl;
    	std::cout << std::vector<double>().max_size()      << std::endl;
    	std::cout << std::vector<fairlyLarge>().max_size() << std::endl;
    	return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Shadow12345
    Guest
    Hmm, it looks as though the max_size() function shows the maximum amount of bytes that you can have in a vector. I meant is there any way to set the maximum number of objects in a vector, for instance having a vector that can only hold 10 sensor objects and no more.

    I was also wondering why you use the scope operator instead of
    using namespace std;
    Last edited by Shadow12345; 07-21-2002 at 11:50 AM.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    max_size holds the number of objects you can have in the largest possible vector. You can't artificially limit the size of the vector with just it's interface. However, you can simply resize it to whatever size you want and then simply not use push_back, resize, or insert. If you really want something fixed size, you could just use an ordinary array, but I don't see the benefit in doing such.

    As for prefixing everything with std rather than using namespace std, it just keeps everything from from being brought into the global namespace (essentially defeating namespaces in the first place). I agree it would simply be dumb to use a name of something in the standard library just in a different namespace, but regardless, keeping the using declarations as local as possible (or not having them at all), is a good idea IMO.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    You may want to try using the size() function and test for any "size" greater than, or equal to, your preferred limit.

    Now, there's also a reserve() function that will allow the allocation of memory space in the vector for, let's say, ten elements. This will not put a limit on the size of the vector (determined by max_size as SilentStrike points out), but will avoid the need for reallocating space as the vector grows to a predetermined size. This simply guarantees that your vector will have space for, at least, a given number of elements without having to allocate more space.

    It's unlikely that this would be necessary, but could play a role if your objects were very large and you wanted to avoid the potential cost in execution speed.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM