- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
#include <iostream>
#include <vector>
using namespace std;
void print(vector<int>::const_iterator &beg, vector<int>::const_iterator &end);
int main()
{
vector<int> ivec = {1, 2, 3, 4, 5};
print(ivec.cbegin(), ivec.cend());
return 0;
}
void print(vector<int>::const_iterator &beg, vector<int>::const_iterator &end)
{
if (beg == end)
{
cout << "over" << endl;
return;
}
cout << *beg << endl;
print(++beg, end);
}
ubuntu 20,g++ 9.0
rec_print_vect.cpp: In function ‘int main()’:
rec_print_vect.cpp:11:19: error: cannot bind non-const lvalue reference of type ‘std::vector<int>::const_iterator&’ {aka ‘__gnu_cxx::__normal_iterator<const int*, std::vector<int> >&’} to an rvalue of type ‘std::vector<int>::const_iterator’ {aka ‘__gnu_cxx::__normal_iterator<const int*, std::vector<int> >’}
11 | print(ivec.cbegin(), ivec.cend());
| ~~~~~~~~~~~^~
rec_print_vect.cpp:6:41: note: initializing argument 1 of ‘void print(std::vector<int>::const_iterator&, std::vector<int>::const_iterator&)’
6 | void print(vector<int>::const_iterator &beg, vector<int>::const_iterator &end);
const int &i = 10
是法律,对吧?为什么
const_iterator
不能?我很困惑。这个程序只想递归打印一个 vector 。我想为迭代器使用引用。因为我不需要修改 vector 中的元素,所以我将 const_iterator 与 cbegin() 和 cend() memeber 函数一起使用。我认为将 const var 引用绑定(bind)到 const var 或文字值是正确的。但是我的代码无法通过编译阶段。如果我在函数形式参数中使用 const_iterator 而不是 const_iterator&,我的代码可以运行良好。
最佳答案
干得好:
void print(vector<int>::const_iterator beg, vector<int>::const_iterator end);
问题是,您不能将临时(由 begin() 等人返回的迭代器)绑定(bind)到非常量引用。所以要么接受
vector<int>::const_iterator const&
或按值获取这些迭代器(这还不错)。
关于c++ - 无法将 const_iterator 绑定(bind)到 const_iterator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64738424/
#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
我是一名优秀的程序员,十分优秀!