(以“[[分类::数据结构]] 事实证明一个能够自动扩展的顺序表也是可以折腾人的0u0 <pre> #include<iostream>#include<cstdlib>#include<cstring>#inclu...”为内容创建页面)
 
(换行)
第1行: 第1行:
[[分类::数据结构]]
+
[[分类:数据结构]]
 
事实证明一个能够自动扩展的顺序表也是可以折腾人的0u0
 
事实证明一个能够自动扩展的顺序表也是可以折腾人的0u0
 
<pre>
 
<pre>
#include<iostream>#include<cstdlib>#include<cstring>#include<cmath>namespace cus { template<typename T> class Vector { T* v; size_t length; size_t cap; void rearrange(size_t logLen) { size_t newLen =( 1 << logLen); if (newLen > cap) { T* from = v; T* to = static_cast<T*>(malloc(sizeof(T)*newLen)); memcpy(to, from, length); free(v); v = to; } cap = newLen; } void reallocate(size_t n) { rearrange(1+ static_cast<int>(log2(n))); } public: Vector() :v(nullptr), length(0), cap(0) {} Vector(T* ori, size_t len) { reallocate(len); memcpy(v, ori, len); } Vector(T* ori, size_t from, size_t len) { reallocate(len); memcpy(&v[from], ori, len); } void resize(size_t n) { reallocate(n); for (int i = length;i < n;++i) { v[i] = 0; } length = n; } void push_back(const T &x) { if (length + 1 >= cap)reallocate(length + 1); v[length++] = x; } friend std::ostream& operator <<(std::ostream& os,Vector a) { for (unsigned i = 0;i < a.size();++i) { os << a[i] << " "; } return os; } inline size_t size() const { return length; } inline size_t capacity() const { return cap; } bool empty() const { return size() == 0; } T& at(size_t index) { if (index >= length)return v[0]; return v[index]; } T& operator [](size_t index) { if (index >= length)return v[0]; return v[index]; } T* find(const T &x) const { for (unsigned i = 0;i < length;++i) { if (v[i] == x) { return &v[i]; } } return nullptr; } size_t find_index(const T &x) const { auto rtn = find(x); if (rtn == nullptr)return static_cast<size_t>(-1); return x; } T* insert(const T* pos,const T& x) { if (length + 1 >= cap)reallocate(length + 1); size_t id = pos - v; if (id >= length)return nullptr; for (auto i =id;i < length;++i) { v[i+1] = v[i]; } ++length; v[id] = x; return &v[id]; } T* insert(size_t index, const T& x) { return insert(v + index, x); } T* erase(const T* pos) { size_t id = pos - v; if (id >= length)return nullptr; --length; for (unsigned i = id;i < length;++i) { v[i] = v[i+1]; } return &v[id]; } T* erase(size_t index) { return erase(v + index); } ~Vector() { if (v == nullptr)return; free(v); } };}int main(){   cus::Vector<int> s;   while(1)   {       int a;       std::cin>>a;       s.push_back(a);       std::cout<<s.size()<<" "<<s.capacity()<<std::endl;   } std::cout<<"(1)初始化顺序表L;"<<std::endl; cus::Vector<char> L; std::cout<<"(2)依次采用尾插法插入a, b, c, d, e元素;"<<std::endl; char a, b, c, d, e; std::cin >> a >> b >> c >> d >> e; L.push_back(a); L.push_back(b); L.push_back(c); L.push_back(d); L.push_back(e); std::cout<<"(3)输出顺序表L;"<<std::endl; std::cout << L << std::endl; std::cout<<"(4)输出顺序表L长度;"<<std::endl; std::cout << "size:" << L.size() << std::endl; std::cout<<"(5)判断顺序表L是否为空;"<<std::endl; std::cout << "isEmpty:" << L.empty() << std::endl; std::cout<<"(6)输出顺序表L的第3个元素;"<<std::endl; std::cout << L << std::endl; std::cout << "3rd:" << L[2] << std::endl; std::cout<<"(7)输出元素'a'的位置;"<<std::endl; std::cout << "a is at:" << L.find_index('a') << std::endl; std::cout<<"(8)在第4个元素位置上插入'f'元素;"<<std::endl; L.insert(3, 'f'); std::cout<<"(9)输出顺序表L;"<<std::endl; std::cout << L << std::endl; std::cout<<"(10)删除L的第3个元素;"<<std::endl; L.erase(2); std::cout<<"(11)输出顺序表L;"<<std::endl; std::cout << L << std::endl; std::cout<<"(12)释放顺序表L。"<<std::endl; system("pause"); return 0;}</pre>
+
#include<iostream>
 +
#include<cstdlib>
 +
#include<cstring>
 +
#include<cmath>
 +
namespace cus {
 +
template<typename T>
 +
class Vector
 +
{
 +
  T* v;
 +
  size_t length;
 +
  size_t cap;
 +
  void rearrange(size_t logLen)
 +
  {
 +
  size_t newLen =( 1 << logLen);
 +
  if (newLen > cap)
 +
  {
 +
    T* from = v;
 +
    T* to = static_cast<T*>(malloc(sizeof(T)*newLen));
 +
    memcpy(to, from, length);
 +
    free(v);
 +
    v = to;
 +
  }
 +
  cap = newLen;
 +
  }
 +
  void reallocate(size_t n)
 +
  {
 +
  rearrange(1+ static_cast<int>(log2(n)));
 +
  }
 +
public:
 +
  Vector() :v(nullptr), length(0), cap(0) {}
 +
  Vector(T* ori, size_t len)
 +
  {
 +
  reallocate(len);
 +
  memcpy(v, ori, len);
 +
  }
 +
  Vector(T* ori, size_t from, size_t len)
 +
  {
 +
  reallocate(len);
 +
  memcpy(&v[from], ori, len);
 +
  }
 +
  void resize(size_t n)
 +
  {
 +
  reallocate(n);
 +
  for (int i = length;i < n;++i)
 +
  {
 +
    v[i] = 0;
 +
  }
 +
  length = n;
 +
  }
 +
  void push_back(const T &x)
 +
  {
 +
  if (length + 1 >= cap)reallocate(length + 1);
 +
  v[length++] = x;
 +
  }
 +
  friend std::ostream& operator <<(std::ostream& os,Vector a)
 +
  {
 +
  for (unsigned i = 0;i < a.size();++i)
 +
  {
 +
    os << a[i] << " ";
 +
  }
 +
  return os;
 +
  }
 +
  inline size_t size() const {
 +
  return length;
 +
  }
 +
  inline size_t capacity() const {
 +
  return cap;
 +
  }
 +
  bool empty() const
 +
  {
 +
  return size() == 0;
 +
  }
 +
  T& at(size_t index)
 +
  {
 +
  if (index >= length)return v[0];
 +
  return v[index];
 +
  }
 +
  T& operator [](size_t index)
 +
  {
 +
  if (index >= length)return v[0];
 +
  return v[index];
 +
  }
 +
  T* find(const T &x) const
 +
  {
 +
  for (unsigned i = 0;i < length;++i)
 +
  {
 +
    if (v[i] == x)
 +
    {
 +
    return &v[i];
 +
    }
 +
  }
 +
  return nullptr;
 +
  }
 +
  size_t find_index(const T &x) const
 +
  {
 +
  auto rtn = find(x);
 +
  if (rtn == nullptr)return static_cast<size_t>(-1);
 +
  return x;
 +
  }
 +
  T* insert(const T* pos,const T& x)
 +
  {
 +
  if (length + 1 >= cap)reallocate(length + 1);
 +
  size_t id = pos - v;
 +
  if (id >= length)return nullptr;
 +
  for (auto i =id;i < length;++i)
 +
  {
 +
    v[i+1] = v[i];
 +
  }
 +
  ++length;
 +
  v[id] = x;
 +
  return &v[id];
 +
  }
 +
  T* insert(size_t index, const T& x)
 +
  {
 +
  return insert(v + index, x);
 +
  }
 +
  T* erase(const T* pos)
 +
  {
 +
  size_t id = pos - v;
 +
  if (id >= length)return nullptr;
 +
  --length;
 +
  for (unsigned i = id;i < length;++i)
 +
  {
 +
    v[i] = v[i+1];
 +
  }
 +
  return &v[id];
 +
  }
 +
  T* erase(size_t index)
 +
  {
 +
  return erase(v + index);
 +
  }
 +
  ~Vector()
 +
  {
 +
  if (v == nullptr)return;
 +
  free(v);
 +
  }
 +
};
 +
}
 +
int main()
 +
{
 +
    cus::Vector<int> s;
 +
    while(1)
 +
    {
 +
        int a;
 +
        std::cin>>a;
 +
        s.push_back(a);
 +
        std::cout<<s.size()<<" "<<s.capacity()<<std::endl;
 +
    }
 +
std::cout<<"(1)初始化顺序表L;"<<std::endl;
 +
cus::Vector<char> L;
 +
std::cout<<"(2)依次采用尾插法插入a, b, c, d, e元素;"<<std::endl;
 +
char a, b, c, d, e;
 +
std::cin >> a >> b >> c >> d >> e;
 +
L.push_back(a);
 +
L.push_back(b);
 +
L.push_back(c);
 +
L.push_back(d);
 +
L.push_back(e);
 +
std::cout<<"(3)输出顺序表L;"<<std::endl;
 +
std::cout << L << std::endl;
 +
std::cout<<"(4)输出顺序表L长度;"<<std::endl;
 +
std::cout << "size:" << L.size() << std::endl;
 +
std::cout<<"(5)判断顺序表L是否为空;"<<std::endl;
 +
std::cout << "isEmpty:" << L.empty() << std::endl;
 +
std::cout<<"(6)输出顺序表L的第3个元素;"<<std::endl;
 +
std::cout << L << std::endl;
 +
std::cout << "3rd:" << L[2] << std::endl;
 +
std::cout<<"(7)输出元素'a'的位置;"<<std::endl;
 +
std::cout << "a is at:" << L.find_index('a') << std::endl;
 +
std::cout<<"(8)在第4个元素位置上插入'f'元素;"<<std::endl;
 +
L.insert(3, 'f');
 +
std::cout<<"(9)输出顺序表L;"<<std::endl;
 +
std::cout << L << std::endl;
 +
std::cout<<"(10)删除L的第3个元素;"<<std::endl;
 +
L.erase(2);
 +
std::cout<<"(11)输出顺序表L;"<<std::endl;
 +
std::cout << L << std::endl;
 +
std::cout<<"(12)释放顺序表L。"<<std::endl;
 +
system("pause");
 +
return 0;
 +
}
 +
</pre>

2015年9月25日 (五) 20:06的版本

事实证明一个能够自动扩展的顺序表也是可以折腾人的0u0

显示/移除行号
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<cstring>
  4. #include<cmath>
  5. namespace cus {
  6. template<typename T>
  7. class Vector
  8. {
  9. T* v;
  10. size_t length;
  11. size_t cap;
  12. void rearrange(size_t logLen)
  13. {
  14. size_t newLen =( 1 << logLen);
  15. if (newLen > cap)
  16. {
  17. T* from = v;
  18. T* to = static_cast<T*>(malloc(sizeof(T)*newLen));
  19. memcpy(to, from, length);
  20. free(v);
  21. v = to;
  22. }
  23. cap = newLen;
  24. }
  25. void reallocate(size_t n)
  26. {
  27. rearrange(1+ static_cast<int>(log2(n)));
  28. }
  29. public:
  30. Vector() :v(nullptr), length(0), cap(0) {}
  31. Vector(T* ori, size_t len)
  32. {
  33. reallocate(len);
  34. memcpy(v, ori, len);
  35. }
  36. Vector(T* ori, size_t from, size_t len)
  37. {
  38. reallocate(len);
  39. memcpy(&v[from], ori, len);
  40. }
  41. void resize(size_t n)
  42. {
  43. reallocate(n);
  44. for (int i = length;i < n;++i)
  45. {
  46. v[i] = 0;
  47. }
  48. length = n;
  49. }
  50. void push_back(const T &x)
  51. {
  52. if (length + 1 >= cap)reallocate(length + 1);
  53. v[length++] = x;
  54. }
  55. friend std::ostream& operator <<(std::ostream& os,Vector a)
  56. {
  57. for (unsigned i = 0;i < a.size();++i)
  58. {
  59. os << a[i] << " ";
  60. }
  61. return os;
  62. }
  63. inline size_t size() const {
  64. return length;
  65. }
  66. inline size_t capacity() const {
  67. return cap;
  68. }
  69. bool empty() const
  70. {
  71. return size() == 0;
  72. }
  73. T& at(size_t index)
  74. {
  75. if (index >= length)return v[0];
  76. return v[index];
  77. }
  78. T& operator [](size_t index)
  79. {
  80. if (index >= length)return v[0];
  81. return v[index];
  82. }
  83. T* find(const T &x) const
  84. {
  85. for (unsigned i = 0;i < length;++i)
  86. {
  87. if (v[i] == x)
  88. {
  89. return &v[i];
  90. }
  91. }
  92. return nullptr;
  93. }
  94. size_t find_index(const T &x) const
  95. {
  96. auto rtn = find(x);
  97. if (rtn == nullptr)return static_cast<size_t>(-1);
  98. return x;
  99. }
  100. T* insert(const T* pos,const T& x)
  101. {
  102. if (length + 1 >= cap)reallocate(length + 1);
  103. size_t id = pos - v;
  104. if (id >= length)return nullptr;
  105. for (auto i =id;i < length;++i)
  106. {
  107. v[i+1] = v[i];
  108. }
  109. ++length;
  110. v[id] = x;
  111. return &v[id];
  112. }
  113. T* insert(size_t index, const T& x)
  114. {
  115. return insert(v + index, x);
  116. }
  117. T* erase(const T* pos)
  118. {
  119. size_t id = pos - v;
  120. if (id >= length)return nullptr;
  121. --length;
  122. for (unsigned i = id;i < length;++i)
  123. {
  124. v[i] = v[i+1];
  125. }
  126. return &v[id];
  127. }
  128. T* erase(size_t index)
  129. {
  130. return erase(v + index);
  131. }
  132. ~Vector()
  133. {
  134. if (v == nullptr)return;
  135. free(v);
  136. }
  137. };
  138. }
  139. int main()
  140. {
  141. cus::Vector<int> s;
  142. while(1)
  143. {
  144. int a;
  145. std::cin>>a;
  146. s.push_back(a);
  147. std::cout<<s.size()<<" "<<s.capacity()<<std::endl;
  148. }
  149. std::cout<<"(1)初始化顺序表L;"<<std::endl;
  150. cus::Vector<char> L;
  151. std::cout<<"(2)依次采用尾插法插入a, b, c, d, e元素;"<<std::endl;
  152. char a, b, c, d, e;
  153. std::cin >> a >> b >> c >> d >> e;
  154. L.push_back(a);
  155. L.push_back(b);
  156. L.push_back(c);
  157. L.push_back(d);
  158. L.push_back(e);
  159. std::cout<<"(3)输出顺序表L;"<<std::endl;
  160. std::cout << L << std::endl;
  161. std::cout<<"(4)输出顺序表L长度;"<<std::endl;
  162. std::cout << "size:" << L.size() << std::endl;
  163. std::cout<<"(5)判断顺序表L是否为空;"<<std::endl;
  164. std::cout << "isEmpty:" << L.empty() << std::endl;
  165. std::cout<<"(6)输出顺序表L的第3个元素;"<<std::endl;
  166. std::cout << L << std::endl;
  167. std::cout << "3rd:" << L[2] << std::endl;
  168. std::cout<<"(7)输出元素'a'的位置;"<<std::endl;
  169. std::cout << "a is at:" << L.find_index('a') << std::endl;
  170. std::cout<<"(8)在第4个元素位置上插入'f'元素;"<<std::endl;
  171. L.insert(3, 'f');
  172. std::cout<<"(9)输出顺序表L;"<<std::endl;
  173. std::cout << L << std::endl;
  174. std::cout<<"(10)删除L的第3个元素;"<<std::endl;
  175. L.erase(2);
  176. std::cout<<"(11)输出顺序表L;"<<std::endl;
  177. std::cout << L << std::endl;
  178. std::cout<<"(12)释放顺序表L。"<<std::endl;
  179. system("pause");
  180. return 0;
  181. }

著作权声明[编辑]

关于[编辑]