const-iterator

“No match for operator-” error on simple iterator difference

不想你离开。 提交于 2019-12-22 00:42:05
问题 Here is my code: #include <set> #include <iostream> using namespace std; int main(){ set<int> st; st.insert(1); int x = st.find(1) - st.begin(); return 0; } I am getting error: no match for 'operator-' in 'st.std::set<_Key, _Compare, _Alloc>::find [with _Key = int, _Compare = std::less<int>, _Alloc = std::allocator<int>](((const int&)((const int*)(&1)))) - st.std::set<_Key, _Compare, _Alloc>::begin [with _Key = int, _Compare = std::less<int>, _Alloc = std::allocator<int>]()' . I am not able

c++11: erase using a const_iterator

核能气质少年 提交于 2019-12-19 16:47:11
问题 I believe that since C++11, the erase function of most containers (e.g. std::vector ) accepts a const_iterator as parameter: iterator erase (const_iterator position); Still my compilers (GCC 4.8 and Clang 3.2, both using GCC libstdc++) won't allow me to use such function, even when compiling with --std=c++11 . Is it a compiler/libstdc++ bug, or did I do something bad? This is a sample code: #include <vector> int main( ) { std::vector<int> v; v.push_back( 1 ); v.push_back( 2 ); v.push_back( 3

C++ iterator and const_iterator problem for own container class

寵の児 提交于 2019-12-17 23:28:07
问题 I'm writing an own container class and have run into a problem I can't get my head around. Here's the bare-bone sample that shows the problem. It consists of a container class and two test classes: one test class using a std:vector which compiles nicely and the second test class which tries to use my own container class in exact the same way but fails miserably to compile. #include <vector> #include <algorithm> #include <iterator> using namespace std; template <typename T> class MyContainer {

Should I prefer iterators over const_iterators?

这一生的挚爱 提交于 2019-12-17 23:17:24
问题 Someone here recently brought up the article from Scott Meyers that says: Prefer iterators over const_iterators (pdf link). Someone else was commenting that the article is probably outdated. I'm wondering what your opinions are? Here is mine: One of the main points of the article is that you cannot erase or insert on a const_iterator , but I think it's funny to use that as an argument against const_iterators . I thought the whole point of const_iterators it that you do not modify the range at

Does `const_iterator` really need to be a different class than `iterator`?

爷,独闯天下 提交于 2019-12-13 15:33:03
问题 Let say I define some kind of container A : struct A { iterator begin(){ return iterator(this,0); } const iterator cbegin() const { return iterator(this, last());} //... }; Suppose now I want to declare the iterator (part of A): struct A::iterator { iterator ( A* ptr, size_t idx){}; //... }; Which I would use like: const A a; A::iterator it = a.cbegin(); That does not work because the pointer passed to the constructor of iterator is non-const. The ideal solution would be something like a

How do I convert iterator to const_iterator in my custom list iterator class?

…衆ロ難τιáo~ 提交于 2019-12-13 03:18:16
问题 I am trying to implement my own doubly-linked list (my_list) code in C++, and in particular an iterator class for my list. My problem is I want to have an implicit conversion from iterator to const_iterator so that for example the code my_list::iterator it = l.begin(); where l is an instance of my_list compiles. However, I cannot find a way to do this without my compiler complaining. Here is the code implementing the list nodes and the iterator class: template<class T> class node { node(const

insert_or_assign is allowing iterator

余生长醉 提交于 2019-12-11 08:51:28
问题 I have this piece of code: auto it = my_map.lower_bound(my_key); The following assert gives me error: static_assert(std::is_same<decltype(it), std::map<K, V>::const_iterator>::value, "Error"); And the following one, is ok: static_assert(std::is_same<decltype(it), std::map<K, V>::iterator>::value, "Error"); Then, the compiler is not giving me a const_iterator . Ok. But here: my_map.insert_or_assign(it, my_key, some_val); even with iterator (not const_iterator ), the function is working. But,

How to expose C++ classes with const_iterator

陌路散爱 提交于 2019-12-11 06:38:18
问题 I am using Boost.Python to expose a 3rd party C++ API. A header file I've come to declares an iterable class (has begin and end methods), and a custom iterator class with which to do the iteration:- // File: data.hpp #include <utility> // for std::pair #include <cstring> // for size_t namespace notmylib { // forward declaration class DataIterator; // Storage for arbitrary data class Data { public: Data(void); virtual ~Data(void); // ... typedef DataIterator const_iterator; const_iterator

How do I create a const boost::iterator_range

和自甴很熟 提交于 2019-12-10 10:27:42
问题 The comment at Why does boost::find_first take a non-const reference to its input? suggests "the caller to create a non-const iterator_range with const_iterator template parameter to "prove" that the iterated object has a sufficient lifetime." What does this mean and how do I do it? In particular, how do I achieve const-correctness with this code? typedef std::map<int, double> tMyMap; tMyMap::const_iterator subrange_begin = my_map.lower_bound(123); tMyMap::const_iterator subrange_end = my_map