EXC Bad Access with Iterators - c++

I created this function that works with vectors, linked lists and double linked lists. The function takes a value and searches for it in the container. If the vlaue is in the container than the function will insert the value right next to where it already exists. So, if val=2 then {3,2,5} will become {3,2,2,5}. But if the value does not exist in the container, it is added to the back instead.
I wrote this function using iterators. It works just fine with a vector, however when I try to run it with a list or double linked list I get an Exc Bad Access error at the line if (*it==val). I am not seeing what I did wrong.
template <class Container, class T>
void insertNextTo( Container &x, const T &val){
typename Container::iterator it = x.begin();
while (it!=x.end() && *it!=val){
++it;
}
if (*it == val){
x.insert(it, val);
}
else{
x.push_back(val);
}
}
Edit: Thank you everyone! Your suggestion to change the if statement worked perfectly!

Change
if (*it == val)
to:
if (it != x.end())
If val isn't found in the container, the loop will end when it == x.end(). That points past the end of the array, so indirecting through it results in undefined behavior.

If the value does not exist yet, it will be equal to end() when the loop is finished, and you cannot dereference end(). You need to change if (*it == val) to if (it != x.end()) instead:
//if (*it == val){
if (it != x.end()){
x.insert(it, val);
}
Alternatively, since you are only inserting one value, if you find val then you can do the insert() and exit the function immediately, and then push_back() only if the loop reaches the end of the container:
template <class Container, class T>
void insertNextTo( Container &x, const T &val ) {
typename Container::iterator it = x.begin();
while (it != x.end()) {
if (*it == val) {
x.insert(it, val);
return;
}
++it;
}
x.push_back(val);
}
In which case, you can simplify the code by using std::find() instead of a manual loop:
#include <algorithm>
template <class Container, class T>
void insertNextTo( Container &x, const T &val ) {
typename Container::iterator it = std::find(x.begin(), x.end(), val);
if (it != x.end()) {
x.insert(it, val);
} else {
x.push_back(val);
}
}
Or even this, since it is safe to insert() using the end() iterator, it is effectively the same as push_back():
template <class Container, class T>
void insertNextTo( Container &x, const T &val ) {
typename Container::iterator it = std::find(x.begin(), x.end(), val);
x.insert(it, val);
}

Someone correct me if I'm wrong but I believe your error comes from the fact that the when your iterator reaches the end of the list, it actually points to the address after the last bit of the list in memory, which may or may not be initialized and in scope, which causes your program to error.

Related

How to implement end sentinel for back_insert_iterator?

