I am working on a simple Windows::Forms
GUI and am running into some issues trying to use STL vector
and pair
through the cliext
package. Basically, I implemented a very simple (and naive) bidirectional map with an underlying cliext::vector< cliext::pair<A, B> >
. I don't care about efficiency at all right now and all instances of it will hold only a handful of entries so my naive linear searches will not be an issue.
But, since I want to use these maps inside my Windows::Forms
instance I made it a managed class. The problem is that I cannot figure out how to modify individual entries of my map.
Here is the basic interface (I put the function def's here for brevity):
template<class A, class B>
ref class bimap
{
public:
typedef typename cliext::vector< cliext::pair<A, B> >::const_iterator const_it;
void Insert(const A & a, const B & b)
{
bmap.push_back(cliext::pair<A,B>(a,b));
}
bool SetInternalByLeftDirect(const A & search_for, const B & set_value)
{
for(int i = 0; i < bmap.size(); i++)
{
if(bmap[i].first == search_for)
{
bmap[i].second = set_value;
return true;
}
return false;
}
}
bool SetInternalByLeftWithIterator(const A & search_for, const B & set_value)
{
for(const_it it = bmap.begin(); it != bmap.end(); it++)
{
if((*it).first == search_for)
{
(*it).second = set_value;
return true;
}
return false;
}
}
bool SetExternalByLeft(const A & search_for, [Out] B % modify_external)
{
for(int i = 0; i < bmap.size(); i++)
{
if(bmap[i].first == search_for)
{
modify_external = bmap[i].second;
return true;
}
}
return false;
}
private:
cliext::vector< cliext::pair<A, B> > bmap;
};
Both SetInternal
approaches do not work. The iterator approach just gives me a compile error where it says "left of first must be a class/struct/union..did you mean ->" but that doesn't make sense to me because Visual Studio already says it is wrong to use -> and when I use . IntelliSense even shows me "first" and "second" in the list.
The direct approach won't work either. It compiles and runs but it doesn't actually modify the value of the pair (even when it returns true!).
Moreover, I included the SetExternal function because that actually works fine. This tells me that searching through the bmap
vector by indices works fine and the conditional on bmap[i]
is fine and it can set the external variable.
Any thoughts on how I can write a SetInternal
method to modify individual pairs in my vector? I also need a way to iterate through the bimap externally and I wanted to use a const iterator (i.e., have member methods that return a const iterator to the begin and end of the underlying bmap
) but when I try to use it externally and dereference the iterator I get the same error as when I try to dereference the iterator in the SetInternalByLeftWithIterator
method above.
Thank you!