c++ vector notes

c++ vector notes - { this -> data= new int[size+...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
vector.h 1. Should maintain an ordered sequence of integers 2. Should expose the following interface a) insert(int) - add arg to end b) elementAt(int loc) - returns value stored @ loc c) modify(int loc, int new val) - update d) getSize() - since arrays don't know how big they are need 3 process directives so that it doesn't declare more than once in the main.cpp " " search local directory for file < > search the system files _____________________________________________________ vector.h #ifndef VECTOR_H #define VECTOR_H class vector { private: int size; int * data; int capacity; void resize(); public: vector(); vector(int [ ], int size); void insert(int data); int elementAt(int loc, int&val); int modify(int loc, int newVal); int getSize(); }; #endif _____________________________________________________ vector.cpp #include "vector.h"
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
vector::vector() { data= new int[10]; size= 0; capacity= 10; } vector::vector(int data[ ], int size)
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: { this -&gt; data= new int[size+ 10]; this -&gt; size= size; capacity= size + 10; for(int i = 0; i&lt;size; i++) { (this -&gt; data)[i] = data[i]; } } void vector::insert(int item) { if(size == capacity) { resize(); } date[size]=item; size++; } int vector::elementAt(int loc, int&amp;val) // location, pass by reference of the value { if(location&lt;0 || location &gt;= size) return -1; else{ val = data[location]; // sets val equal to whatever the user was trying to see. .. they now have the value to cout or whatever they want return 0; } } int vector:: modify(int loc, int newVal) { if(loc &lt; 0 || loc &gt;= size) return -1 else{ data[loc] = newVal; return 0; } } int vector:: getSize() { return size; } void vector:: resize() { int * temp= new int[capacity + 10]; for(int i=0; i&lt;capacity; i++) { temp[i] = data[i]; } capacity+=10; delete data; data= temp; } #include &lt;iostream&gt;...
View Full Document

Page1 / 3

c++ vector notes - { this -&amp;amp;gt; data= new int[size+...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online