Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/m1ad3n/cvec
Vectors in c
https://github.com/m1ad3n/cvec
arrays c cmake console dynamic-memory-allocation library make vector vectors
Last synced: about 9 hours ago
JSON representation
Vectors in c
- Host: GitHub
- URL: https://github.com/m1ad3n/cvec
- Owner: m1ad3n
- License: mit
- Created: 2024-03-09T10:00:08.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2024-03-10T17:48:10.000Z (10 months ago)
- Last Synced: 2024-11-09T02:39:03.579Z (about 2 months ago)
- Topics: arrays, c, cmake, console, dynamic-memory-allocation, library, make, vector, vectors
- Language: C
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CVec: Vector Library in C
CVec is a simple C library for working with dynamic arrays, providing functionality similar to vectors in other programming languages. It allows you to create, manipulate, and free dynamic arrays efficiently.
## Functions
- `new_cvec`: Create a new CVec with an initial capacity.
- `cvec_from`: Create a new CVec from an existing array.
- `cvec_free`: Free memory allocated for a CVec.
- `cvec_push`: Append an element to the end of the CVec.
- `cvec_pop`: Remove and return the last element of the CVec.
- `cvec_resize`: Resize the capacity of the CVec.
- `cvec_remove`: Remove an element from the CVec at a specified index.
- `cvec_insert`: Insert an element at a specified index in the CVec.
- `cvec_clean`: Remove all elements from the CVec.
- `cvec_front`: Get the first element of the CVec.
- `cvec_back`: Get the last element of the CVec.
- `cvec_at`: Get the element at a specified index in the CVec.
- `cvec_iter`: Iterator for traversing the elements of the CVec.## Installation
- **Create a build directory**: `mkdir build && cd build`
- **Build from source**: `cmake .. && make`
- **Install all the files**: `sudo make install`## Usage
- **Include the Header File**: Include `cvec.h` in your C source file.
- **Compile with Your Project**: Compile your project with -lcvec.
- **Use the Functions**: Start using the CVec functions in your code.## CMake Usage
- **Link the library**: `target_link_libraries(ExecName PRIVATE cvec)`
## Example
```c
#include
#includeint main() {
// Create a new CVec
cvec* vec = new_cvec();
// cvec_from(3, 10, 20, 30) - first argument is number of the elements// Push elements into the CVec
cvec_push(vec, 10);
cvec_push(vec, 20);
cvec_push(vec, 30);// Print elements
for (int i = 0; i < vec->size; i++) {
printf("%d ", cvec_iter(vec));
// or vec->items[i]
}// Free memory
cvec_free(vec);return 0;
}
```## License
This project is licensed under the MIT License