- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
容器需要提供 iterator
可隐式转换为 const_iterator
的类型.鉴于此,我正在尝试使用 auto
通过 vector::begin()
初始化对象,并在 std::distance
中使用该结果对象其中 RHS 是 const_iterator
.这是行不通的。这是一个完整的例子:
#include <cstdlib>
#include <vector>
#include <iterator>
#include <iostream>
typedef std::vector <char> Packet;
typedef std::vector <Packet> Packets;
template <typename Iter>
Iter next_upto (Iter begin, Iter end, size_t n)
{
Iter ret = begin;
for (; n > 0 && ret != end; ++ret, --n)
;
return ret;
}
Packets::const_iterator Process (Packets::const_iterator begin, Packets::const_iterator end)
{
Packets::const_iterator ret = begin;
while (ret != end)
++ret; // do something
return ret;
}
int main()
{
Packets test (100); // vector of 100 default-initialized packets
// process them 10 at a time
for (auto it = test.begin();
it != test.end();
it = next_upto (it, test.end(), 10))
{
auto itr = Process (it, next_upto (it, test.end(), 10));
Packets::const_iterator it2 = it;
const size_t n1 = std::distance (it2, itr);
const size_t n = std::distance (it, itr);
std::cout << "Processed " << n << " packets\n";
}
}
在 g++ 4.8.1(和 4.8.2)下编译会产生:
[1/2] Building CXX object CMakeFiles/hacks.dir/main.o
FAILED: /usr/bin/g++ -Wall -std=c++11 -g -MMD -MT CMakeFiles/hacks.dir/main.o -MF "CMakeFiles/hacks.dir/main.o.d" -o CMakeFiles/hacks.dir/main.o -c main.cpp
main.cpp: In function ‘int main()’:
main.cpp:39:45: error: no matching function for call to ‘distance(__gnu_cxx::__normal_iterator<std::vector<char>*, std::vector<std::vector<char> > >&, __gnu_cxx::__normal_iterator<const std::vector<char>*, std::vector<std::vector<char> > >&)’
const size_t n = std::distance (it, itr);
^
main.cpp:39:45: note: candidate is:
In file included from /usr/include/c++/4.8/bits/stl_algobase.h:66:0,
from /usr/include/c++/4.8/vector:60,
from main.cpp:2:
/usr/include/c++/4.8/bits/stl_iterator_base_funcs.h:114:5: note: template<class _InputIterator> typename std::iterator_traits<_Iterator>::difference_type std::distance(_InputIterator, _InputIterator)
distance(_InputIterator __first, _InputIterator __last)
^
/usr/include/c++/4.8/bits/stl_iterator_base_funcs.h:114:5: note: template argument deduction/substitution failed:
main.cpp:39:45: note: deduced conflicting types for parameter ‘_InputIterator’ (‘__gnu_cxx::__normal_iterator<std::vector<char>*, std::vector<std::vector<char> > >’ and ‘__gnu_cxx::__normal_iterator<const std::vector<char>*, std::vector<std::vector<char> > >’)
const size_t n = std::distance (it, itr);
^
我知道我可以通过调用 cbegin()
来修复这个特定的实例和 cend()
而不是 begin()
和 end()
, 但自 begin()
和 end()
返回应可转换为 const_iterator
的类型,我不确定我是否理解为什么需要这样做。
为什么 auto
在这种情况下推导出一个不可转换为 const_iterator
的类型?
最佳答案
您的问题可以简化为以下示例,但由于相同的原因而失败。
#include <vector>
#include <iterator>
int main()
{
std::vector<int> v;
std::vector<int>::const_iterator it1 = v.begin();
auto it2 = v.end();
auto n = std::distance(it1, it2);
}
std::distance
为两个参数使用相同的模板参数类型定义,并且模板参数推导失败,因为您有一个 const_iterator
和 iterator
。
从函数调用推导模板参数时不考虑用户定义的转换,并且由于在这种情况下两个参数具有不同的类型,并且都参与模板参数推导,推导失败。
§14.8.1/6 [temp.arg.explicit]
Implicit conversions (Clause 4) will be performed on a function argument to convert it to the type of the corresponding function parameter if the parameter type contains no template-parameters that participate in template argument deduction.
§14.8.2.1/4 [温度过高]
...
[ Note: as specified in 14.8.1, implicit conversions will be performed on a function argument to convert it to the type of the corresponding function parameter if the parameter contains no template-parameters that participate in template argument deduction. Such conversions are also allowed, in addition to the ones described in the preceding list. —end note ]
您需要将 iterator
转换为 const_iterator
,或明确指定 std::distance
的模板参数。
auto n = std::distance(it1, static_cast<decltype(it1)>(it2));
或
auto n = std::distance<decltype(it1)>(it1, it2);
当然,其他选项是不使用 auto
并在两种情况下显式指定迭代器类型,或者使用 vector::cbegin()
和 vector::cend()
成员函数,当您需要确保类型是 const_iterator
时。
关于c++ - auto it = vector.begin() 结果类型不可转换为 const_iterator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25184932/
#include #include using namespace std; void print(vector::const_iterator &beg, vector::const_itera
为什么在 STL 中 std::iterator_traits::value_type 与类型相同 std::iterator_traits::value_type 为什么要这样设计?第一个不应该是c
我正在阅读有关此功能工作方式的不同解释。 cplusplus.com说这个函数应该“直接在 i 之后移动元素”。 然而cppreference.com表示它拼接元素 AT i。 MSvisual st
我正在寻找一种将 std::set::const_iterator 用作我自己的类的 const_iterator 的方法。 我的代码(实际上行为正确并且编译良好)如下: class MyClass{
当我编译以下 MWE 时出现上述错误在 GCC 上 #include void frobnigate( const std::string& str ) { std::string::con
一般背景: 我正在尝试构建一个容器,该容器将作为运行时定义维度的多维数组的包装器 - 事实上,底层数组当然是总大小的一维数组。主要部分是 operator [] 返回子数组上的包装器。 由于容器需要迭
我在尝试使用 const_iterators 时收到运行时错误。错误是:列表迭代器不可取消引用。我知道不能取消引用 const_iterator 以将值分配到列表中,但我试图取消引用迭代器以访问列表中
我有一个方法可以找到 map 中的特定位置并通过迭代器引用“返回”它: bool Func(const int searchKey, MyMap::iterator& iter) const {
我正在用自定义容器扩展STL容器,以便对元素的操作提供更灵活的控制 class MyContainer; template class myiterator :public iterator {
这段代码 std::ostream& operator indent(output.rdbuf()); output.rdbuf(&indent); for (Arra
有件事我实在想不明白。以下情况: 测试.h文件: class Test{ public: const std::list& getItems() { return m_item
我正在从这本名为 Accelerated C++ 的书中学习 C++。一章有这个函数“split”,它接受从“getline”读取的字符串并返回一个充满分离词的 vector 。 vector spl
好的,这是我的问题。有人告诉我,我的类(class)没有匹配的构造函数。这是调用它的代码。 (忽略输入。我输入的内容似乎无关紧要,结果都很糟糕)。 const_iterator end() const
我正在用 C++ 做一个大学项目,其目的是学习如何使用不同的 STL 容器及其迭代器。 在我的程序中,我有一个带有集合的类: class ConjuntoDeLetras{ private:
我正在尝试用 C++ 实现我自己的双向链表 (my_list) 代码,尤其是我的列表的迭代器类。我的问题是我想要从迭代器到 const_iterator 的隐式转换,例如代码 my_list::ite
我的程序结构类似于: class A { private: std::set members; public: void func(const C& a
我有这个声明: list::const_iterator iter; 我正在尝试了解 *iter 的类型是:string* 、const string* 还是其他类型。 我读到 cost_iterat
我正在尝试为我的基于数组的列表类构建一个自定义的 const_iterator 嵌套类。这是类(class) class const_iterator { private: const T *p
我正在大学学习 OOP 类(class)(C++ 是基础语言)。我的任务是实现自己的链表模板容器类。我几乎完全做到了,但遇到了问题。已知STL提供iterator和 const_iterator通过列
我试图理解 const_iterator 的含义。我有以下示例代码: void CustomerService::RefreshCustomers() { for(std::vector::c
我是一名优秀的程序员,十分优秀!