C ++ implementation of STL in the simple Vector

The vector can accommodate standard library string class,

Directly on the code, StrVec.h contents of the file:

. 1  #ifndef STRVEC_H
 2  #define STRVEC_H
 . 3  
. 4 #include <the iostream>
 . 5 #include < String >
 . 6 #include <Memory>
 . 7  the using  namespace STD;
 . 8  class StrVec {
 . 9  public :
 10      // default constructor 
. 11      StrVec (): Elements (nullptr a), first_free (nullptr a), CAP (nullptr a)
 12 is      {
 13 is          
14      }
 15      // copy constructor 
16      StrVec ( const StrVec & );
 . 17     //拷贝赋值运算符
18     StrVec& operator=(const StrVec&);
19     ~StrVec();
20     void push_back(const string&);
21     size_t size() const { return first_free - elements ; }
22     size_t capacity() const {  return cap - elements  ;}
23     string* begin() const { return elements ;}
24     string* end() const{ Return first_free;}
 25      
26 is      
27      
28  Private :
 29  static the allocator < String > the alloc;   // allocation element
 30   // function using the added element 
31 is   void chk_n_alloc ()
 32  {
 33 is       IF (size () == Capacity ( ))
 34 is              Reallocate ();
 35  
37 [  }
 38 is  // utility function is a copy constructor, assignment operators, and destructors used 
39 pair < String *, String *> alloc_n_copy ( const  String *,const  String * );
 40  
41 is    
45  void  Free ();
 46 is  void Reallocate ();
 47  String * Elements; // pointer to the first element in the array 
48  String * first_free; // pointer to the first free element in the array 
49  String CAP *; // pointer to point to the array after the end of 
50  
51 is      
52 is  };
 53 is 
59  #endif

StrVec.cpp contents of the file:

. 1 #include " StrVec.h " 
2  
. 3 STD :: the allocator < String > StrVec the alloc ::;
 . 4  
. 5  void StrVec :: push_back ( const  String & S)
 . 6  {
 . 7      chk_n_alloc (); // make sure there is room for the new element
 8      // copy elements first_free pointed configuration of s 
. 9      alloc.construct (first_free ++ , s); 
 10          
. 11      
12 is  }
 13 is pair < String *, String *> :: StrVec alloc_n_copy ( const  StringB *, const  String * E)
 14  {
 15      Auto Data = alloc.allocate (E- B);
 16      return the make_pair (Data, uninitialized_copy (B, E, Data));
 . 17      
18 is      
. 19  }
 20 is  void StrVec :: Free ( )
 21  {
 22      // not transmitted to deallcoate a null pointer 
23 is      IF (Elements)
 24      {
 25          for (P = Auto first_free; P =! Elements;)
 26 is              alloc.destroy (- P);
 27         alloc.deallocate(elements,cap-elements) ;
28     }
29     
30     
31 }
32 StrVec& StrVec::operator=( const StrVec& rhs )
33 {
34     auto data = alloc_n_copy(rhs.begin() , rhs.end() );
35     free() ;
36     elements = data.first ;
37     first_free= cap=data.second;
38     
39     return *this ;
40     
41 }
42 void StrVec::reallocate()
43 {
 44      // we will allocate memory space twice the size of the current; 
45      Auto newCapacity = size ()? 2 * size (): . 1 ;
 46 is      // allocate new memory 
47      Auto NewData = alloc.allocate (newCapacity);
 48      
49      Auto dest = NewData;
 50      Auto elem = Elements;
 51 is      for (size_t I = 0 ; I = size ();! I ++ )
 52 is          alloc.construct (dest ++, STD :: Move (* elem ++ ));
 53 is      Free ();
 54 is      Elements = NewData;
 55     first_free = dest ;
56     cap = elements+ newcapacity ;
57     
58     
59 }
60 StrVec::~StrVec()
61 {
62     free();
63 }


Test code for the maintest.cpp

 1 #include "StrVec.h"
 2 
 3 int main()
 4 {
 5     StrVec vec1;
 6     vec1.push_back("ok1");
 7     vec1.push_back("ok2");
 8 
 9     auto begin = vec1.begin();
10     auto end= vec1.end();
11 
12     while( begin != end )
13     {
14       cout<<*begin<<endl;
15       // cout<<endl;
16       begin++;
17 
18     }
19 
22  return 0;
23 }

 

Guess you like

Origin www.cnblogs.com/wanshuafe/p/5340122.html