STL vector searching question

M

Michael

Ok guys I have the following:

class glVertexArray
{
public:
string name
};

class glSurfaceLibrary
{
public:
GetVertexArrayIndex(string name)
vector<glVertexArray*> VertexArrays;
};

int glSurfaceLibrary::GetVertexArrayIndex(string name)
{
for(unsigned int i=0;i<VertexArrays.size();i++)
{
if( VertexArrays->ArrayName == name ) return i;
}
return -1;
}

so the idea is that it searchs through and finds a VertexArray with matching
name. Now this seems like something that would be already implemented in
STL. I'm tring to use binders but not really getting anywhere, can someone
point me in the right direction.
Thanks

Mike
 
A

Andre Dajd

Michael said:
Ok guys I have the following:

class glVertexArray
{
public:
string name
};

class glSurfaceLibrary
{
public:
GetVertexArrayIndex(string name)
vector<glVertexArray*> VertexArrays;
};

int glSurfaceLibrary::GetVertexArrayIndex(string name)
{
for(unsigned int i=0;i<VertexArrays.size();i++)
{
if( VertexArrays->ArrayName == name ) return i;
}
return -1;
}

so the idea is that it searchs through and finds a VertexArray with matching
name. Now this seems like something that would be already implemented in
STL. I'm tring to use binders but not really getting anywhere, can someone
point me in the right direction.
Thanks

Mike


You might wrap your pointer class into another class and overwrite
equality operator that is contingent on some getter Then you can use
standard "find". Something like that (a bit more generic than what
you need, and memory management is bad, but this would be the case for
your design anyway, unless you use somthing like boost::shared_ptr).

Example works with GCC 3.3

**************
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
#include <memory>

using namespace std;

// helpers
template<typename _T>
struct Ptr
{
_T* ptr;

virtual ~Ptr() { delete ptr; }

Ptr(_T* p): ptr(p) {}

Ptr(Ptr const& src): ptr(new _T(*src.ptr)){} // copy
constructablity; to be sorable in a vector
};


template<typename _T,
typename _Key,
_Key const& (_T::*_KeyGetter)() const>
struct PtrWithKey: public Ptr<_T>
{
PtrWithKey(_T* p): Ptr<_T>(p) {}

PtrWithKey(PtrWithKey const& src): Ptr<_T>(src) {} // copy
constructablity

bool
operator==(_Key const& src) { return (ptr->*_KeyGetter)()== src; }
// the bugger
};

// client side
class glVertexArray
{
public:
string name;

glVertexArray(string const& str): name(str) {}

string const&
getName() const { return name; } // field getter to be used by
the template
};


class glSurfaceLibrary
{
public:
typedef PtrWithKey<glVertexArray, string, &glVertexArray::getName>
glVertexArrayPtrT;

vector<glVertexArrayPtrT> VertexArrays;

size_t getVertexArrayIndex(string const& name)
{
ptrdiff_t pos = find(VertexArrays.begin(), VertexArrays.end(),
name) - VertexArrays.begin();
return pos == VertexArrays.size()? -1: pos;
}
};


int main(int argc, char *argv[])
{
cout<<"starting...";

glSurfaceLibrary lib;

lib.VertexArrays.push_back(new glVertexArray("string1"));
lib.VertexArrays.push_back(new glVertexArray("string2"));
lib.VertexArrays.push_back(new glVertexArray("string3"));
lib.VertexArrays.push_back(new glVertexArray("string4"));

cout<<lib.getVertexArrayIndex("string3")<<endl;
cout<<lib.getVertexArrayIndex("string")<<endl;

system("PAUSE");
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

STL Vector Access 1
STL vector push_backpack 4
STL set Problem 5
STL ?? 2
problem with C++ stl (deque and vector) 4
Modify STL multiset 2
School Project 1
STL hash_map 1

Members online

Forum statistics

Threads
474,183
Messages
2,570,970
Members
47,527
Latest member
RoxanneTos

Latest Threads

Top