• 在类外部定义类成员记得加类作用域!切记


      1 #include<iostream>
      2 #include<algorithm>
      3 #include<memory>//the hearder file of shared_ptr
      4 #include <vector>
      5 #include <list>
      6 #include <map>
      7 #include <string>
      8 #include <set>
      9 #include <utility>
     10 
     11 using namespace std;
     12 
     13 
     14 class StrVec {
     15 public:
     16     StrVec() ://allocator成员进行默认初始化
     17         elements(0), first_free(0), cap(0) {}
     18     StrVec(const StrVec&);
     19     StrVec& operator=(const StrVec&);
     20     ~StrVec();
     21 
     22     void push_back(const std::string&);
     23     size_t size()const { return first_free - elements; }
     24     size_t capacity()const { return cap - elements; }
     25     std::string *begin()const { return elements; }
     26     std::string *end()const { return first_free; }
     27 private:
     28     static std::allocator<std::string> alloc;
     29     void chk_n_alloc() {
     30         if (size() == capacity())reallocate();
     31     }
     32     std::pair<std::string*, std::string*> alloc_n_copy(const std::string*, const std::string*);
     33     void free();
     34     void reallocate();
     35 
     36     std::string *elements;
     37     std::string *first_free;
     38     std::string *cap;
     39 };
     40 
     41 allocator<string> StrVec::alloc;
     42 
     43 void StrVec::push_back(const string& s) {
     44     chk_n_alloc();//检查是否有可用空间
     45     alloc.construct(first_free++, s);
     46 }
     47 
     48 pair<string*, string*> StrVec::alloc_n_copy(const string*b, const string*e) {
     49     string* newdata = alloc.allocate(e - b);
     50     return make_pair(newdata, uninitialized_copy(b, e, newdata));
     51 }
     52 
     53 void StrVec::free() {//销毁元素并且释放内存
     54     if (elements) {//检查所要释放的内存非空
     55         for (string* p = first_free;p != elements;/*空哦*/)
     56             alloc.destroy(--p);//逆序销毁
     57         alloc.deallocate(elements, cap - elements);
     58     }
     59 }
     60 
     61 StrVec::StrVec(const StrVec& v) {
     62     auto newdata = alloc_n_copy(v.begin(), v.end());
     63     elements = newdata.first;
     64     cap = first_free = newdata.second;
     65 }
     66 
     67 StrVec& StrVec::operator=(const StrVec& v) {
     68     auto newdata = alloc_n_copy(v.begin(), v.end());
     69 
     70     free();
     71     elements = newdata.first;
     72     cap = first_free = newdata.second;
     73     return *this;
     74 }
     75 
     76 StrVec::~StrVec() { free(); }
     77 
     78 void StrVec::reallocate() {
     79     auto newcapacity = size() ? size() * 2 : 1;
     80 
     81     auto newdata = alloc.allocate(newcapacity);
     82     auto dest = newdata;
     83     auto ele = elements;//elements不可改变,下面size()和free()都将用到
     84 
     85     for (size_t i = 0;i != size();i++)
     86         alloc.construct(dest++, *ele++);
     87     free();
     88 
     89     elements = newdata;
     90     first_free = dest;
     91     cap = elements + newcapacity;
     92 }
     93 
     94 int main(void) {
     95     StrVec xi;
     96     for (int i = 0;i < 10;i++)xi.push_back("xixi");
     97     for (const auto& mem : xi)cout << mem << " ";
     98 
     99     cout << endl;
    100     return 0;
    101 }

    静态成员alloc前面没加StrVec::导致报错,无法解析的外部命令,大概长这个样子:

    想起来以前也碰到过,那时候没弄清楚,只是把成员函数的实现拉进类内了,受教(抱拳

  • 相关阅读:
    Grid search in the tidyverse
    Handling Class Imbalance with R and Caret
    R语言-Kindle特价书爬榜示例 & 输出HTML小技巧(转)
    Centos7下安装部署MXNET
    特征选择, 经典三刀(转)
    vue知识点14
    vue知识点13
    vue知识点12
    vue知识点11
    vue知识点10
  • 原文地址:https://www.cnblogs.com/schsb/p/8591994.html
Copyright © 2020-2023  润新知