Pass a compare function to a class [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to be able to create a compare function using a class, eg:
bool someClassLess(const someClass &a1, const someClass &a2) {
return (a1.variable1 < a2.variable1 && a1.variable2 < a2.variable2);
}
Then declare a priority queue of someClass and pass my compare function to be used when pushing elements, eg:
PriorityQueue<someClass> arr(someClassLess());
If no compare function is passed when declaring the priority queue, it should automatically use the less function in functional library when pushing.
How do I pass a compare function to a class?
Below you can find code of my selfwritten PriorityQueue. The code includes a failed attempt to pass a compare function.
#ifndef PRIORITY_QUEUE_H
#define PRIORITY_QUEUE_H
#include <iostream>
#include <functional>
using std::cout;
using std::endl;
using std::less;
template<typename T>
class PriorityQueue {
public:
template<typename PRED>
PriorityQueue(PRED compare);
~PriorityQueue();
T pop();
void push(const T &e);
size_t getSize() const;
bool isEmpty() const;
void print() const;
private:
T *end;
T *queue;
PRED compare;
};
template<typename T>
PriorityQueue<T>::PriorityQueue(PRED compare = less<T>()) : queue(0), end(0), compare(compare) {
}
template<typename T>
PriorityQueue<T>::~PriorityQueue() {
delete [] queue;
}
template<typename T>
T PriorityQueue<T>::pop() {
if(isEmpty()) {
throw "Queue is empty";
} else if(getSize() == 1) {
T removed = *queue;
delete [] queue;
queue = end = 0;
return removed;
}
T *newQueue = new T[getSize() - 1];
// Iteratorer
T *it = queue, *itNew = newQueue;
T removed = *(it++);
for( ; it != end; it++, itNew++) {
*itNew = *it;
}
int oldSize = getSize();
T *tmp = queue;
queue = newQueue;
delete [] tmp;
end = queue + oldSize - 1;
return removed;
}
template<typename T>
void PriorityQueue<T>::push(const T &e) {
if (isEmpty()) {
queue = new T[1];
*queue = e;
end = queue + 1;
return;
}
T *newQueue = new T[getSize() + 1];
// Iterators
T *it = queue, *itNew = newQueue;
// Find where element e should be inserted, whilst inserting elements
// compare(*it, e) used to look like *it < e when I was initially creating the class
for( ; compare(*it, e) && it != end; it++, itNew++) {
*itNew = *it;
}
// Insert e
*(itNew++) = e;
// Insert the remaining elements
for ( ; it != end; it++, itNew++) {
*itNew = *it;
}
int oldSize = getSize();
T *tmp = queue;
queue = newQueue;
delete [] tmp;
end = queue + oldSize + 1;
}
template<typename T>
size_t PriorityQueue<T>::getSize() const {
return (end - queue);
}
template<typename T>
bool PriorityQueue<T>::isEmpty() const {
return (getSize() <= 0);
}
template<typename T>
void PriorityQueue<T>::print() const {
for(int *i = queue; i != end; i++) {
cout << *i << endl;
}
}
#endif

Why not just shamelessly copy the implementation of std::priority_queue. Just use this:
template <typename T, typename Compare = std::less< T > >
class PriorityQueue {
public:
PriorityQueue(Compare comp = Compare()) : end(0), queue(0), compare(comp) { };
// ...
private:
T *end;
T *queue;
Compare compare;
};

Related

C++: How to iterate through a double pointer pointing to an array of pointers

In the following piece of code, I am iterating through a pointer to an array of pointers to TreeNode objects. Below is my code where I iterate through the array:
TreeNode* childListPointer = *currNode->children;
for (TreeNode* currChild = childListPointer; currChild != NULL; currChild = ++childListPointer) {
std::cout << "Iteration" << endl;
}
And below is the code of my TreeNode struct:
typedef struct TreeNode {
int key;
int val;
bool flag;
int num_children;
TreeNode **children;
} TreeNode;
However, my code keeps getting stuck an infinite loop, even when the length of the array is a small number (e.x. 4 or 5).
Note: The autograder system does not allow me to modify the TreeNode struct.
Your array is a size and count.
int num_children;
TreeNode **children;
Based off that, you can make a simple range adapter for for(:) loops:
template<class It>
struct range {
It s, f;
It begin() const { return s; }
It end() const { return f; }
range(It b, It e):s(b),f(e) {}
range(It b, std::ptrdiff_t count):range(b, b+count) {}
};
now, just:
for(TreeNode* child : range{ currNode->children, currNode->num_children })
{
std::cout << "Iteration" << endl;
}
Prior to c++17 you need a make_range:
template<class It>
range<It> make_range( It b, It e ) { return {b,e}; }
template<class It>
range<It> make_range( It b, std::ptrdiff_t c ) { return {b,c}; }
for(TreeNode* child : make_range( currNode->children, currNode->num_children ))
{
std::cout << "Iteration" << endl;
}
because "deduction guides" where added in c++17.
And you are done. You can now iterate over the children without doing any pointer arithmetic and getting confused.
...
If you are stuck in c++03 you can do:
for (int i = 0; i < currNode->num_children; ++i)
{
TreeNode* child = currNode->children[i];
std::cout << "Iteration" << endl;
}
or
TreeNode** it = currNode->children;
TreeNode** end = it+currNode->num_children;
for (; it != end; ++it)
{
TreeNode* child = *it;
std::cout << "Iteration" << endl;
}
which is almost exactly what the range version compiles down to.

C++ Iterator with hasNext and Next

I am new to C++ world and I need a help. My problem is I try implement my structure hash pair array, there is key and data. In this structure I have nested structure iterator with methods hasNext and next. Because I can not see my array (this array is in parent) from nested structure I need pass it through constructor, but there is error ": cannot convert from...", problem is with pass _array in method getIterator. Code is below. Could you help me? Thanks
#pragma once
template<typename T, typename U, int Size, int(*HashFunction)(T)>
struct HashPairPole {
// Pair - key - data
struct Par {
// key
T _first;
// data
U _second;
// list for collision records
Par* _overflow;
Par(T t, U u) {
_first = t;
_second = u;
_overflow = nullptr;
}
};
HashParovePole() {}
// Static array for save data
Par* _array[Size];
// Add record into hash table
void add(T t, U u) {
// calculating of index
Par* prvek;
int idx = HashFunction(t) % Size;
// Element will be saved in _array[idx], if it is free, else will be
//saved to list (->_overflow)
prvek = new Par(t, u);
if (_array[idx] == nullptr) {
_array[idx] = prvek;
}
else {
prvek->_overflow = _array[idx];
}
_array[idx] = prvek;
}
// Get data from hash tabule
U& get(T t) {
int idx = HashFunction(t) % Size;
Par * prvni = _array[idx];
while (prvni->_overflow != nullptr) {
if (prvni->_first == t) {
return prvni->_second;
}
prvni = prvni->_overflow;
}
}
U& operator[](T t) {
return get(t);
}
U operator[](T t) const {
const U temp = get(t);
return temp;
}
// Iterator for walking all hash table
struct iterator {
Par* index[Size];
Par* pomPar;
int temp = 0;
iterator(Par * _array) {
index = _array;
pomPar = index[0];
}
bool hasNext()const {
return pomPar != nullptr;
}
std::pair<T, U> next() {
std::pair<T, U> data;
if (hasNext()) {
data.first = pomPar->_first;
data.second = pomPar->_second;
pomPar = pomPar->_overflow;
}
temp++;
pomPar = index[temp];
return data;
}
};
// Vytvori iterator
iterator getIterator() {
return iterator(_array);
}
};
As far as I see, the problem is in this line:
Par* _array[Size];
Here you declare an array of size Size of pointers to Par structures, which is probably not what you want.
Later you try to pass this array to constructor iterator(Par * _array), which accepts a pointer to Par structure, which is impossible.
I would fix this code in the following way:
Par _array[Size]; // Instead of Par* _array[Size]
// You need an array of structures instead of array of pointers
...
Par* index; // Instead of Par* index[Size]
// Here looks like index is a pointer to a current element
...
pomPar = index; // Instead of pomPar = index[0];
// This is a pointer to the node, while index[0] is its value
Also, consider using std::vector instead of raw pointers. It will handle memory management issues for you.

C++ lock-free stack is corrupt

i am trying to implement a lockfree stack to be usable with external managed memory from a bounded plain c array. I know reference implementations (like from Anthony Williams: Concurrency in Action) and other books and blogs/article around the web.
The implementation follows those references and avoids the ABA problem, because external memory locations are addressed using unique indexes, rather than recycled pointers. Therefore it does not need to deal with mem managment at all and is simple.
I wrote some tests that execute pop and push operations on that stack under high load and contention (stress tests) and single threaded. The former fail with strange problems, that I do not understand and to me look obscure.
Maybe someone has an idea ?
Problem: Pushing an already popped node back to the stack fails, because precondition is violated that node has no successor (next).
BOOST_ASSERT(!m_aData.m_aNodes[nNode-1].next);
Reproduction setup: At least 3 threads and a capacity of ~16. Around 500 passes. Then push op fails.
Problem: Number of elements popped by all threads and number of elements left in stack after join do not match capacity (nodes lost in transition).
BOOST_ASSERT(aNodes.size()+nPopped == nCapacity);
Reproduction setup: 2 threads and capacity 2. Requires a lot of passes to occur, for me at least 700. After that head of stack is 0, but only one node is present in popped container. Node {2,0} is dangling.
I compiled with vs2005, vs2013 and vs2015. All have the same problem (vs2005 is also the reason that code looks C++03 like).
Here is the basic code for node+stack
template <typename sizeT> struct node
{
sizeT cur; //!< construction invariant
atomic<sizeT> next;
atomic<sizeT> data;
explicit node() // invalid node
: cur(0), next(0), data(0)
{}
explicit node(sizeT const& nCur, sizeT const& nNext, sizeT const& nData)
: cur(nCur), next(nNext), data(nData)
{}
node& operator=(node const& rhs)
{
cur = rhs.cur;
next.store(rhs.next.load(memory_order_relaxed));
data.store(rhs.data.load(memory_order_relaxed));
return *this;
}
};
template <typename sizeT> struct stack
{
private:
static memory_order const relaxed = memory_order_relaxed;
atomic<sizeT> m_aHead;
public:
explicit stack(sizeT const& nHead) : m_aHead(nHead) {}
template <typename tagT, typename T, std::size_t N>
typename enable_if<is_same<tagT,Synchronized>,sizeT>::type
pop(T (&aNodes)[N])
{
sizeT nOldHead = m_aHead.load();
for(;;)
{
if(!nOldHead) return 0;
BOOST_ASSERT(nOldHead <= N);
T& aOldHead = aNodes[nOldHead-1];
sizeT const nNewHead = aOldHead.next.load(/*relaxed*/);
BOOST_ASSERT(nNewHead <= N);
sizeT const nExpected = nOldHead;
if(m_aHead.compare_exchange_weak(nOldHead,nNewHead
/*,std::memory_order_acquire,std::memory_order_relaxed*/))
{
BOOST_ASSERT(nExpected == nOldHead);
// <--- from here on aOldHead is thread local ---> //
aOldHead.next.store(0 /*,relaxed*/);
return nOldHead;
}
// TODO: add back-off strategy under contention (use loop var)
}
}
template <typename tagT, typename T, std::size_t N>
typename enable_if<is_same<tagT,Synchronized>,void>::type
push(T (&aNodes)[N], sizeT const& nNewHead)
{
#ifndef NDEBUG
{
BOOST_ASSERT(0 < nNewHead && nNewHead <= N);
sizeT const nNext = aNodes[nNewHead-1].next;
BOOST_ASSERT(!nNext);
}
#endif
sizeT nOldHead = m_aHead.load(/*relaxed*/);
for(;;)
{
aNodes[nNewHead-1].next.store(nOldHead /*,relaxed*/);
sizeT const nExpected = nOldHead;
BOOST_ASSERT(nOldHead <= N);
if(m_aHead.compare_exchange_weak(nOldHead,nNewHead
/*,std::memory_order_release,std::memory_order_relaxed*/))
{
BOOST_ASSERT(nExpected == nOldHead);
return;
}
// TODO: add back-off strategy under contention (use loop var)
}
}
};
and the quite noisy test class
class StackTest
{
private:
typedef boost::mpl::size_t<64> Capacity;
//typedef boost::uint_t<static_log2_ceil<Capacity::value>::value>::least size_type;
typedef std::size_t size_type;
static size_type const nCapacity = Capacity::value;
static size_type const nNodes = Capacity::value;
typedef node<size_type> Node;
typedef stack<size_type> Stack;
typedef mt19937 Twister;
typedef random::uniform_int_distribution<std::size_t> Distribution;
typedef variate_generator<Twister,Distribution> Die;
struct Data //!< shared along threads
{
Node m_aNodes[nNodes];
Stack m_aStack;
explicit Data() : m_aStack(nNodes)
{
m_aNodes[0] = Node(1,0,0); // tail of stack
for(size_type i=1; i<nNodes; ++i)
{
m_aNodes[i] = Node(static_cast<size_type>(i+1),i,0);
}
}
template <typename syncT>
void Run(
uuids::random_generator& aUUIDGen,
std::size_t const& nPasses,
std::size_t const& nThreads)
{
std::vector<ThreadLocalData> aThreadLocalDatas(nThreads,ThreadLocalData(*this));
{
static std::size_t const N = 100000;
Die aRepetition(Twister(hash_value(aUUIDGen())),Distribution(0,N));
Die aAction(Twister(hash_value(aUUIDGen())),Distribution(0,1));
for(std::size_t i=0; i<nThreads; ++i)
{
std::vector<bool>& aActions = aThreadLocalDatas[i].m_aActions;
std::size_t const nRepetition = aRepetition();
aActions.reserve(nRepetition);
for(std::size_t k=0; k<nRepetition; ++k)
{
aActions.push_back(static_cast<bool>(aAction()));
}
}
}
std::size_t nPopped = 0;
if(nThreads == 1)
{
std::size_t const i = 0;
aThreadLocalDatas[i].Run<syncT>(i);
nPopped += aThreadLocalDatas[i].m_aPopped.size();
}
else
{
std::vector<boost::shared_ptr<thread> > aThreads;
aThreads.reserve(nThreads);
for(std::size_t i=0; i<nThreads; ++i)
{
aThreads.push_back(boost::make_shared<thread>(boost::bind(&ThreadLocalData::Run<syncT>,&aThreadLocalDatas[i],i)));
}
for(std::size_t i=0; i<nThreads; ++i)
{
aThreads[i]->join();
nPopped += aThreadLocalDatas[i].m_aPopped.size();
}
}
std::vector<size_type> aNodes;
aNodes.reserve(nCapacity);
while(size_type const nNode = m_aStack.pop<syncT>(m_aNodes))
{
aNodes.push_back(nNode);
}
std::clog << dump(m_aNodes,4) << std::endl;
BOOST_ASSERT(aNodes.size()+nPopped == nCapacity);
}
};
struct ThreadLocalData //!< local to each thread
{
Data& m_aData; //!< shared along threads
std::vector<bool> m_aActions; //!< either pop or push
std::vector<size_type> m_aPopped; //!< popp'ed nodes
explicit ThreadLocalData(Data& aData)
: m_aData(aData), m_aActions(), m_aPopped()
{
m_aPopped.reserve(nNodes);
}
template <typename syncT>
void Run(std::size_t const& k)
{
BOOST_FOREACH(bool const& aAction, m_aActions)
{
if(aAction)
{
if(size_type const nNode = m_aData.m_aStack.pop<syncT>(m_aData.m_aNodes))
{
BOOST_ASSERT(!m_aData.m_aNodes[nNode-1].next);
m_aPopped.push_back(nNode);
}
}
else
{
if(!m_aPopped.empty())
{
size_type const nNode = m_aPopped.back();
size_type const nNext = m_aData.m_aNodes[nNode-1].next;
ASSERT_IF(!nNext,"nNext=" << nNext << " for " << m_aData.m_aNodes[nNode-1] << "\n\n" << dump(m_aData.m_aNodes));
m_aData.m_aStack.push<syncT>(m_aData.m_aNodes,nNode);
m_aPopped.pop_back();
}
}
}
}
};
template <typename syncT>
static void PushPop(
uuids::random_generator& aUUIDGen,
std::size_t const& nPasses,
std::size_t const& nThreads)
{
BOOST_ASSERT(nThreads > 0);
BOOST_ASSERT(nThreads == 1 || (is_same<syncT,Synchronized>::value));
std::clog << BOOST_CURRENT_FUNCTION << " with threads=" << nThreads << std::endl;
for(std::size_t nPass=0; nPass<nPasses; ++nPass)
{
std::ostringstream s;
s << " " << nPass << "/" << nPasses << ": ...";
std::clog << s.str() << std::endl;
Data().Run<syncT>(aUUIDGen,nPass,nThreads);
}
}
public:
static void Run()
{
typedef StackTest self_t;
uuids::random_generator aUUIDGen;
static std::size_t const nMaxPasses = 1000;
Die aPasses(Twister(hash_value(aUUIDGen())),Distribution(0,nMaxPasses));
{
//std::size_t const nThreads = 2; // thread::hardware_concurrency()+1;
std::size_t const nThreads = thread::hardware_concurrency()+1;
self_t().PushPop<Synchronized>(aUUIDGen,aPasses(),nThreads);
}
}
};
Here is a link to download all required files.
Both problems are just another facet of the ABA problem.
stack: {2,1},{1,0}
Thread A
pop
new_head=1
... time slice exceeded
Thread B
pop
stack: {1,0}, popped: {2,0}
pop
stack: {}, popped: {2,0}, {1,0}
push({2,0})
stack: {2,0}
Thread A
pop continued
cmp_exch succeeds, because head is 2
stack: {}, head=1 --- WRONG, 0 would be correct
Any problems may arise, because access to nodes is not thread local anymore. This includes unexpected modifications of next for popped nodes (problem 1) or lost nodes (problem 2).
head+next need to be modified in one cmp_exch to avoid that problem.

Push_Front Pop_Back for C++ Vector

I'm trying to keep a vector of commands so that it keeps 10 most recent. I have a push_back and a pop_back, but how do I delete the oldest without shifting everything in a for loop? Is erase the only way to do this?
Use std::deque which is a vector-like container that's good at removal and insertion at both ends.
If you're amenable to using boost, I'd recommend looking at circular_buffer, which deals with this exact problem extremely efficiently (it avoids moving elements around unnecessarily, and instead just manipulates a couple of pointers):
// Create a circular buffer with a capacity for 3 integers.
boost::circular_buffer<int> cb(3);
// Insert threee elements into the buffer.
cb.push_back(1);
cb.push_back(2);
cb.push_back(3);
cb.push_back(4);
cb.push_back(5);
The last two ops simply overwrite the elements of the first two.
Write a wrapper around a vector to give yourself a circular buffer. Something like this:
include <vector>
/**
Circular vector wrapper
When the vector is full, old data is overwritten
*/
class cCircularVector
{
public:
// An iterator that points to the physical begining of the vector
typedef std::vector< short >::iterator iterator;
iterator begin() { return myVector.begin(); }
iterator end() { return myVector.end(); }
// The size ( capacity ) of the vector
int size() { return (int) myVector.size(); }
void clear() { myVector.clear(); next = 0; }
void resize( int s ) { myVector.resize( s ); }
// Constructor, specifying the capacity
cCircularVector( int capacity )
: next( 0 )
{
myVector.resize( capacity );
}
// Add new data, over-writing oldest if full
void push_back( short v )
{
myVector[ next] = v;
advance();
}
int getNext()
{
return next;
}
private:
std::vector< short > myVector;
int next;
void advance()
{
next++;
if( next == (int)myVector.size() )
next = 0;
}
};
What about something like this:
http://ideone.com/SLSNpc
Note: It's just a base, you still need to work a bit on it. The idea is that it's easy to use because it has it's own iterator, which will give you the output you want. As you can see the last value inserted is the one shown first, which I'm guessing is what you want.
#include <iostream>
#include <vector>
template<class T, size_t MaxSize>
class TopN
{
public:
void push_back(T v)
{
if (m_vector.size() < MaxSize)
m_vector.push_back(v);
else
m_vector[m_pos] = v;
if (++m_pos == MaxSize)
m_pos = 0;
}
class DummyIterator
{
public:
TopN &r; // a direct reference to our boss.
int p, m; // m: how many elements we can pull from vector, p: position of the cursor.
DummyIterator(TopN& t) : r(t), p(t.m_pos), m(t.m_vector.size()){}
operator bool() const { return (m > 0); }
T& operator *()
{
static T e = 0; // this could be removed
if (m <= 0) // if someone tries to extract data from an empty vector
return e; // instead of throwing an error, we return a dummy value
m--;
if (--p < 0)
p = MaxSize - 1;
return r.m_vector[p];
}
};
decltype(auto) begin() { return m_vector.begin(); }
decltype(auto) end() { return m_vector.end(); }
DummyIterator get_dummy_iterator()
{
return DummyIterator(*this);
}
private:
std::vector<T> m_vector;
int m_pos = 0;
};
template<typename T, size_t S>
void show(TopN<T,S>& t)
{
for (auto it = t.get_dummy_iterator(); it; )
std::cout << *it << '\t';
std::cout << std::endl;
};
int main(int argc, char* argv[])
{
TopN<int,10> top10;
for (int i = 1; i <= 10; i++)
top10.push_back(5 * i);
show(top10);
top10.push_back(60);
show(top10);
top10.push_back(65);
show(top10);
return 0;
}

Implementation of Deque in C++

I'm writing an implementation of Deque as a programming exercise and it's not going too well at all. I'm missing a few key function that are needed to make the test main program I was given function correctly.
Here is my code so far:
#include <vector>
#include <iostream>
#include <cassert>
using namespace std;
template <class T> class DequeIterator;
template <class T>
class Deque {
public:
typedef DequeIterator<T> iterator;
Deque(): vecOne(), vecTwo() { }
Deque(unsigned int size, T& initial): vecOne(size/2, initial), vecTwo(size-(size/2), initial) { }
Deque(Deque<T> & d): vecOne(d.vecOne), vecTwo(d.vecTwo) { }
T & operator[](unsigned int);
T & front();//
T & back();//
bool empty(){ return vecOne.empty() && vecTwo.empty(); }
iterator begin() { return iterator(this,0); }
iterator end() { return iterator(this, size ()); }
void erase(const iterator &);
void erase(const iterator &, const iterator &);
void insert(const iterator &, const T &);
int size() { return vecOne.size() + vecTwo.size(); }
void push_front(const T & value) { vecOne.push_back(value); }
void push_back(const T & value) {vecTwo.push_back(value); }
void pop_front();
void pop_back();
protected:
vector<T> vecOne;
vector<T> vecTwo;
};
template <class T>//
T & Deque<T>::front()//returns the first element in the deque
{
if (vecOne.empty())
return vecTwo.front();
else
return vecOne.back();
}
template <class T>//
T & Deque<T>::back()//returns the last element in the deque
{
if (vecOne.empty())
return vecTwo.back();
else
return vecOne.front();
}
template <class T>//
T & Deque<T>::operator[] (unsigned int index)
{
int n = vecOne.size();
if (index < n)
return vecOne [ (n-1) - index ];
else
return vecTwo [ index - n ];
}
template <class T>//
Deque<T>::iterator DequeIterator<T>::operator ++ (int)
{
Deque<T>::iterator clone(theDeque, index);
index++;
return clone;
}
template <class T>//
void Deque<T>::pop_front()
{
}
template <class T>//
void Deque<T>::pop_back()
{
}
template <class T>//
void Deque<T>::erase (const iterator & itr)
{
int index = itr.index;
int n = vecOne.size();
if (index < n)
vecOne.erase (vecOne.begin() + ((n-1) - index));
else
vecTwo.erase (vecTwo.begin() + (n - index));
}
template <class T>//
void Deque<T>::erase (const iterator &, const iterator &)
{
}
template <class T>//
void Deque<T>::insert(const iterator &, const T &)
{
}
template <class T>
class DequeIterator {
friend class Deque<T>;
typedef DequeIterator<T> iterator;
public:
DequeIterator(): theDeque(0), index(0) { }
DequeIterator(Deque<T> * d, int i): theDeque(d), index(i) { }
DequeIterator(const iterator & d): theDeque(d.theDeque), index(d.index) { }
T & operator*() { return (*theDeque)[index]; }
iterator & operator++(int) { ++index; return *this; }
iterator operator++();
iterator operator--(int) { --index; return *this; }
iterator & operator--();
bool operator==(const iterator & r) { return theDeque == r.theDeque && index == r.index; }
bool operator!=(const iterator & r) { return theDeque == r.theDeque && index != r.index; }
bool operator< (const iterator & r) { return theDeque == r.theDeque && index < r.index; }
T & operator[](unsigned int i) { return (*theDeque) [index + i]; }
iterator operator=(const iterator & r) { theDeque = r.theDeque; index = r.index; }
iterator operator+(int i) { return iterator(theDeque, index + i); }
iterator operator-(int i) { return iterator(theDeque, index - i); }
protected:
Deque<T> * theDeque;
int index;
};
main()
{
Deque<int> d;
d.push_back(10);
d.push_back(20);
assert(d.front() == 10);
assert(d.back() == 20);
d.push_front(1);
d.push_front(2);
d.push_front(3);
assert(d.front() == 3);
assert(d.back() == 20);
d.pop_back();
d.pop_back();
d.pop_back();
assert(d.front() == 3);
assert(d.back() == 2);
d.push_back(1);
d.push_back(0);
Deque<int>::iterator i;
int counter = 3;
for (i = d.begin(); i != d.end(); i++)
assert(*i == counter--);
for (counter = 0; counter < d.size(); counter++)
assert(d[counter] == d.size()-counter-1);
i = d.begin() + 3;
Deque<int>::iterator j(i), k;
k = j = i - 2;
assert(*k == 2);
for (i = d.begin(); not(i == d.end()); ++i)
cout << *i << " ";
cout << endl;
d.erase(d.begin()+3);
//d.erase(d.begin(), d.begin()+2);
assert(d.size() == 1);
assert(d[0] == 1);
Deque<int> c(d);
c.front() = 3;
assert(c.back() == 3);
c.push_front(1);
c.insert(c.begin(), 0);
c.insert(c.begin()+2, 2);
for (i = c.begin(); not(i == c.end()); ++i)
cout << *i << " ";
cout << endl;
for (counter = 0; counter < c.size(); counter++)
assert(c[counter] == counter);
cout << "SUCCESS\n";
}
I was wondering if someone could tell me my function from line 66 is returning:
expected constructor, destructor, or type conversion before 'DequeIterator'
Because I'm not sure what I'm doing wrong in it. Also, if someone would be kind enough to give me an example of the pop_front() function so that I can use it to create the pop_back() function as well, that would be great. Lastly, I have on of the erase functions completed but I am not sure how to go about creating the second one, which basically erases a value within the range of two iterators, it is referenced in line 176.
Any help would be greatly appreciated. Thank you in advance.
As for the error, you probably need a typename before Deque<T>::iterator on that line.
typename Deque<T>::iterator DequeIterator<T>::operator++(int)
I think it is a great programming exercise to implement deque. But a prerequisite is to implement vector and list. deque is one of the most complicated std::containers to implement. You should start with one of the simpler ones (vector and list).
Well, you get the error on line 65 because you return an object of a class that hasn't been defined. You only have the forward declaration (prototype) for class DequeIterator, not the implementation.
void pop_back() {
vecTwo.pop_back();
}
void pop_front() {
vecOne.pop_back();
}