Archived

This topic is now archived and is closed to further replies.

GekkoCube

embarassing question about STL.

Recommended Posts

JTippetts    12970
A heap is generally implemented as one of the other containers (vector, list, etc). The "heap-ness" comes from algorithms (defined, surprisingly enough, in the algorithm include) that are implemented separate from the containers. You would do a heap something like this:


class PQueue
{
std::vector<Node *> heap;

.
.
.
Node *Pop();
void Push(Node *N);
};

Node *PQueue::Pop()
{
Node *n = heap.front();
std::pop_heap(heap.begin(), heap.end(), NodeGreaterThan() );
heap.pop_back();
return(n);
}

void PQueue::Push(Node *N)
{
heap.push_back(N);
std::push_heap(heap.begin(), heap.end(), NodeGreaterThan() );
}



Note that for the operations push_heap() and pop_heap(), you need to define a class for Node comparison, something like this should work:

  
class NodeGreaterThan
{
public:
bool operator()(Node * first, Node *second) const {/* Do a comparison between first and second*/};
};


There is no heap type, per se.

Josh
vertexnormal AT linuxmail DOT org


Check out Golem at:
My cheapass website

[edited by - VertexNormal on September 1, 2003 10:47:49 PM]

[edited by - VertexNormal on September 1, 2003 10:48:51 PM]

Share this post


Link to post
Share on other sites
GekkoCube    116
umm, he he, im getting errors for vector.h too.
sorry, i havent used anything stl in a while - always used custom containers.

#include <vector.h> is a problem, cannot find file it says.

Share this post


Link to post
Share on other sites
JTippetts    12970
quote:
Original post by GekkoCube
umm, he he, im getting errors for vector.h too.
sorry, i havent used anything stl in a while - always used custom containers.

#include <vector.h> is a problem, cannot find file it says.


The use of vector.h is deprecated and obsolete. instead, do #include < vector >, without the .h

Josh
vertexnormal AT linuxmail DOT org


Check out Golem at:
My cheapass website

Share this post


Link to post
Share on other sites
GekkoCube    116
using #include <vector>
gets me this error:

error C2143: syntax error : missing '';'' before ''<''

and are you sure vector.h is obsolete?
i remember using it no TOO long ago.

Share this post


Link to post
Share on other sites
JTippetts    12970
quote:
Original post by GekkoCube
using #include <vector>
gets me this error:

error C2143: syntax error : missing ';' before '<'

and are you sure vector.h is obsolete?
i remember using it no TOO long ago.


Yes, vector.h (and list.h, and all the other STL .h files) are obsolete. In many cases, they are included in the interest of backward compatability, but their use in new projects is not recommend, as you can not guarantee that they will always be around.

This comes from the backward_warning.h file in my /usr/include/c++/backward directory:


#ifndef _CPP_BACKWARD_BACKWARD_WARNING_H
#define _CPP_BACKWARD_BACKWARD_WARNING_H 1

#ifdef __DEPRECATED
#warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
#endif

#endif


As for the semicolor error, it's hard to say without seeing your code. First thing would be to make sure the standard C++ headers are in your path, just to be certain.

Josh
vertexnormal AT linuxmail DOT org


Check out Golem at:
My cheapass website

[edited by - VertexNormal on September 1, 2003 11:14:56 PM]

Share this post


Link to post
Share on other sites
GekkoCube    116
standard c++ header files?

not sure.
i do have all of the default directories.

the code is simple (i just started to write it).


#ifndef _ASTAR_H_
#define _ASTAR_H_

//#include <heap>

#include <vector>
#include "GDPGraph.h"


class CAStar {
public:

/////////////////////////////////////////////////////////////////////////////


CAStar()
{
m_pgraph = NULL;
}

/////////////////////////////////////////////////////////////////////////////


~CAStar()
{

}

/////////////////////////////////////////////////////////////////////////////


void SetGraph(CGDPGraph &graphobj)
{
m_pgraph = graphobj;
}

/////////////////////////////////////////////////////////////////////////////


void FindPath()
{

}

/////////////////////////////////////////////////////////////////////////////


void Find()
{

}

/////////////////////////////////////////////////////////////////////////////


private:
//heap<int> m_Open;

vector<int> m_Closed;
CGDPGraph *m_pgraph;
};

#endif

Share this post


Link to post
Share on other sites
JTippetts    12970
Hmmm... the only problem I can see with that is:


private:
//heap<int> m_Open;

vector<int> m_Closed;
CGDPGraph *m_pgraph;


/* This should be : */

private:
//heap<int> m_Open;

std::vector<int> m_Closed;
CGDPGraph *m_pgraph;


Unless you use a "using namespace std" directive, you need to qualify all std types with the std:: namespace declaration.


Josh
vertexnormal AT linuxmail DOT org


Check out Golem at:
My cheapass website

Share this post


Link to post
Share on other sites