How does std :: sort modify the type of support?

When I call sortin vectorfrom doubleusing iterators begin()and end()how sortdoes the function modify the original vectorto contain sorted values?

I, although iterators simply represented the value, so how could they force vectorme to modify the original ?

vector<double> nums = {10.33, 20.44, 60.77};
sort(nums.begin(), nums.end(); // how does the original nums get changed?
+4
source share
1 answer

, , . , . , , (*it).

. :

vector<double> nums = {10.33, 20.44, 60.77};
double* it = &nums[0]; //get the address of the first element
++it; //increment the pointer
*it = 42; //assign 42 to nums[1]

( ).

vector<double> nums = {10.33, 20.44, 60.77};
vector<double>::iterator it = nums.begin();//get an iterator to the first element
++it; //increment the iterator
*it = 42; //assign 42 to nums[1]
+4

Source: https://habr.com/ru/post/1628952/


All Articles