This repository was archived by the owner on Apr 4, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +130
-0
lines changed
Expand file tree Collapse file tree 3 files changed +130
-0
lines changed Original file line number Diff line number Diff line change 1+ #ifndef HEADER_H
2+ #define HEADER_H
3+
4+ #include < iostream>
5+ #include < string>
6+
7+ using std::cout;
8+ using std::cin;
9+ using std::endl;
10+
11+ namespace sion
12+ {
13+ template <typename type>
14+ class vector
15+ {
16+ private:
17+ type* elements;
18+ size_t max_size;
19+ size_t size;
20+
21+ public:
22+ vector ()
23+ {
24+ max_size = 10 ;
25+ size = 0 ;
26+ elements = (type*)(std::malloc (max_size * sizeof (type)));
27+ }
28+
29+ ~vector ()
30+ {
31+ std::free (elements);
32+ }
33+
34+ int push_back (type item)
35+ {
36+ try
37+ {
38+ if (size == max_size)
39+ {
40+ max_size *= 2 ;
41+ elements = (type*)(std::realloc (elements, max_size * sizeof (type)));
42+ elements[size] = item;
43+ size += 1 ;
44+ return 0 ;
45+ }
46+ else
47+ {
48+ elements[size] = item;
49+ size += 1 ;
50+ return 0 ;
51+ }
52+ }
53+ catch (...)
54+ {
55+ return 1 ;
56+ }
57+ }
58+
59+ size_t length ()
60+ {
61+ return size;
62+ }
63+
64+ type pop_back ()
65+ {
66+ if (size == 0 )
67+ {
68+ type t;
69+ return t;
70+ }
71+ else
72+ {
73+ type t = elements[size - 1 ];
74+ elements[size - 1 ] = NULL ;
75+ size -= 1 ;
76+ return t;
77+ }
78+ }
79+
80+ type& operator [](size_t index)
81+ {
82+ if (index >= size)
83+ {
84+ cout << " Error. Out of bounds." << endl;
85+ exit (-1 );
86+ }
87+ else
88+ {
89+ return elements[index];
90+ }
91+ }
92+ };
93+ }
94+
95+ #endif // HEADER_H
Original file line number Diff line number Diff line change 1+ {
2+ "project-name" : " custom-cpp-vector" ,
3+ "version" : " v1.0.0" ,
4+ "description" : " Making a custom vector implementation." ,
5+ "author" : " Sion Pixley" ,
6+ "license" : " MIT; LICENSE" ,
7+ "main-file" : " main.cpp" ,
8+ "dependencies" : {
9+ "c++" : " 14"
10+ },
11+ "dev-dependencies" : {
12+ "clang" : " v11.x"
13+ },
14+ "scripts" : {
15+ "build" : " clang++ -O2 -std=c++14 main.cpp -o main" ,
16+ "start" : " ./main" ,
17+ "timed-start" : " time ./main"
18+ }
19+ }
20+
Original file line number Diff line number Diff line change 1+ #include " header.h"
2+
3+ int main ()
4+ {
5+ sion::vector<std::string> v;
6+ for (int i = 0 ; i < 100000 ; i += 1 )
7+ {
8+ v.push_back (" hello" );
9+ }
10+ for (int i = 0 ; i < v.length (); i += 1 )
11+ {
12+ cout << v[i] << endl;
13+ }
14+ return 0 ;
15+ }
You can’t perform that action at this time.
0 commit comments