c ++ vector Precautions

1. Initialization

       c ++ added after 11 braces {} are initialized, and to note the difference (), such as:

         std :: vector <int> vecTest1 (5); // initialize the five elements, each of which is 0

         std :: vector <int> vecTest2 {5}; // initializes an element value is 5

    

 

2. Add the elements: push_back

       Push_back by adding a new element into the vector, vector memory sometimes vary, depending on the size and capacity size, of course, these systems are handled, reference may be made stl source

        When the size <capacity directly added to the end, does not change

        When the size == capacity when will re-apply for another piece of memory, and then copy the past added to the end, this time there will be a change.

        For stl containers have members:

                 begin () // start position 

                 end () // end position 

                 size () // current size 

         capacity () // current capacity, ie the application memory size

     vector is a contiguous memory space, the memory location identified three, start, end, finish, size = end-start, capacity = finish-start

     Many times when using the vector, you see the size = capacity, this time directly add elements to the end, the memory obviously is not enough, then re-allocate a sufficient size elsewhere

     Sometimes there are size <capacity, this time directly added to the tail of the.

std::cout << "vecNum push back init" << std::endl;
vector<int> vecNum(5);
std::cout << "vecNum addr: " << &vecNum << std::endl;
for(auto i = 5; i < 10; i++)
{
	vecNum.push_back(i*10);
	std::cout << "vecNum push_back(" << i << ")=" << i*10 << std::endl;

	std::cout << "vecNum.size() = " << vecNum.size() << ",vecNum.capacity() = " << vecNum.capacity() << std::endl;
	std::cout << "vecNum.begin() addr: " << &(*vecNum.begin()) << std::endl;
}
std::cout << "vecNum addr: " << &vecNum << std::endl;

  

       

3. With regard to earse and remove

     erase returns an iterator about the current position of the deleted elements, it is important to note that when traversing the ++ operator, the other list, map almost,

    Note that after earse memory not really emptied, just delete the contents of the real capacity of the size of the capacity has not changed, need to be implemented by swap reducing the capacity of

     Clear all conceivable: vector <int> () swap (vecNum);.

auto itor = vecNum.begin();
for( ; itor != vecNum.end(); )
{
    auto num = *itor;
    if(num == 60)
    {
        itor = vecNum.erase(itor);
        break;
    }
    else
    {
        itor++;
    }
}

  :: COUT << STD "After ERASE Element 60:" STD :: << endl;
  printVector (vecNum);
  Vector <int> (VECNUM) .swap (VECNUM) ; // Clear the cavity VECNUM memory
  printVector (vecNum) ;

     

 

      // remove just delete the pointer to the iterator is moved forward by the need to delete the element moved forward, so they all need to be removed at the end of the

      // Returns an iterator pointing to the tail of the new elements to be deleted

      So with earse still have to use, so generally you really want to delete, traverse the proposal directly use earse

auto itor = remove_if(vecNum.begin(), vecNum.end(),[](int x)->bool{ return x == 20; });
//or
//auto itor = remove(vecNum.begin(), vecNum.end(),20);
        
//通过erase删除
vecNum.erase(itor, vecNum.end());


4. With regard to vector <bool> - caution

       Source:  https://blog.csdn.net/DoronLee/article/details/78462208

       vector <bool> is not an STL container, not an STL container, not an STL container!

       First vector <bool> is not a vector container in the usual sense, this is derived from historical issues. 

When early in the C ++ 98, there is vector <bool> this type, but because it was to take into account the space-saving idea, so the vector <bool> inside a Byte is not a Byte store, which is a bit a bit saved!

Because C ++ does not go directly to a bit to operate,

    Therefore, with the operator [] when  a normal container should return a reference to the corresponding element,

   But for vector <bool> actually visit it is a "proxy reference" rather than a "true reference", returns: Object "std :: vector <bool> reference" type.

   Therefore, there will be a problem when using the auto automatic type inference

//vector<bool>慎用
vector<bool> vecBool = { false, true, false };
bool test1 = vecBool[0];
auto test2 = vecBool[1];

= to true test1; // test1 initialization it is actually implies an implicit type conversion
test2 = to false; // test2 it is not BOOL type, but an inner class a vector <bool> of, if at a time test2 modified value, the value will follow the modified vecBool
Auto index = 0;
for (Auto I: vecBool)
{
    COUT << "vecBool [" << << index "]" STD :: << endl << I ;
    index ++;
}

     

Guess you like

Origin www.cnblogs.com/leehm/p/10929756.html