C ++ vector: Call of erase and non-argument Constructor (Code tutorial), vectorerase

Source: Internet
Author: User

C ++ vector: Call of erase and non-argument Constructor (Code tutorial), vectorerase

Vector: erase

Delete the element of C ++ vector. The source code is as follows:

template <class _Tp, class _Allocator>inline _LIBCPP_INLINE_VISIBILITYtypename vector<_Tp, _Allocator>::iteratorvector<_Tp, _Allocator>::erase(const_iterator __position){#if _LIBCPP_DEBUG_LEVEL >= 2    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,        "vector::erase(iterator) called with an iterator not"        " referring to this vector");#endif    _LIBCPP_ASSERT(__position != end(),        "vector::erase(iterator) called with a non-dereferenceable iterator");    difference_type __ps = __position - cbegin();    pointer __p = this->__begin_ + __ps;    this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));    this->__invalidate_iterators_past(__p-1);    iterator __r = __make_iter(__p);    return __r;}

C ++ moves all the elements behind the deleted part forward as a set.

Therefore, assume that we need to clear the elements, and do not write as follows:

int main(){    vector<int> v_int;    for(int i=0;i<10;i++){        v_int.push_back(i+1);    }    int size = v_int.size();    vector<int>::iterator it = v_int.begin();    while(size--) {        cout<<*it<<" ";        v_int.erase(it++);   // attention !        cout<<"size: "<<v_int.size()<<endl;    }    return 0;}

The result is as follows:

1 size: 93 size: 85 size: 77 size: 69 size: 5

In this example, you can change it ++ to it to clear all the elements.

No-argument constructor call

Will Base instance () call the class's no-argument constructor?

The answer is no. C ++ interprets it as a function that returns the Base object.

#include <iostream>#include <vector>#include <string>using namespace std;class Base{public:    Base(){ cout<<"Base()..\n"; }    Base(string str){ cout<<str<<endl; }    ~Base(){ cout<<"~Base().. \n"; }};int main(){    //Base ins(); //empty parentheses interpreted as a function declaration    Base ins;     //ok    Base ins2("hello world");  //ok    return 0;}

However, it is interesting that the constructor with parameters can be called in this way.

Sometimes, we encounter such a function:

void show(const Base &b){    //...}

We need to pass a variable stored in the stack to this function. To construct such a variable, we can simply write Base () in this way. It is equivalent to Base B.

That is:

    //show(Base("hello"));  //ok. output: hello     it's equal to 'Base b("hello"); show(b);'    //show(Base());         //ok. output: Base()..  it's equal to 'Base b; show(b);'

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.