N
Nick Overdijk
How would I cast a const_iterator to a normal iterator? For example:
template <typename T1, typename T2>
typename multimap<T1, T2>::iterator &get_indexed_it(const
multimap<T1, T2> &container, const T1 &key, size_t index = 0) const
throw (out_of_range) {
size_t count = container.count(key);
if (count != 0 && count-1 >= index) {
typename multimap<T1, T2>::const_iterator it =
container.find(key);
while (index--) it++;
return const_cast<typename multimap<T1, T2>::iterator&>
(it);
} else {
throw out_of_range((string)"get_indexed_it->"+"Element
" + key + "doesn't exist!");
}
}
MinGW GCC on compiling says:
D:\MyDocs\Code\SSSParser\sss.hpp|155|error: invalid const_cast from type
`std::_Rb_tree_const_iterator<std:air<const std::string, sss::node> >'
to type `std::_Rb_tree_iterator<std:air<const std::string, sss::node> >&'|
I'm guessing this is because const_iterator isn't the same as const
iterator, how else would I accomplish this?
Thanks in Advance,
template <typename T1, typename T2>
typename multimap<T1, T2>::iterator &get_indexed_it(const
multimap<T1, T2> &container, const T1 &key, size_t index = 0) const
throw (out_of_range) {
size_t count = container.count(key);
if (count != 0 && count-1 >= index) {
typename multimap<T1, T2>::const_iterator it =
container.find(key);
while (index--) it++;
return const_cast<typename multimap<T1, T2>::iterator&>
(it);
} else {
throw out_of_range((string)"get_indexed_it->"+"Element
" + key + "doesn't exist!");
}
}
MinGW GCC on compiling says:
D:\MyDocs\Code\SSSParser\sss.hpp|155|error: invalid const_cast from type
`std::_Rb_tree_const_iterator<std:air<const std::string, sss::node> >'
to type `std::_Rb_tree_iterator<std:air<const std::string, sss::node> >&'|
I'm guessing this is because const_iterator isn't the same as const
iterator, how else would I accomplish this?
Thanks in Advance,