R
Rune Allnor
Hi folks.
I have a function that takes an element in a vector as
argument. The naive interface goes as
float computeSomething(const std::vector<float>& v, size_t i)
{
size_t j = i-1;
size_t k = i+1;
if (j<0){/* Handle start boundary condition */}
if (k>=v.size()){/* Handle terminal boundary condition */}
}
I would like to implement this in terms of iterators.
But it seems one needs several iterators to get this
done:
typedef std::vector<float>::const_iterator pos;
float computeSomething(pos i, pos b, pos e)
// Call function with b=v.begin(), e=v.end()
{
pos j = i; --j;
pos k =i; ++k;
if (i==b){/* Handle start boundary condition */}
if (k==e){/* Handle terminal boundary condition */}
}
So my question is: Do there exist generally recognizable
invalid values for the std::vector<>::iterators? Like std::string::npos
() ?
If yes I could make a greatly simplified (as well as
safer) interface to the function:
float computeSomething(pos i)
{
pos j=i;--j;
pos k = i;++k;
if(j==std::vector::npos){}
if(k==std::vector::npos) {}
}
But I can't find any analogy to std::string::npos defined for
vectors. Does such a value exist?
Rune
I have a function that takes an element in a vector as
argument. The naive interface goes as
float computeSomething(const std::vector<float>& v, size_t i)
{
size_t j = i-1;
size_t k = i+1;
if (j<0){/* Handle start boundary condition */}
if (k>=v.size()){/* Handle terminal boundary condition */}
}
I would like to implement this in terms of iterators.
But it seems one needs several iterators to get this
done:
typedef std::vector<float>::const_iterator pos;
float computeSomething(pos i, pos b, pos e)
// Call function with b=v.begin(), e=v.end()
{
pos j = i; --j;
pos k =i; ++k;
if (i==b){/* Handle start boundary condition */}
if (k==e){/* Handle terminal boundary condition */}
}
So my question is: Do there exist generally recognizable
invalid values for the std::vector<>::iterators? Like std::string::npos
() ?
If yes I could make a greatly simplified (as well as
safer) interface to the function:
float computeSomething(pos i)
{
pos j=i;--j;
pos k = i;++k;
if(j==std::vector::npos){}
if(k==std::vector::npos) {}
}
But I can't find any analogy to std::string::npos defined for
vectors. Does such a value exist?
Rune