简体   繁体   English

将对象转换为向量的const_iterator

[英]Cast object into vector's const_iterator

Hi I have a function which takes std::vector<T>::const_iterator as parameter. 嗨,我有一个函数,将std::vector<T>::const_iterator作为参数。

Question is: 问题是:

How can I cast standalone object of type T, into iterator, so I can use it as an argument? 如何将类型为T的独立对象转换为迭代器,以便可以将其用作参数?

"How can I cast standalone object of type T, into iterator, so I can use it as an argument?" “如何将类型为T的独立对象转换为迭代器,以便可以将其用作参数?”

You cannot. 你不能。 That standalone object needs to be stored in an appropriate container. 独立对象需要存储在适当的容器中。 Only containers provide creation of iterators. 仅容器提供迭代器的创建。

You shouldn't be trying to cast an arbitrary object type T to std::vector::const_iterator . 您不应该尝试将任意对象类型T强制转换为std::vector::const_iterator

Assuming the object is a std::vector<T> , you can use the std::vector::cbegin or std::vector::cend functions to get a std::vector::const_iterator . 假设对象是std::vector<T> ,则可以使用std::vector::cbeginstd::vector::cend std::vector::cbegin函数来获取std::vector::const_iterator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM