如何为集合提供带有迭代器的 const 接口
How to provide const interface with iterators to a collection?
我想创建一个带有如下签名的函数:
// Set found to be an iterator to the location of key in map or end()
// if not found.
bool lookup(const Key &key,
const std::map<Key, Value> &map,
std::map<Key, Value>::const_iterator &found);
但是我也想在映射和迭代器不是常量的情况下调用它,以便我可以修改找到的值:
const Key key;
std::map<Key, Value> map;
std::map<Key, Value>::iterator found;
if (lookup(key, map, found)) {
found->second.modifingNonConstFunction()
}
但是我不相信我可以将std::map<Key, Value>::iterator
对象传递给期望引用std::map<Key, Value>::const_iterator
的函数,因为它们是不同的类型,而如果const
是C++声明的一部分,我通常可以这样做,这样我就可以,我可以将非 const 类型提升为 const 类型:
void someFunction(const int &arg);
int notConstArg = 0;
someFunction(nonConstArg);
除了使用模板为lookup()
提供两个定义之外,一个如参数 2 和 3 所示const
另一个如非常量参数 2 和 3 所示,是否有更好的C++方法来实现这一点,更类似于如何在上面的例子中传递const int &
非常量int
。 换句话说,我可以只有一个函数而不是两个吗?
如果函数很简单,或者您不介意二进制膨胀,只需将每个参数都设置为模板参数即可。
template <typename Key, typename T, typename Iter>
bool lookup(Key const& key,
T& map,
Iter &found)
{
return (found=map.find(key))!=map.end();
}
int main()
{
std::map<std::string, int> m; m["hello"] = 42;
std::map<std::string, int> const cm(m.begin(), m.end());
std::map<std::string, int>::iterator it;
std::map<std::string, int>::const_iterator cit;
std::cout << std::boolalpha << lookup("hello", m, it) << 'n'; // Key isn't even std::string
std::cout << std::boolalpha << lookup("hello", m, cit) << 'n';
//std::cout << std::boolalpha << lookup("hello", cm, it) << 'n'; // error
std::cout << std::boolalpha << lookup("hello", cm, cit) << 'n';
}
这是有效的,因为T
可以是map
和const map
,所以T&
是map&
或const map&
。
不,我认为如果没有重载/模板魔法,您将无法做到这一点。
编译器可保护您免受以下情况的影响:
typedef vector<int> T;
const T v; // Can't touch me
void foo(T::const_iterator &it) {
it = v.begin(); // v.begin() really is a const_iterator
}
int main() {
T::iterator it;
foo(it);
*it = 5; // Uh-oh, you touched me!
}
相关文章:
- 迭代器和 Const 迭代器在C++?
- C++ 为 costum 类创建一个 const 迭代器
- 为什么我们在C++标准中没有"const 迭代器",而是const_iterator?
- 如何在单个模板调用中传递const_iterator和非const迭代器
- 什么时候足以将const_iterator声明为const迭代器
- const矢量和const迭代器之间的差异
- 为什么删除字符串迭代器会产生const char参考
- 将 const auto & 转换为迭代器
- 通过引用传递迭代器会给出错误:无法将参数 1 从 'const data' 转换为 'data &
- 从 const char* 到迭代器错误的"无已知转换" - 另一个例子
- 矢量迭代器与 const Vector&不兼容
- 在 C++11 中通过 const 迭代器进行破坏
- find_if() 在 const 函数中返回意外的迭代器类型
- 为什么std::distance不适用于const和nonconst迭代器的混合
- 仅为容器提供const迭代器有意义吗?
- Const与非Const迭代器的比较是否有效
- 创建const迭代器和非const迭代器
- 实现部分const迭代器
- 我应该使用 const &iterator,还是只使用迭代器?
- C++放弃迭代器(const)上的限定符