I want to fill a container by consequtive values of iterators to elements of another container (often occured real life problem), say:
std::container1< T > c1{/* initialized */};
assert(!c1.empty());
std::continer2< typename std::container1< T >::iterator > c2;
auto it = std::begin(c1), const end = std::end(c1);
do { c2.push_back(it); } while (++it != end);
There is attractive std::iota algorithm in STL, but it is range-based and for std::back_inserter(c2) there is no way to achieve desired currently. However in the next versions of STL I can expect the iota algorithm of the form:
template< typename ForwardIterator, typename EndSentinel, typename T >
void
iota(ForwardIterator first, EndSentinel last, T value)
{
for (; first != last; ++first) {
*first = value;
++value;
}
}
How to implement EndSentinel and operator != (ForwardIterator, EndSentinel) to make above iota stop after exactly c1.size() step of the for loop in iota(std::back_inserter(c1), something(c1, c1.size()), std::begin(c1))?
There is no sentinel for std::back_insert_iterator (or any OutputIterator) and also no equality operator, because an output iterator is an "unlimited sequence": You can append elements to the end of a container or write to a file until you run out of memory or disk space.
However, it makes sense to have an output iterator with a sentinel if you need to call an algorithm which expects an "output sentinel" (because not expecting one may be unsafe if the output is a "limited sequence", such as a pre-allocated std::vector). Such an algorithm could look like:
template<typename InIter, typename InSentinel, typename OutIter, typename OutSentinel>
OutIter modernAlgorithm(InIter first, InSentinel last, OutIter outFirst, OutSentinel outLast);
In this case, all you need is a trivial sentinel, which compares unequal to everything. See also this answer.
template<typename T>
struct TrivialSentinel
{
bool operator==(const T&) { return false; }
bool operator!=(const T&) { return true; }
friend bool operator==(const T&, TrivialSentinel&) { return false; }
friend bool operator!=(const T&, TrivialSentinel&) { return true; }
};
modernAlgorithm(v.begin(), v.end(), std::back_inserter(r), TrivialSentinel<decltype(std::back_inserter(r))>());
(This may seem odd, but it does make sense if you consider that even if you repeat the same operation *out = expr on the same value of out, the output will be in a different state each time, so in a certain sense, no two output iterators are ever necessarily equivalent...)
However, older algorithms often don't allow the iterator and sentinel to have different types:
template<typename InIter, typename OutIter>
OutIter olderAlgorithm(InIter first, InIter last, OutIter outFirst, OutIter outLast);
In this case, you can write a sub class or wrapper of std::back_insert_iterator, which has a default constructor and always compares unequal to itself.
This is easy in C++20, where std::back_insert_iterator has a default constructor:
// C++20
template<typename C>
struct BackInsertIteratorWithSentinel : public std::back_insert_iterator<C>
{
BackInsertIteratorWithSentinel() {} // C++20 only
BackInsertIteratorWithSentinel(C& c) : std::back_insert_iterator<C>(c) {}
bool operator==(const BackInsertIteratorWithSentinel&) { return false; }
bool operator!=(const BackInsertIteratorWithSentinel&) { return true; }
};
template<typename C>
BackInsertIteratorWithSentinel<C> BackInserterWithSentinel(C& c)
{
return BackInsertIteratorWithSentinel<C>(c);
}
template<typename C>
BackInsertIteratorWithSentinel<C> BackInserterWithSentinel()
{
return BackInsertIteratorWithSentinel<C>();
}
olderAlgorithm(v.begin(), v.end(), BackInserterWithSentinel(r), BackInserterWithSentinel<std::vector<int> >());
Note that even in C++20, std::back_insert_iterator does not have an equality operator.
If you have to support older versions of C++, then you may have to implement your own std::back_insert_iterator from scratch, or use boost::optional or in-place construction to work around the lack of a default constructor.
Full test program for C++20
I dont think you can do it - or maybe I dont understand your question, but..
according to http://en.cppreference.com/w/cpp/algorithm/iota, this algorithm works on existing range of elements - so it does not make sense to use it with: std::back_inserter as first iterator which basicly is used to insert elements.
I want to fill a container by consequtive values of iterators to elements of another container
a different solution which uses generate_n:
live
std::vector<int> src = {0,1,2,3};
std::vector<std::vector<int>::iterator> dst;
std::generate_n(std::back_inserter(dst), src.size(), [it=src.begin()]() mutable {return it++;});
Your question includes an iota implementation which is different than the one in the standard I believe. Here is the standard version I know http://en.cppreference.com/w/cpp/algorithm/iota.
Your iota (which I will rename it as miota in my code) allows different type of iterators for begin and end.
What you want in the algorithm is; end sentinel needs to be different from begin (the inserter) until all values are processed. For processing values you only take one object and you use increment and copy-construction on that object.
Therefore, your end sentinel should know about the value processing and when finished the end sentinel should become equal to the inserter somehow.
I did it via holding begin/end iterators of the original container in a class called IotaHelper. This uses shared_ptr for sharing state with the sentinel class which is called IotaEndSentinel.
When you increment the value inside miota, it actually increments the begin iterator of the IotaHelper. When you check equality with the inserter and the sentinel it actually checks the iterator equality inside the IotaHelper.
All code with a basic example is here:
#include <iterator>
#include <numeric>
#include <vector>
#include <iostream>
#include <utility>
#include <memory>
template< typename ForwardIterator, typename EndSentinel, typename T >
void miota(ForwardIterator first, EndSentinel last, T value)
{
for (; first != last; ++first) {
*first = value;
++value;
}
}
template<typename Container>
struct IotaHelper
{
using Iterator = typename Container::iterator;
using IteratorPair = std::pair<Iterator, Iterator>;
IotaHelper(Iterator begin, Iterator end)
:
pair(std::make_shared<IteratorPair>(begin, end))
{ }
operator Iterator()
{
return pair->first;
}
IotaHelper& operator++()
{
++pair->first;
return *this;
}
std::shared_ptr<IteratorPair> pair;
};
template<typename Container>
struct IotaEndSentinel
{
using Helper = IotaHelper<Container>;
using Iterator = typename Helper::Iterator;
IotaEndSentinel(const Helper& helper)
:
helper(helper)
{}
template<typename C>
friend bool operator!=(const std::back_insert_iterator<C>& bii,
const IotaEndSentinel& sentinel)
{
return sentinel.helper.pair->first != sentinel.helper.pair->second;
}
Helper helper;
};
int main()
{
using Container0 = std::vector<int>;
using Container1 = std::vector<Container0::iterator>;
Container0 c0 = {1, 2, 3, 4, 5};
Container1 c1;
IotaHelper<Container0> iotaHelper(c0.begin(), c0.end());
miota(std::back_inserter(c1),
IotaEndSentinel<Container0>(iotaHelper),
iotaHelper);
std::cout << "Result: ";
for (auto iter : c1)
{
std::cout << *iter << ", ";
}
std::cout << std::endl;
}
I have tried to do this because it was fun. But please don't use this method for hacking output iterators like back_insert_iterator and make a generic method for yourself for different containers.
template<typename SourceContainer, typename IteratorContainer>
void FillIterators(SourceContainer& sc, IteratorContainer& ic)
{
for (auto iter = sc.begin(); iter != sc.end(); ++iter)
{
ic.insert(ic.end(), iter);
}
}
EDIT:
After using heap-allocation that code was smelling to me. Instead of trying to reason about the "value and the process" we can reason about the "iterators and the process".
We can build an iterator-wrapper which contains the process iterator and the insert iterator together.
When the algorithm needs to dereference the wrapper, it will return the insert iterator.
When the algorithm needs to compare to other "wrapper or sentinel", wrapper will compare the process iterator.
In the end we can use such iterator for both std::iota and your miota.
Complete example is here:
#include <iterator>
#include <numeric>
#include <vector>
#include <iostream>
#include <utility>
#include <memory>
template< typename ForwardIterator, typename EndSentinel, typename T >
void miota(ForwardIterator first, EndSentinel last, T value)
{
for (; first != last; ++first) {
*first = value;
++value;
}
}
template<typename InsertIterator, typename Iterator>
struct InsertWrapper
{
InsertWrapper(const InsertIterator& inserter, const Iterator& iter)
:
inserter(inserter),
iter(iter)
{ }
bool operator!=(const InsertWrapper& other) const
{
//only compare process iterators
return iter != other.iter;
}
bool operator!=(const Iterator& sentinel) const
{
//compare process iterator against the sentinel
return iter != sentinel;
}
InsertIterator& operator*()
{
//return inserter for dereference
return inserter;
}
InsertWrapper& operator++()
{
//iterate inserter as the process progresses
++inserter;
++iter;
return *this;
}
InsertIterator inserter;
Iterator iter;
};
template<typename InsertIterator, typename Iterator>
InsertWrapper<InsertIterator, Iterator> WrapInserter(const InsertIterator& inserter,
const Iterator& iter)
{
return InsertWrapper<InsertIterator, Iterator>(inserter, iter);
}
int main()
{
using Container0 = std::vector<int>;
using Container1 = std::vector<Container0::iterator>;
Container0 c0 = {1, 2, 3, 4, 5};
Container1 c1;
//use wrapper as usual iterator begin/end
std::iota(WrapInserter(std::back_inserter(c1), c0.begin()),
WrapInserter(std::back_inserter(c1), c0.end()),
c0.begin());
std::cout << "std::iota result: ";
for (auto iter : c1)
{
std::cout << *iter << ", ";
}
std::cout << std::endl;
c1.clear();
miota(WrapInserter(std::back_inserter(c1), c0.begin()),
c0.end(), //end iterator as sentinel
c0.begin());
std::cout << "miota result: ";
for (auto iter : c1)
{
std::cout << *iter << ", ";
}
std::cout << std::endl;
}

Declaring a generic iterator

I have the following problem: I need to make a function which takes two iterators and a value and checks if the value is found between the two. The catch: I can only have one template parameter which denotes the type of the elements from the iterators and the value.
My try is like this, but doesn't seem to work:
template <typename T>
T myFind(iterator<std::bidirectional_iterator_tag,T> begin, iterator<std::bidirectional_iterator_tag, T> end, T elem){
// Code
}
But this doesn't work then:
// vector<int> vect; list<string> lst;
myFind(vect.begin(), vect.end(), 15);
myFind(lst.begin(), lst.end(), "some element");
Any ideas?
Code after changes:
template <typename T>
T myFind(T begin, T end,typename std::iterator_traits<T>::value_type elem){
for(T it = begin; it != end; ++it){
if(*it == elem){
return it;
}
}
return end;
}
Can you have one template parameter that is the iterator type? If so:
template <typename It>
typename std::iterator_traits<It>::value_type
myFind(It begin, It end, typename std::iterator_traits<It>::value_type elem){
// ...
}
Otherwise, I think your restriction is too strong.
After your edit: If you want to do - on the iterator that is returned (as you show you do in the comments), you need a random access iterator. However, std::list::iterator is a bidirectional iterator so you can't. You will need to use std::prev (or in C++03, use std::advance).

Compiler error with using std::list

I have this simple function to check if a value is in list:
template <class T>
bool IsinList(list<T> l, T x)
{
for(list<T>::iterator it=list.begin(); it != list.end(); it++)
{
if (*it == x)
return true;
}
return false;
}
I used the function in the same .cpp file like this:
if (!IsinList (words, temp))
goodwords.push_back(temp);
but I am getting this error :
'std::list' : use of class template requires template argument list
and I cant figure out what the problem is. I checked in previous asked questions and it didn't help.
Can you explain to me what am I doing wrong?
Typo there:
list.begin() / list.end()
should be
l.begin() / l.end()
Your variable is called l, not list.
EDIT:
As Martinho pointed out, this might not be enough. Some compilers will accept this, but since the iterator depends on the template argument, you might need a typename:
typename list<T>::iterator it=list.begin()
You made a typo (list vs. l) and did not specify that list<T>::iterator is a typename. Further, you should pass the list and the search argument by reference-to-const. All in all, it should look like this:
template <class T>
bool IsinList(const list<T>& l, const T& x)
{
typename list<T>::const_iterator first = l.begin(), last = l.end();
for(; first != last; ++first)
{
if (*it == x)
return true;
}
return false;
}
That said, still don't use this. Much better to use std::find instead
if (std::find(words.begin(), words.end(), temp)==words.end())
{
goodwords.push_back(temp);
}

How to neatly move some elements from std::set to another container?

Since iterator invalidates upon remove operation, how do I iterate with the least amount of code over all the elements of std::set, removing some of them ?
// Filters the elements of a set: elements that satisfy the predicate 'pred'
// are removed from the source set and inserted into the output.
template <typename TSet, typename TOutputIterator, typename TPredicate>
void extract_if(TSet& s, TOutputIterator out, TPredicate pred)
{
for (typename TSet::iterator it(s.begin()); it != s.end();)
{
if (pred(*it))
{
*out++ = *it;
it = s.erase(it);
}
else
{
++it;
}
}
}

Find index of iterator in STL container - need template function

I want to have a function with interface like this:
template<typename T, typename R> int find_index (const T& list, const R& value);
As I know, there is find() in STL that returns iterator. I need to return index of iterator (even for non-indexed containers such as std::list). I tried this code:
template<typename T, typename R>
int find_index (const T& list, const R& value)
{
int index = 0;
for (T::const_iterator it = list.begin(); it != list.end(); it++, index++)
if ((*it) == value)
return index;
return -1;
}
But compiler shows error on it - seems like it is not allowed to get const_iterator from templated typename. Can I go around it?
At the worst case I can pass begin and end iterators to find_index arguments, but it looks not so fine. Would be thankful for elegant solution.
for (typename T::const_iterator it = list.begin(); it != list.end(); ++it, ++index)
should solve your problem.
When using dependent types (types depending on template parameters), the compiler does not know that const_iterator is a type until it instantiates the template with a concrete type, it could also just be a static variable or whatever. Using the typename keyword, you tell him that const_iterator is really a type.
In C++11 you can also circumvent the whole typename issue using the auto keyword:
for (auto it = list.begin(); it != list.end(); ++it, ++index)
If you already have the iterator (maybe from some other operation), you can also just compute the distance from the list's begin to this iterator:
#include <iterator>
int index = std::distance(list.begin(), it);
But since this has linear complexity for a std::list, using your self-made find_index function is a better idea than std::find followed by std::distance, at least performance-wise.