vector,array,c++

hello everyone. could anyone please tell me if writing a code this way is considered to be correct? i mean adding vector as an argument in TreeSort function, but then using array in its body.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  template<typename T>
void store(Tree* root, T arr[],int &i)
{
    if (root != nullptr)
    {
        store(root->left, arr, i);
        arr[i++] = root->data;
        store(root->right, arr, i);
    }
}

template<typename T>
void TreeSort(vector<T>& a)
{
    struct Tree* root = nullptr;
    root = insert(root, a[0]);
    for (size_t i = 1; i < a.size(); i++)
        insert(root, a[i]);
    int i = 0;
    store(root, a.data(), i);
}
T arr[] as a function argument is equivalent to T* arr, so yes you can pass data() to this. This doesn't necessarily mean your logic is correct; are you running into any issues with your code as-is? If so, post a larger excerpt. What is the point of your store function if you are already presumably putting values in the tree in your insert function?
Topic archived. No new replies allowed.