I'm new to C++ so bare with me please.
Here is the vec.h file that contains (class Vec), all its methods and two attributes. Below the class are three inline functions.
Code:
class Vec
{
public:
Vec( ); // constructor
void push_back( int ); // similar to vector
void pop_back( ); // remove last element in Vec
void erase( int idx ); // erase element at position idx
// optional: // erase howmany elements starting at idx
/*void erase( int idx, int howmany );*/
// insert a value at position idx
void insert( int idx, int value );
int size( ) const; // number of elements in array
// use operator [ ] as in a vector
int operator[ ]( int ) const;
private:
int array[ 5 ]; // local attribute
int loc; // local index
};
inline Vec::Vec( ) // constructor
{
cout << "in Vec::Vec( )\n";
loc = 0; // loc refers to the next empty location
}
// size of the object
inline int Vec::size( ) const
{
return loc;
}
// return a copy of element at index i
inline int Vec::operator [ ]( int i ) const
{
if( i >= 0 && i < loc )
{ // i is within the range of elements in array
return array[ i ];
}
else
{
return -1; // invalid index; return -1
}
}
I am trying to create vec.cpp that contains the definitions for (class Vec) methods. Push_back and pop_back seem to work ok. I'm having some issues with the proper syntax for erase. I'm sure i will have issues with insert as well.
Code:
// methods for class Vec
#include<iostream>
#include<iomanip>
using namespace std;
#include "vec.h"
void Vec::push_back(int i)
{
if( loc >= 5 )
{
cerr << "object of type Vec is full...\n";
}
else
{
cout << "push_back( " << i << " )\t at location "
<< loc << '\n';
array[ loc ] = i;
++loc; // advance to the next empty location
}
}
void Vec::pop_back()
{
--loc;
cout << "pop_back( ) " << array[ loc ]
<< " at location " << loc << '\n';
return;
}
void Vec::erase(int idx)
{
cout << "erase ( 0 )" << endl;
return;
}
void Vec::insert(int idx, int howmany)
{
cout << "insert( 0, 60 )" << endl;
array[0] = 60;
}
In methods for class Vec I need erase to point to the location of the first element of the array and remove it. i tried using begin() + n int the method definition but had syntax/errors. Not sure how to tackle this. Ive used begin() + n successfully in other programs, so I'm not sure if I used it correctly here.
Here is function main() with the calls;
Code:
#include <iostream>
#include <iomanip>
using namespace std;
#include "vec.h"
ostream &operator <<( ostream &out, const Vec &v )
{
for( int i = 0; i < v.size( ); ++i )
{
out << setw( 5 ) << v[ i ];
}
return out;
}
int main( )
{
Vec v; // v is an object of type Vec
v.push_back( 10 ); // push 5 ints into v
v.push_back( 20 );
v.push_back( 30 );
v.push_back( 40 );
v.push_back( 50 );
v.push_back( 60 ); // try to push one element too many
cout << "after initializing v:\n" << v << '\n';
v.pop_back( ); // remove the last element of v
cout << "after popping last element of v:\n" << v << '\n';
v.erase( 0 ); // erase the first element of v
cout << "after erasing first element of v:\n" << v << '\n';
v.insert( 0, 60 ); // insert value 60 at index 0
cout << "after inserting 60 at index 0 of v:\n" << v << '\n';
v.erase( 1 ); // erase the second element of v
cout << "after erasing second element of v:\n" << v << '\n';
// erase the last element of v
v.erase( v.size( ) - 1 );
cout << "after erasing the last element of v:\n" << v << '\n';
v.erase( 0 ); // erase the first element of v
cout << "after erasing the 1st element of v:\n" << v << '\n';
v.insert( 0, 10 ); // insert 10 at position 0
v.insert( 1, 20 ); // insert 20 at position 1
v.insert( 3, 40 ); // insert 40 at position 3
cout << "after inserting three elements in v:\n" << v << '\n';
v.insert( 5, 50 ); // try to insert 50 at position 5; invalid!
cout << "after trying to insert 50 at an invalid loc in v:\n"
<< v << '\n';
return 0;
}