繁体   English   中英

std :: vector clear函数的实现

[英]Implementation of std::vector clear function

我一直试图了解std :: vector中clear()函数的工作方式,我试图模拟std :: vector的工作原理。

到目前为止,我了解到clear()破坏了所有对象,但保留了向量的容量。

我不明白的一点是如何调用vector中对象的析构函数。

class A {
  public:
    A(int a) {
        m_a = a;
        cout << "Constructed object number: " << a << endl;
    }

    ~A() {
        cout << "Destructed object number: " << m_a << endl;
    }

    int m_a;
};

int main() {
  char** memory =  new char*[100];
  A t1(1);
  memory[sizeof(A)*10] = reinterpret_cast<char *>(&t1);
  A* t = reinterpret_cast<A*>(memory[sizeof(A)*10]);
  cout << t->m_a << endl;

  //Trying to figure out on how to clear the vector. 
  memory[sizeof(A)*10] = NULL;

  //Testing on how the destructor is getting called
  vector<A*> vec;
  vec.push_back(&A(2));  // I know it is wrong, just here for understanding purposes.
  A t2(3);
  vec.push_back(&t2);
  cout << "Clear" << endl;
  vec.clear();
  cout << "End" << endl;

  return 0;
}

清除向量不会调用“ t2”的析构函数,因为它是这里的指针,但是如果我存储对象,则将在clear函数中调用“ t2”的析构函数。

这仅出于理解std :: vector实际工作方式的目的。

@Anurag关于STL容器的一件非常重要的事情:不管容器类型(顺序容器,关联容器和无序容器)如何,都保留对象/指针/原始类型的副本。

现在回到您的疑问1.带对象和2.带指针,下面我以示例进行解释:

  1. 对象类型示例(参见输出,您将很清楚):

 #include <iostream> #include<vector> using namespace std; class Achintya { static int counter ; static int i ; public: Achintya() { cout<<"Achintya Constructor called : " << ++i <<endl; } ~Achintya() { cout<<"Achintya destructor called : " << ++counter <<endl; } Achintya(const Achintya&) { cout<<"Achintya copy constructor called : "<<endl; } }; int Achintya:: i; int Achintya:: counter; int main() { vector<Achintya> VecAchintya; Achintya a1; // Address of the pointer cout << " 1st object address : " <<&a1 <<endl; //Push back to vector // it is just a copy and store in vector (copy constructor is begin invoke ) VecAchintya.push_back(a1); cout << " =============================================" << endl; cout<< " Number of element present in vector is : " << VecAchintya.size() << endl; cout << " =============================================" << endl; // cli is not iterator for(auto& cli:VecAchintya ) { cout << " Adress of 1st element is : " << &cli <<endl; } // it clear the object it self which is being created at the time of the push_back() VecAchintya.clear(); cout << " =============================================" << endl; cout<< " Number of element present in vector is : " << VecAchintya.size() << endl; cout << " =============================================" << endl; } output :: Achintya Constructor called : 1 1st object address : 0x7ffd70ad339f Achintya copy constructor called : ============================================= Number of element present in vector is : 1 ============================================= Adress of 1st element is : 0x23c6c30 Achintya destructor called : 1 ============================================= Number of element present in vector is : 0 ============================================= Achintya destructor called : 2 

  1. 指针类型示例(参见输出,您将很清楚):

 #include <iostream> #include<vector> using namespace std; class Achintya { static int counter ; static int i ; public: Achintya() { cout<<"Achintya Constructor called : " << ++i <<endl; } ~Achintya() { cout<<"Achintya destructor called : " << ++counter <<endl; } Achintya(const Achintya&) { cout<<"Achintya copy constructor called : "<<endl; } }; int Achintya:: i; int Achintya:: counter; int main() { vector<Achintya *> VecAchintya; Achintya* a1 = new Achintya(); // Address of the pointer cout << " 1st object address : " << a1 <<endl; //Push back to vector // it is just a copy the pointer value and store VecAchintya.push_back(a1); cout << " =============================================" << endl; cout<< " Number of element present in vector is : " << VecAchintya.size() << endl; cout << " =============================================" << endl; // cli is not iterator for(auto& cli:VecAchintya ) { cout << " Adress of 1st element is : " << cli <<endl; } // it clear the pointer it self which is being stored at the time push_back() VecAchintya.clear(); cout << " =============================================" << endl; cout<< " Number of element present in vector is : " << VecAchintya.size() << endl; cout << " =============================================" << endl; // call destructor explicitly delete a1; } Output :: Achintya Constructor called : 1 1st object address : 0x2533c20 ============================================= Number of element present in vector is : 1 ============================================= Adress of 1st element is : 0x2533c20 ============================================= Number of element present in vector is : 0 ============================================= Achintya destructor called : 1 

您可能会发现研究pop_back并考虑将其缩小并清除为特殊的多pop调用更容易。 您当然可以在自己的实现中以这种方式实现它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM