0

I'm having a lot of trouble using vector as a private data member. I'm not sure how to reserve space for 10 elements and I tried a combination of resize and I also tried calling the vector constructor in my class's constructor but I keep getting compilation errors from the vector constructor. I want to initialize the vector with 10 elements, because I'm trying to populate a vector with the values from my binary tree.

class bst
{
public:
    // constructor:  initializes an empty tree
    bst() 
   {
        root = nullptr;
        totalNodes = 0;
        treeHeight = -1; // empty tree with no nodes has a height of -1
        myVector->reserve(10);
    }
private:

    bst_node * root;
    int totalNodes;
    std::vector<T> *myVector;
    int treeHeight; // height of the tree to be used later for some functions
}; // end class bst

void _to_vector(bst_node *r, std::vector<T> *vec) 
{
    if (r == nullptr) {
        return;
    }
    _to_vector(r->left, vec);
    vec->push_back(r->val);
    _to_vector(r->right, vec);
}
JeJo
  • 30,635
  • 6
  • 49
  • 88
Shinji-san
  • 971
  • 4
  • 14
  • 31
  • 3
    You don't have a vector, you have a pointer. – juanchopanza Apr 15 '18 at 20:06
  • 1
    `std::vector *myVector` is *extremely* unlikely to be a good idea. Which book taught you that? – Baum mit Augen Apr 15 '18 at 20:10
  • 1
    since myVector is a vector*, you can use myVector(new vector(10)) in the initialization list of the constructor to initialize 10 elements. However, as others have mentioned, why not just make it a vector? – gchen Apr 15 '18 at 20:12
  • For the parameter or the return type? – Shinji-san Apr 15 '18 at 20:12
  • Additionally, your attempt to initialize the vector with 10 elements is wrong. `reserve(10)` will only reserve space for 10 elements, but not really place them on the vector. The correct function is `resize()`. Yes, thats confusing, I also ocassionally do it wrong after decades ;) `reserve()` is a pretty unimportant command, only necessary to improve performance. [reserve()](http://en.cppreference.com/w/cpp/container/vector/reserve) [resize()](http://en.cppreference.com/w/cpp/container/vector/resize) – user2328447 Apr 15 '18 at 20:16
  • 1
    Not to be nit-picky about the downvotes here, and I'm hardly the person to come to the defense of bad questions, but: This is not a stupid question. It is a clearly a C++ beginner asking for help, They said what they tried to do, explained what they were having a problem with, did not ask for a solution to a homework problem, and showed their code. And before you bitch that "they should have done more research before asking," two words: "Pointers. Beginner." Granted, it might be obvious with more experience, but that's exactly my point. Upvoting the question just to offset the downvotes. – frasnian Apr 15 '18 at 20:22
  • I'm apparently feeling love for my fellow humans today :) – frasnian Apr 15 '18 at 20:24
  • 1
    @frasnian It may not be a stupid question, but it is far from being good, and unlikely to elicit good answers or be useful to anyone in its current form. An [mcve] would be a good starting point. Also, [ask]. Unbelievable it has 3 up-votes. – juanchopanza Apr 15 '18 at 20:46
  • @juanchopanza - point taken (I've pointed people at the MCVE more than I care to remember). It was just getting completely beat down when I responded. Three up-votes, though? Wow. I think a net of zero is fair, but.YMMV :) – frasnian Apr 15 '18 at 22:56

1 Answers1

1

reserve() is not the same as resize(). See this answer for a detailed explanation. https://stackoverflow.com/a/7397862/9281750

Since you want to initialize your vector with 10 elements(i.e. has a size of 10), you should use resize() instead of reserve() inside your constructor.

An alternative solution would be to use the vector's constructor directly in the initialization list.

bst() :  myVector(new vector<T>(10)) {...} //myVector is initialized with 10 default T elements

Also, a pointer to a std::vector is usually not necessary. You can simply make myVector a std::vector and pass-by-reference in a function. The following should perform the same thing

class bst
{
public:
    // constructor:  initializes an empty tree
    bst() : myVector(10)
   {
       ...
    }
private:
    ...
    std::vector<T> myVector;
    ...
}; // end class bst

void _to_vector(bst_node *r, std::vector<T>& vec) 
{
    if (r == nullptr) {
        return;
    }
    _to_vector(r->left, vec);
    vec.push_back(r->val);
    _to_vector(r->right, vec);
}
gchen
  • 1,173
  • 1
  • 7
  • 12