包含标签 c++ 的文章

cpp 中删除 vector 中的数据

一、vector 中的数据删除 vector 中删除元素,使用 erase 来达到目的 int main() { vector<int> vecInts; vecInts.push_back(1); vecInts.push_back(2); vecInts.push_back(3); vecInts.push_back(3); vecInts.push_back(4); // 删除一个中间的非相同元素,这种方法可以达到目的 // for (auto it = vecInts.begin(); it != vecInts.end(); it++) { // if (*it == 2) { // vecInts.erase(it); // } // } // 删除相邻相同的元素 // for (auto it = vecInts.begin(); it != vecInts.end(); it++) { // if (*it == 3) { // vecInts.erase(it); //出现 core, 由于迭代器改变 // } // } // 删除元素在最后 // for (auto it = vecInts.……

阅读全文

c++ noncopyable 类

一、noncopyable类作用 当类实现者不想让调用者拷贝或赋值构造类对象时,继承noncopyable类可达到此目的。 二、noncopyable类实现 class noncopyable { protected: noncopyable() {} ~noncopyable() {} private: noncopyable(const noncopyable&); const noncopyable& operator=(const noncopyable&); }; // c++11 实现方法 class noncopyable { protected: noncopyable() = default; ~noncopyable() = default; private: noncopyable(const noncopyable&) = delete; const noncopyable& operator=(const noncopyable&) = delete; }; 三、原理 派生类调用拷贝构造函数或赋值构造函数时, 会调用基类(noncopyable)的拷贝构造函数或赋值构造函数 class A : public noncopyable { public: A(int i) { i_ = i; } private: int i_; }; //调用 A a1(55); A a2(a1); //error; ……

阅读全文