Hi all again!
Now i have a question about the array that keeps indices sorted.
I've thought about 2 ways to do it:
1) following TheDestroyer suggestion, have it to be always sorted and sort it each time insert an element.
2) keep it unsorted and sort it only when i call the begin() function for sortIterator.
The 1st way seems the best one to me, but i don't know how to solve this problem:
how can i keep the array sorted whenever i change a value?
For example, after 4 insert(value) calls my data array is:
[42, 12, 24, 10]
and my indices array should be:
[3, 1, 2, 0]
What happens if i change the value in position 1 in the data array?
vec[1]=9;
or
iterator[1]=9;
Now my data array is [42, 9, 24, 10]
but the "sorted" indices array is still the same [3, 1, 2, 0]
What should i do to sort it again, after a change in the data array?
> how can i keep the array sorted whenever i change a value?
Intercept every change to a value and re-order the indices accordingly.
For example, for modification of a value through the subscript operator on the vector:
Likewise, return a my_vector<T>::reference instead of a reference everywhere - my_vector<T>::front(), my_vector<T>::iterator::operator*() etc.
The semantics of modifying a value through the 'sortIterator' is messy to say the least; perhaps make the 'sortIterator' an iterator that iterates over non-modifiable values.
Oh yes. And the compiler was also telling me that: returning a reference to temporary...
Need to pay more attention...
Thanks again, you're saving the project!
Hi, im back again. When i think i'm done, a new problem comes out!
Now i can't figure out how to return a my_vector<T>::reference from inside my_vector<T>::iterator.
Inside the iterator class i only have a pointer to data array of my_vector, so i dont know how to call reference( my_vector<T>& v, std::size_t p ) : vec(v), pos(p) {}
since i don't have the *this, representing my_vector, nor pos, as it was for reference operator[] ( std::size_t pos ) { return reference( *this, pos ) ; }
Neverending story's here!
Thank you for the code, but i have some more questions now.
Is the default constructor always needed for the iterator class?
In this case i don't know how to inizialize my_vector<T>& vec;
For example, i used to create iterator like this:
1 2 3
my_vector<T>::iterator start, end;
start = vec.begin();
end = vec.end();
Now i have to write:
1 2
my_vector<T>::iterator start = vec.begin();
my_vector<T>::iterator end = vec.end();
Is this behaviour correct or do i always have to guarantee the support to my_vector<T>::iterator start, end;
> Is the default constructor always needed for the iterator class?
No. To be compatible with the standard library, an iterator must be copy- constructible, copy-assignable, and destructible; in addition iterator l-values must be swappable. However, my understanding is that for this particular project, use of standard library components is not allowed, and therefore standard conformance is not an issue.
If you want to make the iterator default-constructible and swappable, use a pointer instead of a reference.
I don't think your solution is a good one..
Because you are now returning a T& instead of a const T& and this doesn't change nothing. You still don't have any member in my_vector::reference...
Ah! You've finally come face to face with it; we can have 'smart' pointers, but not 'smart' references - the structure-to-member . operator can't be overloaded.
> Should i change my_vector::reference
That change does not really improve anything - it makes it worse by masking the object identity.