xxxxxxxxxx
#include <iostream>
#include <vector>
using namespace std;
int main()
{
// Create a vector containing integers
vector<int> v = { 7, 5, 16, 8 };
v.push_back(25); // Adds 25 to the contents of the vector
v.push_back(13); // Adds 13 to the contents of the vector
// Print out the contents of the vector
cout << "v = { ";
for (int n : v) {
std::cout << n << ", ";
}
cout << "}; \n";
}
xxxxxxxxxx
#include <iostream>
#include <vector>
int main()
{
// Create a vector containing integers
std::vector<int> v = { 7, 5, 16, 8 };
// Add two more integers to vector
v.push_back(25);
v.push_back(13);
// Print out the vector
std::cout << "v = { ";
for (int n : v) {
std::cout << n << ", ";
}
std::cout << "}; \n";
}
C++ vectors are like dynamic arrays. Vectors can be included in a library called "Vector". It is simple to edit and insert items in vectors and any item can easily be added or deleted.
xxxxxxxxxx
//Here is an example, i am going to include the library responsible for including the vector function.
//note: if you arent aware, libraries are like the header files you include before starting your program like python.
#include <iostream>//input output stream has the cout(console out) and cin(console in) function
#include <vector>
//PLEASE NOTE I DONT BRING STDL TO NAMESPACE BECAUSE I DONT WANT MERGE CONFLICT ERRORS LIKE for example, if i name an item cin our cout, it will have an error because there are two items in the same namespace with the same name. The compiler goes confused and boom
int main() {
//this is the main function where the code is executed
std::vector<int> myVect{1,2,3,4};//i created an integer vector, you have to place the data type after std::vector in the <>
myVect.push_back(6);
//this makes it adds 6 to the end of the array.
//dont try to print it cuz u cant, try to loop through all the items and print items individually.
return 0;//exit code
}
//https://www.programiz.com/cpp-programming/vectors visit this website for more details and if you are confused
xxxxxxxxxx
//
// Created by merom on 5/4/2023.
//
#ifndef HOMEWORK_NO_SUBMISSION_MYVECTOR_IMP_H
#define HOMEWORK_NO_SUBMISSION_MYVECTOR_IMP_H
#include <exception>
#include "MyVector.h"
template<class T>
template<class Iter>
Vector<T>::Vector(Iter begin, Iter end):size_(std::distance(begin,end)), capacity_(size_), data_(new T[size_],std::default_delete<T[]>()){
std::copy(begin,end,data_.get());
}
template<class T>
Vector<T>::Vector():size_(0),capacity_(0),data_(nullptr){
}
template<class T>
Vector<T>:: Vector(int size):size_(size),capacity_(size),data_(new T[size], std::default_delete<T[]>()) {
std::for_each(begin(),end(), []( T & val){val = T();});
}
template<class T>
Vector<T>::Vector(const Vector &other):size_(other.size_),capacity_(other.capacity_),data_(std::make_shared<T[]>(new T[other.size_], std::default_delete<T[]>())) {
std::copy(other.data_.get(), other.data_.get() + other.size_, data_.get());
}
template<class T>
Vector<T> &Vector<T>::operator=(const Vector &other) {
if (this != &other) {
std::swap(*this, Vector{ other });
}
return *this;
}
template<class T>
Vector<T>::Vector(Vector &&other) noexcept:data_(std::move(other.data_)),size_(other.size_),capacity_(other.capacity_){
other.data_.reset();
other.size_ = 0;
other.capacity_ = 0;
}
template<class T>
Vector<T> &Vector<T>::operator=(Vector &&other) noexcept {
if (this != &other) {
data_ = std::move(other.data);
size_ = other.size_;
capacity_ = other.capacity_;
other.data.reset();
other.size_ = 0;
other.capacity_ = 0;
}
return *this;
}
template<class T>
void Vector<T>::reserve(size_t new_capacity){
if(new_capacity > capacity_){
std::shared_ptr<T[]> new_data(new T[new_capacity], std::default_delete<T[]>());
std::copy(data_.get(), data_.get() + size_, new_data.get());
data_ = std::move(new_data);
capacity_ = new_capacity;
}
}
template<class T>
void Vector<T>::push_back(const T &value) {
if (size_ == capacity_) {
reserve(capacity_ == 0 ? 1 : 2 * capacity_);
}
/*Same as data_.get() + size = new T(value) */
new (data_.get() + size_) T(value);
size_++;
}
template<class T>
void Vector<T>::emplace_back( T &&value) {
if (size_ == capacity_) {
reserve(capacity_ == 0 ? 1 : 2 * capacity_);
}
/*Same as data_.get() + size = new val */
new (data_.get() + size_) T(value);
size_++;
}
template<class T>
void Vector<T>::print(const Vector::iter first, const Vector::iter last, const char *lb , const char *sep, std::ostream &os) {
if(lb != nullptr && *lb != '\0')
os << lb << ": " << sep;
using type = decltype(*first); // typedef typename std::iterator_traits<T>::type T;
std::copy(first, last,std::ostream_iterator<type>(os,sep));
os << std::endl;
}
template<class T>
T & Vector<T>::at(int i) {
if( i >= size_ || i < 0){
throw std::out_of_range("index out of range");
}
return operator[](i);
}
template<class T>
void Vector<T>::erase() {
data_.reset(); // release the shared pointer, deallocating the array
size_ = 0; // reset the size to zero
capacity_ = 0;
}
#endif //HOMEWORK_NO_SUBMISSION_MYVECTOR_IMP_H
xxxxxxxxxx
//
// Created by merom on 5/4/2023.
//
#ifndef HOMEWORK_NO_SUBMISSION_MYVECTOR_H
#define HOMEWORK_NO_SUBMISSION_MYVECTOR_H
#include <memory>
#include <algorithm>
#include <iterator>
template<class T>
class Vector {
public:
/*ctr's*/
Vector();
explicit Vector(int size);
Vector(const Vector & other) ;
Vector(Vector && other) noexcept ;
Vector<T> & operator =(const Vector & other);
Vector<T> & operator =(Vector && other) noexcept ;
~Vector()=default;
/*Random access*/
template<class Iter>
Vector(Iter begin, Iter end);
public:
void push_back(const T& value);
void emplace_back( T&& value);
// Accessors
size_t size() const {
return size_;
}
T& at(int i);
size_t capacity() const {
return capacity_;
}
// Index operators
T& operator[](size_t index) {
return data_[index];
}
const T& operator[](size_t index) const {
return data_[index];
}
[[nodiscard]] bool is_empty() const {return data_.size == 0;}
void erase();
template <class comp>
void sort(comp cm);
public:
class iter {
public:
using value_type = T;
using reference = T&;
using pointer = T*;
using difference_type = std::ptrdiff_t;
using iterator_category = std::random_access_iterator_tag;
explicit iter( pointer ptr) : ptr_(ptr) {}
typename std::iterator_traits<iter>::reference operator*() const { return *ptr_; }
pointer operator->() const { return ptr_; }
iter& operator++() {
++ptr_;
return *this;
}
const iter operator++(int) {
iter temp = *this;
++ptr_;
return temp;
}
iter& operator--() {
--ptr_;
return *this;
}
iter operator--(int) {
iter temp = *this;
--ptr_;
return temp;
}
iter& operator+=(difference_type n){
ptr_ += n;
return *this;
}
iter operator+(difference_type n) {
iter temp = *this;
return temp+=n;
}
iter operator+(difference_type n) const {
iter temp = *this;
return temp += n;
}
friend iter operator+(difference_type n, iter const& it) {
return it + n;
}
iter& operator-=(difference_type n) {
return *this += -n;
}
iter operator-(difference_type n) const {
iter temp = *this;
return temp -= n;
}
difference_type operator-(iter const& other) const {
return ptr_ - other.ptr_;
}
reference operator[](difference_type n) const {
return *(*this + n);
}
bool operator==(iter const& other) const {
return ptr_ == other.ptr_;
}
bool operator!=(iter const& other) const {
return ptr_ != other.ptr_;
}
bool operator<(iter const& other) const {
return ptr_ < other.ptr_;
}
bool operator>(iter const& other) const {
return ptr_ > other.ptr_;
}
bool operator<=(iter const& other) const {
return ptr_ <= other.ptr_;
}
bool operator>=(iter const& other) const {
return ptr_ >= other.ptr_;
}
private:
pointer ptr_;
};
// Other Vector class members...
iter begin() {
return iter(data_.get());
}
iter end() {
return iter((data_.get() + size_));
}
public:
void print(const iter first , const iter last , const char* lb = "",
const char* sep = "\n",
std::ostream& os = std::cout);
private:
size_t size_{};
size_t capacity_{};
std::shared_ptr<T[]> data_;
void reserve(size_t new_capacity);
};
template<class T>
template<class comp>
void Vector<T>::sort(comp _cmp) {
std::sort(Vector<T>::begin(),Vector::end(), _cmp);
}
#include "MyVector_imp.h"
#endif //HOMEWORK_NO_SUBMISSION_MYVECTOR_H
xxxxxxxxxx
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> v1 ={1,2,3,4,5};
vector<int> v2{1,2,3,4};
vector<int> v3(4,11);
cout << "vector1" <<" ";
for(int i:v1){
cout << i << " ";
}
for(int i:v1){
cout << i << " ";
}
for(int i:v1){
cout << i << " ";
}
}