Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7994397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:07:16+00:00 2026-06-04T14:07:16+00:00

I have a vector and it is going to hold 10 objects maximum. When

  • 0

I have a vector and it is going to hold 10 objects maximum. When I create the vector I believe it creates 10 “blank” objects of the class it will hold. Therefore I have a problem because my code relies on checking that a vector element is null and obviously it is never null.

How can I check whether a vector object contains an element I inserted, or one of the default constructor “blank” objects upon initialisation?

Is there a technique round this?

(I need to check for null because I am writing a recursive algorithm and the termination point, when the recursive function returns is when an object in the vector is null)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:07:17+00:00Added an answer on June 4, 2026 at 2:07 pm

    An instance of a class cannot be null. Only a pointer.

    You do, however, have size() that you can use.

    typedef stdd::vector<SomeClass> vec;
    //define some vec, v
    for (vec::size_type i = 0, s = vec.size(); i < s; ++i) {
        //do something with v[i]
    }
    

    With a recursive function you could use this idea by passing along a maximum index.

    void recursiveFunc(vec& v, vec::size_type s);
    

    Then when checking your condition to recurse, you would need to check “am I at the end of the vector?”

    Alternatively, instead of working on indexes, you could use iterators:

    template <typename Iterator>
    void recursiveFunc(Iterator begin, const Iterator& end);
    

    If done correctly (and if possible in your situation), this could decouple your manipulation from being aware of the underlying data being stored in a vector.

    The loop to go over the vector would then look like:

    while (begin != end) {
        //do something with *begin
        ++begin;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that has a vector of another class objects as a
I have a vector of pointers to a class. I need to call their
I have a vector of pointers to objects. I'd like to remove objects from
I have a Vector that holds a number of objects. My code uses a
I have a vector class with hashCode() implemented. It wasn't written by me, but
I'm going nuts trying to find the problem here. I have a main window,
I'm trying to create a vector (or any STL container, really) that could hold
I have a vector of objects which are pointers but when I print the
I have a set of objects in a Vector from which I'd like to
I am first going to describe the problem which I have, and then give

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.