fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <fstream>
  4. #include <memory>
  5. #include <iterator>
  6. #include <utility>
  7.  
  8. using std::cout;
  9. using std::endl;
  10. using std::allocator;
  11. using std::sort;
  12. using std::move;
  13. using std::ostream;
  14. using std::initializer_list;
  15. using std::reverse;
  16. using std::remove;
  17.  
  18. class Point
  19. {
  20. private:
  21. int x;
  22. int y;
  23. public:
  24. Point() : x(0), y(0){}
  25. Point(int x1) : x(x1), y(0){}
  26. Point(int x1, int y1) : x(x1), y(y1){}
  27. Point(const Point& p) : x(p.x), y(p.y){}
  28. ~Point(){}
  29. Point& operator=(const Point& other)
  30. {
  31. this->x = other.x;
  32. this->y = other.y;
  33. return *this;
  34. }
  35. Point& operator=(Point&& other)
  36. {
  37. this->x = move(other.x);
  38. this->y = move(other.y);
  39. return *this;
  40. }
  41. friend ostream& operator << (ostream & out, Point p)
  42. {
  43. out << "(" << p.x << ", " << p.y << ")";
  44. return out;
  45. }
  46. bool operator == (Point p)
  47. {
  48. return (x == p.x && y == p.y);
  49. }
  50. bool operator < (Point p)
  51. {
  52. return (x*x + y*y < p.x*p.x + p.y*p.y);
  53. }
  54. };
  55.  
  56. template <class T>
  57. class Vector
  58. {
  59. private:
  60. T* ptr;
  61. size_t size;
  62. size_t capacity;
  63. std::allocator<T> a1;
  64. public:
  65. Vector() : size(0), capacity(0), ptr(nullptr){}
  66. Vector(size_t size) : size(size), capacity(size * 2)
  67. {
  68. ptr = a1.allocate(capacity);
  69. for (size_t i = 0; i < size; i++)
  70. {
  71. try
  72. {
  73. a1.construct(ptr + i, T());
  74. }
  75. catch (...)
  76. {
  77. clean();
  78. throw;
  79. }
  80. }
  81. }
  82. Vector(size_t size, T& value) : size(size), capacity(size * 2)
  83. {
  84. ptr = a1.allocate(capacity);
  85. for (size_t i = 0; i < size; i++)
  86. {
  87. try
  88. {
  89. a1.construct(ptr + i, value);
  90. }
  91. catch (...)
  92. {
  93. clean();
  94. throw;
  95. }
  96. }
  97. }
  98. Vector(const Vector& v) : size(v.size), capacity(v.capacity)
  99. {
  100. this->ptr = a1.allocate(capacity);
  101. for (size_t i = 0; i < size; i++)
  102. {
  103. try
  104. {
  105. a1.construct(ptr + i, v.ptr[i]);
  106. }
  107. catch (...)
  108. {
  109. clean();
  110. throw;
  111. }
  112. }
  113. }
  114. Vector(initializer_list<T> l)
  115. {
  116. size = l.size();
  117. ptr = a1.allocate(size * 2);
  118. capacity = size * 2;
  119. size_t index = 0;
  120. for (const auto& item : l)
  121. {
  122. a1.construct(ptr + index, item);
  123. index++;
  124. }
  125. }
  126. ~Vector()
  127. {
  128. for (size_t i = 0; i < size; i++)
  129. {
  130. a1.destroy(ptr + i);
  131. }
  132. a1.deallocate(ptr, capacity);
  133. }
  134. void print()
  135. {
  136. for (size_t i = 0; i < size; i++)
  137. {
  138. cout << ptr[i] << "; ";
  139. }
  140. cout << endl;
  141. }
  142. void clean()
  143. {
  144. for (size_t i = 0; i < size; i++)
  145. {
  146. a1.destroy(ptr + i);
  147. }
  148. size = 0;
  149. }
  150. Vector& operator=(const Vector& other)
  151. {
  152. for (size_t i = 0; i < size; i++)
  153. {
  154. a1.destroy(ptr + i);
  155. }
  156. a1.deallocate(ptr, capacity);
  157. this->size = other.size;
  158. this->capacity = other.capacity;
  159. this->ptr = a1.allocate(capacity);
  160. for (size_t i = 0; i < size; i++)
  161. {
  162. a1.construct(ptr + i, other.ptr[i]);
  163. }
  164. return *this;
  165. }
  166. Vector& operator=(Vector&& other)
  167. {
  168. this->size = move(other.size);
  169. this->capacity = move(other.capacity);
  170. ptr = other.ptr;
  171. other.size = 0;
  172. other.capacity = 0;
  173. other.ptr = nullptr;
  174. return *this;
  175. }
  176. T& operator [] (size_t index)
  177. {
  178. if (index < 0 || index >= size)
  179. throw std::out_of_range("Error");
  180. return ptr[index];
  181. }
  182. const T& operator [] (size_t index) const
  183. {
  184. if (index < 0 || index >= size)
  185. throw std::out_of_range("Error");
  186. return ptr[index];
  187. }
  188. void push_back(T value)
  189. {
  190. if (size < capacity)
  191. {
  192. a1.construct(ptr + size, value);
  193. size++;
  194. }
  195. else
  196. {
  197. capacity = size * 2;
  198. T* temp_ptr = a1.allocate(capacity);
  199. for (size_t i = 0; i < size; i++)
  200. {
  201. a1.construct(temp_ptr + i, ptr[i]);
  202. }
  203. for (size_t i = 0; i < size; i++)
  204. {
  205. a1.destroy(ptr + i);
  206. }
  207. a1.deallocate(ptr, size);
  208. a1.construct(temp_ptr + size, value);
  209. size++;
  210. ptr = temp_ptr;
  211. }
  212.  
  213. }
  214. void pop_back()
  215. {
  216. if (size == 0)
  217. throw std::out_of_range("Error");
  218. a1.destroy(ptr + size - 1);
  219. size--;
  220. }
  221. void resize(size_t new_size, T value = T())
  222. {
  223. if (size >= new_size)
  224. {
  225. for (size_t i = new_size; i < size; i++)
  226. {
  227. this->pop_back();
  228. }
  229. }
  230. else
  231. {
  232. for (size_t i = size; i < new_size; i++)
  233. {
  234. this->push_back(value);
  235. }
  236. }
  237. }
  238. T* begin()
  239. {
  240. return ptr;
  241. }
  242. T* end()
  243. {
  244. return ptr + size;
  245. }
  246. void insert(T* p, T value)
  247. {
  248. if (p > this->end() || p < this->begin())
  249. throw std::out_of_range("Error");
  250. size_t index = 0, index_1 = 0;
  251. for (T* i = this->begin(); i < this->end(); i++)
  252. {
  253. if (i < p)
  254. {
  255. index++;
  256. index_1 = index;
  257. }
  258. else
  259. {
  260. index_1++;
  261. }
  262. }
  263. if (size == capacity)
  264. {
  265. capacity *= 2;
  266. }
  267. size++;
  268. T* temp_ptr = a1.allocate(capacity);
  269. for (size_t i = 0; i < index; i++)
  270. {
  271. a1.construct(temp_ptr + i, ptr[i]);
  272. }
  273. a1.construct(temp_ptr + index, value);
  274. index++;
  275. for (size_t i = index; i < size; i++)
  276. {
  277. a1.construct(temp_ptr + i, ptr[i - 1]);
  278. }
  279. for (size_t i = 0; i < size - 1; i++)
  280. {
  281. a1.destroy(ptr + i);
  282. }
  283. a1.deallocate(ptr, size - 1);
  284. ptr = temp_ptr;
  285. }
  286. void insert(T* p, T* first, T* last)
  287. {
  288. if (p > this->end() || p < this->begin())
  289. throw std::out_of_range("Error");
  290. if (first > last)
  291. throw std::out_of_range("Error");
  292. size_t index = 0, index_1 = 0;
  293. for (T* i = this->begin(); i < this->end(); i++)
  294. {
  295. if (i < p)
  296. {
  297. index++;
  298. index_1 = index;
  299. }
  300. else
  301. {
  302. index_1++;
  303. }
  304. }
  305. capacity = 10000;
  306. T* temp_ptr = a1.allocate(capacity);
  307. for (size_t i = 0; i < index; i++)
  308. {
  309. a1.construct(temp_ptr + i, ptr[i]);
  310. }
  311. size_t index_2 = index;
  312. for (T* i = first; i < last; i++)
  313. {
  314. a1.construct(temp_ptr + index_2, *i);
  315. index_2++;
  316. }
  317. for (size_t i = index; i < size; i++)
  318. {
  319. a1.construct(temp_ptr + index_2, ptr[i]);
  320. index_2++;
  321. }
  322. for (size_t i = 0; i < size; i++)
  323. {
  324. a1.destroy(ptr + i);
  325. }
  326. a1.deallocate(ptr, size);
  327. size = index_2;
  328. ptr = temp_ptr;
  329. }
  330. void erase(T*p)
  331. {
  332. if (p > this->end() || p < this->begin())
  333. throw std::out_of_range("Error");
  334. size_t index = 0, index_1 = 0;
  335. for (T* i = this->begin(); i < this->end(); i++)
  336. {
  337. if (i < p)
  338. {
  339. index++;
  340. index_1 = index;
  341. }
  342. else
  343. {
  344. index_1++;
  345. }
  346. }
  347. index++;
  348. for (size_t i = index; i < size; i++)
  349. {
  350. ptr[i - 1] = ptr[i];
  351. }
  352. a1.destroy(ptr + size - 1);
  353. size--;
  354. }
  355. void erase(T* first, T* last)
  356. {
  357. if (first > this->end() || first < this->begin())
  358. throw std::out_of_range("Error");
  359. if (last > this->end() || last < this->begin())
  360. throw std::out_of_range("Error");
  361. if (first > last)
  362. throw std::out_of_range("Error");
  363. size_t index = 0, index_1 = 0, index_2 = 0;
  364. for (T* i = this->begin(); i < this->end(); i++)
  365. {
  366. if (i < first)
  367. {
  368. index++;
  369. index_1 = index;
  370. }
  371. else
  372. {
  373. if (i > last)
  374. {
  375. index_1++;
  376. }
  377. else
  378. {
  379. index_1++;
  380. index_2 = index_1;
  381. }
  382. }
  383. }
  384. size_t temp = index;
  385. for (size_t i = index_2; i < size; i++)
  386. {
  387. ptr[temp] = ptr[i];
  388. temp++;
  389. }
  390. for (size_t i = temp; i < size; i++)
  391. {
  392. a1.destroy(ptr + i);
  393. }
  394. size -= (index_2 - index);
  395. }
  396. size_t Size() const
  397. {
  398. return size;
  399. }
  400. template<class... T1>
  401. void emplace_back(T1&&... args)
  402. {
  403. if (size < capacity)
  404. {
  405. std::allocator_traits<std::allocator<T>>::construct(a1, ptr + size, std::forward<T1>(args)...);
  406. size++;
  407. }
  408. else
  409. {
  410. capacity = size * 2;
  411. T* temp_ptr = a1.allocate(capacity);
  412. for (size_t i = 0; i < size; i++)
  413. {
  414. a1.construct(temp_ptr + i, ptr[i]);
  415. }
  416. for (size_t i = 0; i < size; i++)
  417. {
  418. a1.destroy(ptr + i);
  419. }
  420. a1.deallocate(ptr, size);
  421. std::allocator_traits<std::allocator<T>>::construct(a1, temp_ptr + size, std::forward<T1>(args)...);
  422. size++;
  423. ptr = temp_ptr;
  424. }
  425. }
  426. };
  427.  
  428. int main()
  429. {
  430. Vector <Point> v2(5);
  431. Vector <Point> v3(7, Point(1, 1));
  432. Vector <Point> v4 = v3;
  433. Vector <Point> v5;
  434. cout << "v2(5), v2 = ";
  435. v2.print();
  436. cout << "v3(7,2), v3 = ";
  437. v3.print();
  438. cout << "v4=v3, v4 = ";
  439. v4.print();
  440. cout << "v5(), v5 = ";
  441. v5.print();
  442. v4 = move(v2);
  443. cout << "v4 = move(v2)" << endl << "v4 = ";
  444. v4.print();
  445. v4 = v5;
  446. cout << "v4 = v5" << endl << "v4 = ";
  447. v4.print();
  448. cout << "v3[1] = " << v3[1] << endl;
  449. Point a(1, 1);
  450. cout << "a = " << a << endl;
  451. Vector <Point> v1(3, Point(2, 2));
  452. cout << "v1 = ";
  453. v1.print();
  454. cout << "v1.pop_back()" << endl;
  455. v1.pop_back();
  456. cout << "v1 = ";
  457. v1.print();
  458. cout << "v1.push_back(Point(1,1))" << endl;
  459. v1.push_back(Point(1, 1));
  460. cout << "v1 = ";
  461. v1.print();
  462. cout << "v1.push_back(Point(3,3))" << endl;
  463. v1.push_back(Point(3, 3));
  464. cout << "v1 = ";
  465. v1.print();
  466. cout << "reverse(v1.begin(), v1.end() - 1)" << endl;
  467. reverse(v1.begin(), v1.end() - 1);
  468. cout << "v1 = ";
  469. v1.print();
  470. cout << "v1.resize(v1.size_of_vector() - 1)" << endl;
  471. v1.resize(v1.Size() - 1);
  472. cout << "v1 = ";
  473. v1.print();
  474. cout << "v1.resize(v1.size_of_vector() + 3)" << endl;
  475. v1.resize(v1.Size() + 3);
  476. cout << "v1 = ";
  477. v1.print();
  478. cout << "v1.insert(v1.begin()+2, a)" << endl;
  479. v1.insert(v1.begin() + 2, a);
  480. cout << "v1 = ";
  481. v1.print();
  482. Point myarray[2] = { { 3, 4 }, { 5, 6 } };
  483. cout << "myarray[2] = {{ 3, 4 }, { 5, 6 } }" << endl;
  484. cout << "v1.insert(v1.begin() + 1, myarray, myarray+2)" << endl;
  485. v1.insert(v1.begin() + 1, myarray, myarray + 2);
  486. cout << "v1 = ";
  487. v1.print();
  488. cout << "v1.erase(v1.begin() + 1)" << endl;
  489. v1.erase(v1.begin() + 1);
  490. cout << "v1 = ";
  491. v1.print();
  492. cout << "v1.erase(v1.end()-2, v1.end())" << endl;
  493. v1.erase(v1.end() - 2, v1.end());
  494. cout << "v1 = ";
  495. v1.print();
  496. cout << "v1.erase(remove(v1.begin(), v1.end(), Point{ 2, 2 }), v1.end())" << endl;
  497. v1.erase(remove(v1.begin(), v1.end(), Point{ 2, 2 }), v1.end());
  498. cout << "v1 = ";
  499. v1.print();
  500. cout << "sort(v1.begin(), v1.end())" << endl;
  501. sort(v1.begin(), v1.end());
  502. cout << "v1 = ";
  503. v1.print();
  504. Vector<Point> v = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
  505. cout << "Vector<Point> v = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }" << endl;
  506. cout << "v = ";
  507. v.print();
  508. v.emplace_back(10, 10);
  509. cout << "v.emplace_back(10, 10)" << endl;
  510. cout << "v = ";
  511. v.print();
  512. return 0;
  513. }
Compilation error #stdin compilation error #stdout 0s 3480KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:431:34: error: invalid initialization of non-const reference of type 'Point&' from an rvalue of type 'Point'
  Vector <Point> v3(7, Point(1, 1));
                                  ^
prog.cpp:82:2: note:   initializing argument 2 of 'Vector<T>::Vector(size_t, T&) [with T = Point; size_t = unsigned int]'
  Vector(size_t size, T& value) : size(size), capacity(size * 2)
  ^
prog.cpp:451:34: error: invalid initialization of non-const reference of type 'Point&' from an rvalue of type 'Point'
  Vector <Point> v1(3, Point(2, 2));
                                  ^
prog.cpp:82:2: note:   initializing argument 2 of 'Vector<T>::Vector(size_t, T&) [with T = Point; size_t = unsigned int]'
  Vector(size_t size, T& value) : size(size), capacity(size * 2)
  ^
stdout
Standard output is empty