Std :: vector :: assign - redistribute data?

I work with the STL library, and my goal is to minimize cases of data redistribution. I was wddering doing

std :: vector :: assign (size_type n, const value_type & val)

redistributes data if the size does not change or does it really just assign new values ​​(for example, using the = operator)?

The STL documentation at http://www.cplusplus.com/ means the following (C ++ 98):

In the fill option (2), the new content consists of n elements, each of which is initialized with a copy of val. If redistribution occurs, the required storage is allocated using an internal allocator.

All elements stored in the container before the call are destroyed and replaced by newly created elements (in this case, the assignment of elements is not performed). This leads to automatic redistribution of the allocated storage space if - and only if - the new vector size exceeds the bandwidth of the vector.

The phrase “no element assignments” makes things a little confusing.

So, for example, I want to have a class vector (for example, cv :: Vec3i OpenCV). Does this mean that

  • will the cv :: Vec3i destructor or constructor be called?
  • Will a direct copy of the Vec3i memory be created and the vector populate?
  • , ? . , assign() ?

EDIT: - 0 ( , std::vector < cv:: Vec3i > v). - . std::vector .

, (), :

 for(int i=0; i<v.size(); i++)  
   for(int j=0; j<3; j++)
     v[i][j] = 0;

++ 98

+4
4

, , , , :

  • ( ), clear()
  • n , .

, , :

, ( ). reserve(). , assign() , ( , ), .

, - , , , .

+1

assign :

void assign (size_type n, const T & t);

:

erase(begin(), end());
insert(begin(), n, t);

, . t , , .

, value_type MoveAssignable ( erase ).

insert , value_type CopyInsertable CopyAssignable.

, . . . .

+1

std::vector.assign(...) , . , .

, , : ++ 11 .

0

vector::resize,

std::vector::assign(size_type n, const value_type& val)

"val" . resize, / , . resize, realloc , :

, , / , (, new ), .

, YourClass::operator=, , .

, !

0

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


All Articles