I would like a wrapper for a std::vector<std::vector<T>>
object. Here are a few basic methods implemented under Wrapper
template<class T>
class Wrapper
{
private:
std::vector<std::vector<T>>& data;
int totalSize;
std::vector<int> indicesMap;
public:
Wrapper(std::vector<std::vector<T>>& input):data(input)
{
totalSize = 0;
for (int i = 0 ; i < data.size() ; i++)
{
totalSize += data[i].size();
indicesMap.push_back(totalSize);
}
}
T operator[](int index)
{
int whichVector = std::upper_bound(
indicesMap.begin(),
indicesMap.end(),
index
) - indicesMap.begin();
int i = whichVector == 0 ? index : index - indicesMap[whichVector-1];
return data[whichVector][i];
}
int size()
{
return totalSize;
}
};
Here is a simple test
int main()
{
std::vector<std::vector<int>> x;
std::vector<int> x1 = {1,2,3};
std::vector<int> x2 = {10,20,30};
std::vector<int> x3 = {100,200,300};
x.push_back(x1);
x.push_back(x2);
x.push_back(x3);
Wrapper<int> w(x);
std::cout << w[4] << "\n"; // prints 20 as expected
return 0;
}
I would like to be able to use upper_bound
and lower_bound
on the object Wrapper
. I don't really understand how to make iterators to custom-made objects though and fail to implement that and even then, I am not quite sure it is feasible to just give the being and end iterators to lower_bound
.
Can you help me to implement upper_bound
and lower_bound
for the object Wrapper
?
My goal would be to be able to do
std::lower_bound(w.begin(), w.end(), object);