C++ template function using iterators - c++

I'm sorry this must be a really simple question...
I'm a beginner of C++ and I was trying to write a trivial quicksort function using function template.
#include <iostream>
#include <vector>
using namespace std;
template <class iterator, class val>
void move_to_front(iterator movethis, iterator leftend) {
val temp = *movethis; // hold the element being moved
for (iterator i = movethis; i != leftend; i--) {
*i = *(i-1);
} // all other elements shift right
*leftend = temp; // put it back to the front
}
template <class iterator>
void qsort(iterator begin, iterator end) {
iterator oldbegin = begin;
for (iterator i = begin + 1; i != end; i++) {
if (*i <= *begin) {
move_to_front(i, begin);
oldbegin++;
}
} // move elements smaller than or equal to the first element
// to the left of the first element.
// oldbegin indicates the first element, so it + 1 every time an
// element is moved to its left.
qsort(begin, oldbegin);
qsort(oldbegin, end);
}
int main(int argc, char const *argv[]) {
int test[] = {8,7,2,4,1,4,5,4,2};
vector<int> ar(test, test+9);
qsort(ar.begin(), ar.end());
for (vector<int>::iterator i = ar.begin(); i != ar.end(); i++) cout << *i;
return 0;
}
The compiler complains
/Users/Larry_Li/Project Euler/foo.cpp:20:11: error: no matching function for call to 'move_to_front'
move_to_front(i, begin);
^~~~~~~~~~~~~
/Users/Larry_Li/Project Euler/foo.cpp:31:7: note: in instantiation of function template specialization 'qsort<std::__1::__wrap_iter<int *> >' requested here
qsort(ar.begin(), ar.end());
^
/Users/Larry_Li/Project Euler/foo.cpp:6:10: note: candidate template ignored: couldn't infer template argument 'val'
void move_to_front(iterator movethis, iterator leftend) {
^
1 error generated.
I think I was defining the template wrong somehow... especially the val part in template <class iterator, class val>,
May I ask how to make it work?

The problem is that the compiler cannot deduce one of your template arguments here:
template <class iterator, class val>
void move_to_front(iterator movethis, iterator leftend) {
val temp = *movethis; // hold the element being moved
There is no function parameter of type val, so there's no way for the compiler to know what that type should be. But you don't need it anyway. If you have C++11 available, you could use auto, as Scooby suggested. Otherwise, you could use std::iterator_traits to get the value type from the iterator type:
template <class iterator>
void move_to_front(iterator movethis, iterator leftend) {
typename std::iterator_traits<iterator>::value_type temp = *movethis;

This probably won't help directly, as you appear to be intentionally reinventing the wheel, and/or using this scenario to test and expand your template knowledge rather than to solve the exact problem your code is written to solve. But just in case all you want to do is implement move_to_front, consider std::rotate:
#include <algorithm>
template <class iterator>
void move_to_front(iterator movethis, iterator leftend) {
std::rotate(leftend, movethis, movethis + 1);
}
You may also want to consider changing the order of your arguments to be more in line with the STL, but that's up to you. See a live example (the example uses C++11 but the function does not require it).

Try this, assuming you are using C++11:
template <class iterator>
void move_to_front(iterator movethis, iterator leftend) {
auto temp = *movethis; // hold the element being moved
for (iterator i = movethis; i != leftend; i--) {
*i = *(i-1);
} // all other elements shift right
*leftend = temp; // put it back to the front
}
But, in my opinion, I thik you're overcomplicating with all the iterators.

Related

boost::transform_iterator and std::iter_swap

I am trying to generalize a function I have which used to take two iterators to a vector of a specific data-structure, and re-arrange the elements in a certain way using std::iter_swap (like std::sort does).
Since this function only actually needs a subset of the data, and I will need to use it in other contexts in the future, I thought about removing the dependency on the data structure, and use boost::transform_iterator at the point of call to handle the transformation.
Unfortunately, it seems that boost::transform_iterator is not happy with this change. I can imagine why: std::iter_swap is usually implemented as std::swap(*lhs, *rhs), and dereferencing the transform_iterator does not yield the original element to swap in the correct way.
I was wondering if there was a way to handle this case. I am open to use boost::range or the experimental std::ranges ts if it needed.
This question is probably similar to this one, but even there the solution ends up modifying the subset of data the algorithm needs, rather than the outside structure.
Here is an MWE:
#include <boost/iterator/transform_iterator.hpp>
#include <vector>
#include <algorithm>
#include <iostream>
struct A {
int x;
int y;
};
template <typename It>
void my_invert(It begin, It end) {
while (begin < end) {
std::iter_swap(begin++, --end);
}
}
template <typename It>
void my_print(It begin, It end) {
for (; begin != end; ++begin)
std::cout << (*begin) << ' ';
std::cout << '\n';
}
int main() {
std::vector<int> x{7,6,5,4,3,2};
my_invert(std::begin(x), std::end(x));
my_print(std::begin(x), std::end(x));
auto unwrap = +[](const A & a) { return a.x; };
std::vector<A> y{{9,8}, {7,6}, {5,4}, {3,2}};
auto begin = boost::make_transform_iterator(std::begin(y), unwrap);
auto end = boost::make_transform_iterator(std::end(y), unwrap);
//my_invert(begin, end); // Does not work.
my_print(begin, end);
return 0;
}
Accessing the underlying iterator
You could access the base() property of transform_iterator (inherited publicly from iterator_adaptor) to implement your custom transform_iter_swap, for swapping the underlying data of the wrapped iterator.
E.g.:
template<class IteratorAdaptor>
void transform_iter_swap(IteratorAdaptor a, IteratorAdaptor b)
{
std::swap(*a.base(), *b.base());
}
template <typename It>
void my_invert(It begin, It end) {
while (begin < end) {
transform_iter_swap(begin++, --end);
}
}
After which your example (omitting the std::vector part) runs as expected:
my_invert(begin, end); // OK
my_print(begin, end); // 3 5 7 9
If you want a general function template to cover both the boost (adaptor) iterators as well as typical iterators, you could e.g. use if constexpr (C++17) based on whether the iterators public typedef iterator_category derives from boost::iterators::no_traversal_tag or not:
// expand includes with
#include <boost/iterator/iterator_categories.hpp>
template <class It>
void iter_swap(It a, It b) {
if constexpr(std::is_base_of<
boost::iterators::no_traversal_tag,
typename It::iterator_category>::value) {
std::swap(*a.base(), *b.base());
}
else {
std::swap(*a, *b);
}
}
template <typename It>
void my_invert(It begin, It end) {
while (begin < end) {
iter_swap(begin++, --end);
}
}
The problem comes from the unary predicate you've passed. Notice, that since you allow the return type to be deduced, the return type is deduced to be an int, a copy is returned, and the compilation fails when you try to swap two unmodifiable ints. However, if you were to specify the return type to be int&, like so:
auto unwrap = [](A & a)->int& { return a.x; }; // explicitly demand to return ref
It will compile, and reverse the elements. Tested on gcc 8.1.0 and clang 6.0.0.

Receive iterators as arguments and know class

I need to create a method that receives iterators as arguments and return a templated collection of the same type.
I created a minimal example the demonstrates my need:
#include <list>
#include <iostream>
using namespace std;
class A {
int i;
public:
A(int _i) : i(_i) {}
operator int() {
return i;
}
};
class B {
int i;
public:
B(int _i) : i(_i) {}
operator int() {
return i;
}
};
template <class T>
list<T> method(typename list<T>::iterator begin, typename list<T>::iterator end) {
list<T> res; // I need the template class here
for (; begin != end; begin++) {
cout << *begin; // Whatever...
}
return res;
}
int main() {
list<A> listA = {1, 2, 3};
list<B> listB = {4, 5, 6};
auto res1 = method(listA.begin(), listA.end()); // Cannot change
auto res2 = method(listB.begin(), listB.end()); // Cannot change
}
This is not a working example, but I am looking for a way to make this work.
The important part is the method and it's arguments, and that it will return a templated class with T. So I can change the method as much as i want but the auto res1 = method(listA.begin(), listA.end()); should stay the same. (not my code)
How can I do something like this?
In this particular case (if you know that it's std::list) you can get value_type from iterator itself:
template <class T>
auto method(T begin, T end) {
list<typename T::value_type> res; // I need the template class here
for (; begin != end; begin++) {
cout << *begin; // Whatever...
}
return res;
}
value_type = U only for std::list<U>
typename list<T>::iterator is a non-deduced context. You need to either pass the container itself or specify it as an explicit template argument.
Iterators designate a sequence; they do not have to be associated with a "collection". For example, you can create an iterator that reads input from the console.
If you want to restrict a function to only work with containers, then write it that way:
template <class T>
T f(const T& container) {
// whatever; code that uses begin() and end()?
}
This isn't possible. You can have iterators that don't correspond to any container. The function that calls method should create an empty list and pass a back_inserter to the list as the third argument to method. Use std:copy as an example of what your method should look like.

Return template iterator based on argument class

I'm writing an algorithm function that uses iterators. This function should work with both normal and constant iterators, and importantly the class that these iterators come from is NOT a template, I know it in advance.
Is there any way to enforce in the following definition that the iterators come from a specific class?
// This is an example, A could be any other class with exposed iterators.
using A = std::vector<int>;
// How to enforce that Iterator is an iterator from A?
template <typename Iterator>
Iterator foo(Iterator begin, Iterator end);
...
A a;
auto it = foo(a.begin(), a.end());
*it = 4; // Must compile
// --------
const A a;
auto it = foo(a.begin(), a.end());
*it = 4; // Must not compile
// --------
B b;
auto it = foo(b.begin(), b.end()); // Should not compile.
In this case, foo does not modify directly the supplied range, but allows for modification of the result iterator if the supplied range was modifiable in the first place. It would be nice if this could be done without replicating code.
Simply don't use template:
A::iterator foo(A::iterator begin, A::iterator end);
You might use std::enable_if:
#include <type_traits>
#include <vector>
class X : public std::vector<int> {};
class Y : public std::vector<double> {};
template <typename Iterator>
typename std::enable_if<std::is_same<Iterator, X::iterator>()
|| std::is_same<Iterator, X::const_iterator>(),
Iterator>::type
foo(Iterator begin, Iterator end) {
return begin;
}
int main() {
X x0;
auto i0 = foo(x0.begin(), x0.end());
*i0 = 4; // Must compile
const X x1;
auto i1 = foo(x1.begin(), x1.end());
// error: assignment of read-only location
//*i1 = 4; // Must not compile
Y y;
// error: no type named ‘type’ in ‘struct std::enable_if ...
//auto i2 = foo(y.begin(), y.end()); // Should not compile
}
Or static_assert as a nicer alternative:
template <typename Iterator>
Iterator foo(Iterator begin, Iterator end) {
static_assert(std::is_same<Iterator, X::iterator>()
|| std::is_same<Iterator, X::const_iterator>(),
"No X::iteator or X::const_iterator");
return begin;
}
You could use a function overload check:
inline void check_must_be_iterator_from_A(A::iterator) {}
inline void check_must_be_iterator_from_A(A::const_iterator) {}
template <typename I>
I foo(I a, I b) {
typedef void (*must_be_iterator_from_A)(I);
must_be_iterator_from_A c = &check_must_be_iterator_from_A;
//...
}
The other option is to use template specialization to create a constraint, which makes the code within the function terser and definitely without runtime penalty regardless of compiler:
template <typename I> struct is_iterator_from_A;
template <> struct is_iterator_from_A<A::iterator>{ enum {ok}; };
template <> struct is_iterator_from_A<A::const_iterator>{ enum {ok}; };
template <typename I>
I bar(I a, I b) {
is_iterator_from_A<I>::ok;
return a;
}
I would first write your function for const iterators and then write a wrapper around it for the non_const case:
A::const_iterator foo(A::const_iterator start, A::const_iterator end) {
std::cout << " Called foo with const iterators " << std::endl;
return start;
}
A::iterator foo(A::iterator start, A::iterator end) {
std::cout << " Called foo with non_const iterators " << std::endl;
auto it = foo(static_cast<A::const_iterator>(start), static_cast<A::const_iterator>(end));
return start + std::distance(static_cast<A::const_iterator>(start), it);
}
If you inline the wrapper function you should get zero (or nearly zero) overhead.
EDIT:
In case your container doesn't provide random access iterators distance has linear complexity and you will have to use std::advance instead of the "+"-operator, so depending on your performance requirements this might not be a viable solution for you.

c++ proper syntax for templated function of `std::vector<T>::iterator` [duplicate]

This question already has an answer here:
C++ template won't accept iterators
(1 answer)
Closed 9 years ago.
I'm trying to code a template that takes iterators to any type of vector as its arguments. When I try to compile the following, it gives me a no matching function call error.
#include <vector>
struct A { int x; };
template <class T>
void process (typename std::vector<T>::iterator begin,
typename std::vector<T>::iterator end)
{ for(; begin != end; begin++) { /*do_something*/ } }
int main()
{
std::vector <A> obj;
process(obj.begin(), obj.end());
}
1 the type T cannot be deduced from the argument types.
2 Why would you want to restrict the function to only accept iterators to std::vector elements? If you really only want vector elements, better take std::vector<T> const& as argument. But better simply take any iterator arguments (or any container argument).
edit okay, here is an example. You may omit the static_assert, when this becomes identical (apart from the return type) to std::for_each(begin,end,do_something);
template <class It>
void process(It begin, const It end)
{
static_assert(std::is_same<typename std::iterator_traits<It>::iterator_category,
std::random_access_iterator_tag>::value,
"arguments not random access iterators");
for (; begin != end; ++begin)
do_something(*begin);
}
Per the OP's request, see below. you can use any valid container forward iterator that supports value references from operator *() I.e. a vector, deque, list, etc. This does not employ the static assert logic mentioned by chris, I leave that for you to decide.
#include <iostream>
#include <iterator>
template<typename Iterator>
void process(Iterator start, Iterator stop)
{
typedef typename std::iterator_traits<Iterator>::value_type value_type;
for (Iterator it=start; it != stop; ++it)
{
const value_type& val = (*it);
// do something with val
std::cout << val << std::endl;
}
}
int main()
{
int ar[] = { 1,2,3,4,5 };
process(std::begin(ar), std::end(ar));
return 0;
}

C++ compiler couldn't derive a right type from iterator

I have the following function:
template <typename Iterator>
void merge(Iterator begin, Iterator middle, Iterator end) {
std::vector<T> first(begin, middle), second(middle, end);
auto i1 = first.begin();
auto i2 = second.begin();
while (begin != end) {
if (i2 == second.end() || *i1 < *i2) {
*begin = *i1;
++i1;
} else {
*begin = *i2;
++i2;
}
++begin;
}
}
But when I try to use it:
int data[] = {1,2,3,4,5,6,7,8};
merge(data, data + 4, data + 8);
I get a error:
candidate template ignored: couldn't infer template argument 'T'
How can I define the merge function right without explicit specifying of the T type?
How is compiler supposed to guess what T is? You neither provided any context to deduce it from nor did you explicitly tell it (in any case, T would need to be declared in template parameter list, like template<typename T, typename Iterator>).
I'd use iterator_traits here:
typedef typename std::iterator_traits<Iterator>::value_type T;
std::vector<T> first(begin, middle), second(middle, end);
Use std::iterator_traits to extract the type you need instead of having T being a template argument (which the error is really hinting at, despite your code):
std::vector<typename std::iterator_traits<Iterator>::value_type> first(begin, middle), second(middle, end);