仿寫的一個string,雖然效率比不上std::string,但也算自己幾乎完整(90%)的寫完了以個string類
_STL_STRING_H_ #include #include #include #include <cstring> #include <cstdlib> #include <iostream> template< iterator> typedef typename iterator_traits<iterator> typedef typename iterator_traits<iterator> typedef typename iterator_traits<iterator> typedef reverse_iterator_t<iterator> typedef reverse_iterator_t<iterator>& reverse_iterator_t( self rs){ p = self ++(){ --p; * self_type ++( self_type tmp(* ++* self --(){ ++p; * self_type --( self_type tmp(* --* reference * (){ * pointer -> (){ &* friend == ( reverse_iterator_t& lhs, reverse_iterator_t& lhs.p == friend != ( reverse_iterator_t& lhs, reverse_iterator_t& !(lhs == pointer get_p(){ typedef typedef * typedef * typedef & typedef & typedef reverse_iterator_t<iterator> size_t length; *p_data; simple_alloc<, alloc> size_t npos = - () : length( ( * ( * ( size_t n, & template< InputIterator> ~ ( & ( & (&& str) & = ( & & = (&& str) & = ( & & = ( * iterator begin() { &p_data[ iterator end() { & const_iterator cbegin() { &p_data[ const_iterator cend() { & reverse_iterator_t<iterator> rbegin(){ reverse_iterator_t<iterator>(end() - reverse_iterator_t<iterator> rend(){ reverse_iterator_t<iterator>(begin() - size_t size() { size_t max_size() { size_type(- resize(size_t n, & empty() { length == clear() { data_alloc.deallocate(p_data, length + reference front() { * reference back() { *(end() - reference [](size_t index){ const_reference [](size_t index) { const_reference at(size_t index) *c_str() { iterator data() { size_t copy(* s, size_t len, size_t pos) size_t find( & str, size_t pos) size_t find( * s, size_t pos) size_t find( * s, size_t pos, size_t n) size_t find( c, size_t pos) size_t find_first_of( & str, size_t pos = ) size_t find_first_of( * s, size_t pos = ) size_t find_first_of( * s, size_t pos, size_t n) size_t find_first_of( c, size_t pos = ) size_t find_last_of( & str, size_t pos = npos) size_t find_last_of( * s, size_t pos = npos) size_t find_last_of( * s, size_t pos, size_t n) size_t find_last_of( c, size_t pos = npos) size_t rfind( & str, size_t pos = npos) size_t rfind( * s, size_t pos = npos) size_t rfind( * s, size_t pos, size_t n) size_t rfind( c, size_t pos = npos) substr(size_t pos = , size_t len = npos) compare( & str) compare(size_t pos, size_t len, & str) compare(size_t pos, size_t len, & size_t subpos, size_t sublen) compare( * s) compare(size_t pos, size_t len, * s) compare(size_t pos, size_t len, * s, size_t n) & += ( & & += ( * & += ( & append( & & append( & & append( * & append( * & append(size_t n, & template< InputIterator> & push_back( & & replace(size_t pos, size_t len, & & replace(iterator i1, iterator i2, & & replace(size_t pos, size_t len, & & replace(size_t pos, size_t len, * & replace(iterator i1, iterator i2, * & replace(size_t pos, size_t len, * & replace(iterator i1, iterator i2, * & replace(size_t pos, size_t len, size_t n, & replace(iterator i1, iterator i2, size_t n, template < InputIterator> & & insert(size_t pos, & & insert(size_t pos, & & insert(size_t pos, * & insert(size_t pos, * & insert(size_t pos, size_t n, insert(iterator p, size_t n, iterator insert(iterator p, template < InputIterator> & assign( & & assign( & & assign( * & assign( * & assign(size_t n, template < InputIterator> & swap(& friend std::ostream& <<(std::ostream& os, & friend std::istream& >>(std::istream& , & friend == ( & lhs, & friend == ( *p, & friend == ( & lhs, * friend != ( & lhs, & friend != ( *p, & friend != ( & lhs, * friend + ( & lhs, & friend + ( *p_char, & friend + ( & lhs, * friend + ( & ch, & friend + ( & lhs, & friend swap(& x, & set_empty() { length = , p_data = reclaim() { (!empty()) data_alloc.deallocate(p_data, length + iterator& get_data() { size_t& get_length() { }; size_t ::( * length = p_data = data_alloc.allocate(length + memcpy(p_data, p, length + ::( * length = p_data = data_alloc.allocate(n + p_data[length] = ::( size_t n, & length = p_data = data_alloc.allocate(n + p_data[length] = template< InputIterator> :: InputIterator tmp = size_t len = (tmp++ != ++ length = p_data = data_alloc.allocate(length + p_data[length] = ::( & str, size_t pos, size_t len = (len == len = length = len - p_data = data_alloc.allocate(length + uninitialized_copy(str.data() + pos, str.data() + pos + p_data[length] = ::~ ::( & iterator tmp = data_alloc.allocate(str.size() + memcpy(tmp, str.data(), str.size() + p_data = length = ::(&& p_data = length = & :: = ( & (* != ->p_data = ->length = * & :: = (&& (* != ->p_data = ->length = * & :: = ( & (* != & p_new = data_alloc.allocate( p_new[] = p_new[] = length = p_data = data_alloc.deallocate(p_new, * & :: = ( * (* != * = * ::reference (index > ::const_reference ::at(size_t index) (index > resize(n, ::resize(size_t n, & (n == (n > iterator p_new = data_alloc.allocate(n + fill_n(p_new + length, n - *(p_new + (n + )) = p_data = length = data_alloc.deallocate(p_data + n, length + - *(p_data + n) = length = + ( & lhs, & tmp += + ( *p_char, & tmp_l + + ( & lhs, * tmp_l + std::ostream& <<(std::ostream& os, & (! ::size_type len = ( i = ; i < len; ++ std::cout << *(str.data() + == ( & lhs, & lhs.size() == rhs.size() && memcmp(lhs.data(), rhs.data(), lhs.size()) == != ( & lhs, & !(lhs == == ( *p, & strcmp(p, rhs.data()) == != ( *p, & !(p == == ( & lhs, * strcmp(lhs.data(), p) == != ( & lhs, * !(lhs == & ::append( & ->append(str, * & ::append( & iterator p_new = data_alloc.allocate(length + sublen + iterator tmp = uninitialized_copy(p_data, p_data + uninitialized_copy(str.data() + subops, str.data() + subops + sublen + *(p_new + (length + sublen)) = p_data = length += * & ::append( * -> * & ::append( * -> * & ::append(size_t n, & -> * template< InputIterator> & -> * size_t ::copy(* s, size_t len, size_t pos = ) uninitialized_copy(p_data + pos, p_data + pos + & :: += ( & p_new = data_alloc.allocate(length + str.size() + memcpy(p_new + length, str.data(), str.size() + p_data = length = data_alloc.deallocate(p_new, length + str.size() + * & :: += ( * * += * & :: += ( tmp( * += * + ( & ch, & tmp( tmp + + ( & lhs, & tmp( lhs + ::swap(& iterator tmp_data = p_data = str.get_data() = size_t tmp_len = length = str.get_length() = swap(& x, & ::push_back( p_new = data_alloc.allocate(length + uninitialized_copy(p_data, p_data + p_new[length] = p_new[length + ] = p_data = ++ data_alloc.deallocate(p_new, length + data_alloc.deallocate(p_data + length, -- p_data[length] = size_t ::find( & str, size_t pos = ) find(str.data(), pos, length - size_t ::find( * s, size_t pos = ) find(s, pos, length - size_t ::find( * s, size_t pos, size_t n) (pos >= size_t s_len = (n < size_t index = pos, i = (; index < pos + n; ++ (p_data[index] == s[ (; i != s_len; ++ (p_data[index + i] != (i == size_t ::find( c, size_t pos = ) (pos >= size_t index = (; index != length; ++ (p_data[index] == index == length ? ::substr(size_t pos, size_t len) (len == real_len = length - real_len = tmp(p_data + pos, p_data + pos + ::compare( & str) ->compare( ::compare(size_t pos, size_t len, & str) -> ::compare(size_t pos, size_t len, & size_t subpos, size_t sublen) *p = str.data() + -> ::compare( * s) ->compare( ::compare(size_t pos, size_t len, * s) -> ::compare(size_t pos, size_t len, * s, size_t n) istr = pos, = (; istr != pos + len && != n; ++istr, ++ (p_data[istr] < s[ - (p_data[istr] > s[ (istr == pos + len && == (istr == pos + - & ::assign( & -> & ::assign( & ->assign(str.cbegin() + subpos, str.cbegin() + subpos + & ::assign( * ->assign(&s[], & & ::assign( * ->assign(&s[], & & ::assign(size_t n, -> template < InputIterator> & (* != p_new = data_alloc.allocate(last - p_data = length = last - data_alloc.deallocate(p_new, last - * & ::replace(size_t pos, size_t len, & ->replace(pos, len, str, * & ::replace(iterator i1, iterator i2, & -> * & ::replace(size_t pos, size_t len, & ->replace(begin() + pos, begin() + pos + str.data() + subpos, str.data() + subpos + * & ::replace(size_t pos, size_t len, * -> * & ::replace(iterator i1, iterator i2, * -> * & ::replace(size_t pos, size_t len, * ->replace(begin() + pos, begin() + pos + * & ::replace(iterator i1, iterator i2, * iterator tmp = -> ->insert(tmp - * & ::replace(size_t pos, size_t len, size_t n, ->replace(begin() + pos, begin() + pos + * & ::replace(iterator i1, iterator i2, size_t n, iterator tmp = -> -> * template < InputIterator> & iterator tmp = -> -> * size_t ::find_first_of( & str, size_t pos) -> size_t ::find_first_of( * s, size_t pos) ->find_first_of(s, pos, size() - size_t ::find_first_of( * s, size_t pos, size_t n) size_type ret = ( i = pos; i != pos + n; ++ ch = ret = (ret != size_t ::find_first_of( c, size_t pos) -> size_t ::find_last_of( & str, size_t pos) -> size_t ::find_last_of( * s, size_t pos) ->find_last_of(s, pos, size() - size_t ::find_last_of( * s, size_t pos, size_t n) size_type ret = ( i = pos; i != pos - n; -- ch = ret = (ret != size_t ::find_last_of( c, size_t pos) -> size_t ::rfind( & str, size_t pos) -> size_t ::rfind( * s, size_t pos) ->rfind(s, pos, size() - size_t ::rfind( * s, size_t pos, size_t n) size_type ret = ( i = pos, j = ; j != n; ++j, ++ (ret != find(s, i, n - size_t ::rfind( c, size_t pos) size_type ret = ( i = pos; i > ; -- (c == & ::insert(size_t pos, & ->insert(pos, str, & ::insert(size_t pos, & ->insert(p_data + pos, str.cbegin() + subpos, str.cbegin() + subpos + * & ::insert(size_t pos, * -> & ::insert(size_t pos, * ->insert(p_data + pos, s, s + * & ::insert(size_t pos, size_t n, ->insert(p_data + * ::insert(iterator p, size_t n, -> ::iterator ::insert(iterator p, ->insert(p, &c, (&c) + template < InputIterator> p_new = data_alloc.allocate(length + (last - first) + difference_type n = p - iterator tmp = uninitialized_copy(p_data, p_data + (p - tmp = uninitialized_copy(p_data + (p - length = length + (last - p_data = p_data[length] = data_alloc.deallocate(p_new, length + (last - first) + & ::erase(size_t pos = , size_t len = iterator first = begin() + iterator last = (len == npos ? end() : first + -> * ::iterator -> ::iterator (first == size_t n = first - iterator ret = size_t tmp_len = ret - size_t des_num = end() - data_alloc.deallocate(ret + length = p_data[length] = ret -