+= on a vector without boost - c++

Is there any way to use the += operator with a vector without using boost or using a derivated class?
Eg.
somevector += 1, 2, 3, 4, 5, 6, 7;
would actually be
somevector.push_back(1);
somevector.push_back(2);
somevector.push_back(3);
etc.

With a little ugly operator overloading, this isn't too difficult to accomplish. This solution could easily be made more generic, but it should serve as an adequate example.
#include <vector>
Your desired syntax uses two operators: the += operator and the , operator. First, we need to create a wrapper class that allows us to apply the , operator to push an element onto the back of a vector:
template <typename T>
struct push_back_wrapper
{
explicit push_back_wrapper(std::vector<T>& v) : v_(&v) { }
push_back_wrapper& operator,(const T& x)
{
v_->push_back(x);
return *this;
}
std::vector<T>* v_;
};
Then, in order to use this in conjunction with += on a vector, we overload the += operator for a vector. We return a push_back_wrapper instance so that we can chain push backs with the comma operator:
template <typename T, typename U>
push_back_wrapper<T> operator+=(std::vector<T>& v, const U& x)
{
v.push_back(x);
return push_back_wrapper<T>(v);
}
Now we can write the code you have in your example:
int main()
{
std::vector<int> v;
v += 1, 2, 3, 4, 5, 6, 7;
}
The v += 1 will call our operator+= overload, which will return an instance of the push_back_wrapper. The comma operator is then applied for each of the subsequent elements in the "list."

Not with syntax like that, no. But you could do something like this:
int tmparray[] = {1, 2, 3, 4, 5, 6, 7};
somevector.insert(somevector.end(),
tmparray,
tmparray + (sizeof(tmparray) / sizeof(tmparray[0])));

Related

polymorphism between set and multiset in c++

Is there a way using polymorphism to have a generic for set and multiset? as follows.
Note: but only for sets, (set, multiset)
template<typename T>
void foo(parent_set<T> &s) {
// do something
}
// main
set<int> s1 = {1, 2, 3, 4, 5};
foo(s1);
multiset<int> s2 = {1, 2, 2, 2, 2, 3};
foo(s2);
Well, why don't you make the whole container your template parameter?
template <class SetType>
void foo( SetType& s)
{
using T = typename SetType :: value_type;
enter code here
....
}
The restriction for the parameter to only be a set or multiset, as is usually done with templates, is enforced by the usage of the template parameter as a set. E.g. you call insert with a single parameter, and therefore you cannot pass a vector. If there is a third, unknown, container that has all the required interface, maybe it makes sense to allow it as well?

Using brace-init for a non-trivial multi-variable class

I am trying to make a certain template class brace-initializable, e.g.
template<typename T>
class A {
private:
std::vector<T> _data;
std::size_t _m;
std::size_t _n;
public:
Matrix(std::size_t m, std::size_t n, const T &fill); // regular (non-trivial) constructor
Matrix(std::initializer_list<T> list);
};
However, I'm having trouble coming up with the implementation. I want to be able to do:
A<int> a = {{1, 2, 3, 4}, 2, 2};
// or something similar...e.g. C++11 style brace-init
A<int> a {{1, 2, 3, 4}, 2, 2};
I've tried:
template<typename T>
Matrix<T>::Matrix(std::initializer_list<T> list)
: _data(*list.begin()),
_m(*(list.begin() + 1)),
_n(*(list.begin() + 2)) {}
But that doesn't work for me. Help!
In order to convert from an initializer_list to a vector you may copy all the elements.
STL makes this pretty nice with the begin/end iterators. Here's all the possible constructors for vector
Matrix(std::initializer_list<T> list, std::size_t m, std::size_t n)
: _data(list.begin(), list.end()) // This is what might help
, _m(m)
,_n(n)
{
}

Insert into vector with conditional iterator

Say I have a vector with various entries, which I want to insert into another vector, while leaving out entries that satisfy a condition.
For example, I want to insert a vector while leaving out all three's.
{1, 3, 2, 3, 4, 5, 3} -> { /* previous content, */ 1, 2, 4, 5}
What I came up with so far uses std::partition, which does not preserve the relative order and rearranges the source vector.
std::vector<int> source({1, 3, 2, 3, 4, 5, 3});
std::vector<int> target;
auto partition = std::partition(std::begin(source),
std::end(source), [](const auto& a) { return a == 3; });
target.insert(std::begin(target), partition, std::end(source));
What I am looking for is more of an iterator that checks a condition and moves on if the condition is not satisfied. Something like this:
target.insert(std::begin(target),
conditional_begin(source, [](const auto& a) { return a != 3; }),
conditional_end(source));
I suppose a conditional_end function would be necessary, since std::end would return a different iterator type than conditional_begin.
Maybe I have overlooked something, so my questions are:
Does the standard library provide something similar?
Is there a different easy way to achieve my goal?
Is there an easy way to implement the conditional iterator functionality?
Is there a different easy way to achieve my goal?
Yes, the standard already has this functionality built in. The function you are looking for is std::copy_if.
std::vector<int> source({1, 3, 2, 3, 4, 5, 3});
std::vector<int> target;
std::copy_if(source.begin(),
source.end(),
std::back_inserter(target), [](auto val){ return val != 3; });
Here, std::back_inserter(target), will call push_back on target for each element that the predicate returns true.
Yes, you can create a custom iterator that does what you want but it is currently a little tedious to create custom iterators using standard C++. It would look something like this:
template <typename Itr, typename F>
struct ConditionalIterator {
Itr itr;
Itr end;
F condition;
using value_type = typename Itr::value_type;
using difference_type = typename Itr::difference_type;
using pointer = typename Itr::pointer;
using reference = typename Itr::reference;
using iterator_category = std::forward_iterator_tag;
ConditionalIterator() = default;
ConditionalIterator(Itr itr, Itr end, F condition): itr(itr), end(end), condition(condition) {}
bool operator!=(const ConditionalIterator &other) const { return other.itr != itr; }
reference operator*() const { return *itr; }
pointer operator->() const { return &(*itr); }
ConditionalIterator& operator++() {
for (; ++itr != end;) {
if (condition(*itr))
break;
}
return *this;
}
ConditionalIterator operator++(int) {
ConditionalIterator ret(*this);
operator++();
return ret;
}
};
You can then create something like the conditional_begin and conditional_end helper functions you asked for. The only issue is that std::vector::insert expects the two iterators to have the same type. If we use a lambda for our condition then this will be part of the type of our conditional iterator. So we need to pass the lambda to both helper functions so that they return iterators with matching types:
template <typename C, typename F>
auto conditional_begin(const C &source, F f) {
return ConditionalIterator<typename C::const_iterator, F>(source.begin(),
source.end(), f);
}
template <typename C, typename F>
auto conditional_end(const C &source, F f) {
return ConditionalIterator<typename C::const_iterator, F>(source.end(),
source.end(), f);
}
Which you could call with a lambda like this:
auto condition = [](const auto &a) { return a != 3; };
target.insert(std::begin(target),
conditional_begin(source, std::ref(condition)),
conditional_end(source, std::ref(condition)));
Live demo.
My crude tests show, in this case, this ends up being significantly faster than simply using copy_if and back_inserter because std::vector::insert first works out how much memory to allocate before inserting. Just using back_inserter will cause multiple memory allocations. The difference in performance will depend on how expensive the condition is to evaluate. You can get the same speedup by using count_if to reserve enough space before using copy_if:
auto count = static_cast<size_t>(std::count_if(source.begin(),
source.end(), condition));
target.reserve(target.size() + count);
std::copy_if(source.begin(),
source.end(),
std::back_inserter(target), condition);
Live demo.
As ranges will be standardized soon, this is an alternative using range-v3, the reference library for the proprosal:
#include <range/v3/view/concat.hpp>
#include <range/v3/view/filter.hpp>
using namespace ranges;
const std::vector<int> source{1, 3, 2, 3, 4, 5, 3};
const std::vector<int> target = view::concat(source,
source | view::filter([](auto i){ return i != 3; }));

Wide variety of unambiguous constructors for the same class

I have the following class:
class Foo
{
public:
// Constructors here
private:
std::vector<X> m_data; // X can be any (unsigned) integer type
};
I want the following code to work:
Foo f0;
Foo f1(1); // and/or f1({1})
Foo f2(1, 2); // and/or f2({1, 2})
Foo f3(1, 2, 3); // and/or f3({1, 2, 3})
Foo f4(1, 2, 3, 4); // ... and so on
std::vector<int> vec = {1, 2, 3, 4};
Foo f5(vec);
Foo f6(vec.begin(), vec.end());
std::list<std::size_t> list = {1, 2, 3, 4};
Foo f7(list);
Foo f8(list.begin(), list.end());
std::any_iterable_container container = {1, 2, 3, 4};
Foo f9(container);
Foo f10(container.begin(), container.end());
// PS: I guess I only want containers/iterators that store individual
// values and not pairs (e.g., I don't want care about std::map
// because it does not make sense anyway).
So far I have tried to combine SFINAE, constructor overloading with all possible types, variadic templates, etc. Every time I fix one constructor case, others break down. Also, the code I write becomes very complex and hard to read. However, the problem seems quite simple and I guess I am just approaching it in a wrong way. Any suggestions on how to write the constructors (ideally in C++17), while also keeping the code as simple as possible, is more than welcome.
Thank you.
The simplest way to implement f1-f4 (which seem to take a variable number of arguments of a known type T that is not a container or iterator) ist this:
template<typename... Args>
Foo(T arg, Args... args) {...}
As this constructor takes at least 1 argument, there is no ambiguity with the default constructor f0. As the first argument is of type T, there is no ambiguity with the following constructors.
If you want to treat std::vector and std::list differently than other containers, you can create a partly specialized helper template to check if an argument is an instance of a given template:
template<typename>
struct is_vector : std::false_type {};
template<typename T, typename Allocator>
struct is_vector<std::vector<T, Allocator>> : std::true_type {};
And use it like this to implement f5 and f7:
template<typename T, Constraint = typename std::enable_if<is_vector<typename std::remove_reference<T>::type>::value, void>::type>
Foo(T arg) {...}
By testing for the respective iterator types of std::vector and std::list you can implement f6 and f8 in the same way.
You can check for the presence of member functions begin() and end() to implement f9 (I suppose) like this:
template<typename T>
Foo(T arg, decltype(arg.begin())* = 0, decltype(arg.end())* = 0) {...}
However, you'll have to explicitly disable this constructor for std::vector and std::list using the helper templates you created to avoid ambiguity.
To check if an argument is some iterator to implement f10, you can use std::iterator_traits:
template<typename T, typename Constraint = typename std::iterator_traits<T>::iterator_category>
Foo(T begin, T end) {...}
Again, you'll have to explicitly disable this constructor for the iterator types of std::vector and std::list.
The idea is to define the class like this:
template <typename X>
class Foo
{
public:
Foo() { };
Foo(initializer_list<int> l) :m_data(l) { };
template<typename container>
Foo(container const & c) :m_data(c.begin(), c.end()) {};
template<typename iterator>
Foo(iterator begin, iterator end) :m_data(begin, end) { };
private:
std::vector<X> m_data;
};
Where:
Foo() is the default (non-parametric) constructor.
Foo(initializer_list<int> l) accepts a list like {1, 2, 3}.
Foo(container const & c) accepts any container that supports begin and end iterators.
Foo(iterator begin, iterator end) initializes the class with begin and end iterators.
Usage:
Foo<int> f0;
Foo<int> f1({1});
Foo<int> f2({1, 2});
Foo<int> f3({1, 2, 3});
Foo<int> f4({1, 2, 3, 4});
std::vector<int> vec = {1, 2, 3, 4};
Foo<int> f5(vec);
Foo<int> f6(vec.begin(), vec.end());
std::list<size_t> list = {1, 2, 3, 4};
Foo<size_t> f7(list);
Foo<size_t> f8(list.begin(), list.end());
set<unsigned> container = {1, 2, 3, 4};
Foo<unsigned> f9(container);
Foo<unsigned> f10(container.begin(), container.end());
Assuming the class is defines as following:
template <class T>
class Foo
{
public:
[..]
private:
std::vector<T> m_data;
}
Let's break this task into sub-tasks:
Construct from iterators
template <class Iterator>
Foo (Iterator begin, Iterator end, typename Iterator::iterator_category * = 0)
: m_data(begin, end);
We will fill in our m_data from begin and end.
The third parameter will make sure only Iterator types that declare iterator_category will match this prototype. Since this argument has a default value of 0 and is never specified, it serves a purpose only during the template deduction process. When the compiler checks if this is the right prototype, if the type Iterator::iterator_category doesn't exist, it will skip it. Since iterator_category is a must-have type for every standard iterator, it will work for them.
This c'tor will allow the following calls:
std::vector<int> vec = {1, 2, 3, 4};
Foo<int> f(vec.begin(), vec.end());
-- AND --
std::list<std::size_t> list = {1, 2, 3, 4};
Foo<int> f(list.begin(), list.end());
Construct from container
template <class Container>
Foo (const Container & container, decltype(std::begin(container))* = 0, decltype(std::end(container))* = 0)
: m_data(std::begin(container), std::end(container));
We will fill our m_data from the given container. We iterate over it using std::begin and std::end, since they are more generic than their .begin() and .end() counterparts and support more types, e.g. primitive arrays.
This c'tor will allow the following calls:
std::vector<int> vec = {1, 2, 3, 4};
Foo<int> f(vec);
-- AND --
std::list<std::size_t> list = {1, 2, 3, 4};
Foo<int> f(list);
-- AND --
std::array<int,4> arr = {1, 2, 3, 4};
Foo<int> f(arr);
-- AND --
int arr[] = {1, 2, 3, 4};
Foo<int> f(arr);
Construct from an initializer list
template <class X>
Foo (std::initializer_list<X> && list)
: m_data(std::begin(list), std::end(list));
Note: We take the list as an Rvalue-reference as it's usually the case, but we could also add a Foo (const std::initializer_list<X> & list) to support construction from Lvalues.
We fill in our m_data by iterating over the list once again. And this c'tor will support:
Foo<int> f1({1});
Foo<int> f2({1, 2});
Foo<int> f3({1, 2, 3});
Foo<int> f4({1, 2, 3, 4});
Constructor from variable number of arguments
template <class ... X>
Foo (X ... args) {
int dummy[sizeof...(args)] = { (m_data.push_back(args), 0)... };
static_cast<void>(dummy);
}
Here, filling in the data into the container is a bit trickier. We use parameter expansion to unpack and push each of the arguments. This c'tor allows us to call:
Foo<int> f1(1);
Foo<int> f2(1, 2);
Foo<int> f3(1, 2, 3);
Foo<int> f4(1, 2, 3, 4);
Entire class
The final result is quite nice:
template <class T>
class Foo
{
public:
Foo () {
std::cout << "Default" << std::endl;
}
template <class ... X>
Foo (X ... args) {
int dummy[sizeof...(args)] = { (m_data.push_back(args), 0)... };
static_cast<void>(dummy);
std::cout << "VA-args" << std::endl;
}
template <class X>
Foo (std::initializer_list<X> && list)
: m_data(std::begin(list), std::end(list)) {
std::cout << "Initializer" << std::endl;
}
template <class Container>
Foo (const Container & container, decltype(std::begin(container))* = 0, decltype(std::end(container))* = 0)
: m_data(std::begin(container), std::end(container)) {
std::cout << "Container" << std::endl;
}
template <class Iterator>
Foo (Iterator first, Iterator last, typename Iterator::iterator_category * = 0)
: m_data(first, last) {
std::cout << "Iterators" << std::endl;
}
private:
std::vector<T> m_data;
};

Add multiple values to a vector

I have a vector of ints that I want to add multiple values too but too many values to add using a lot of push_backs. Is there any method of adding multiple values at the end of a vector. Something along the lines of this:
std::vector<int> values
values += {3, 9, 2, 5, 8, etc};
I found that boost has something like this, but I would like not having to include boost.
#include <boost/assign/std/vector.hpp>
using namespace boost::assign;
{
std::vector<int> myElements;
myElements += 1,2,3,4,5;
}
Which seems to be declared like this:
template <class V, class A, class V2>
inline list_inserter<assign_detail::call_push_back<std::vector<V,A> >, V>
operator+=( std::vector<V, A>& c, V2 v )
{
return push_back( c )( v );
}
Is there any C++/C++11 way to do this or, if not, how would it be implemented?
This should work:
std::vector<int> values;
values.insert( values.end(), { 1, 2, 3, 4 } );
Perhaps with insert:
values.insert( values.end(), {3, 9, 2, 5, 8, etc} );
Demo.
In order to present as much as possible solutions, this should work too:
for(const auto x : {11, 12, 13, 14})
v.push_back(x);
Demo.
You can just make an operator:
template <class T>
std::vector<T>& operator+=(std::vector<T>& lhs, std::initializer_list<T> l)
{
lhs.insert(std::end(lhs), l);
return lhs;
}
You can mimic the boost boost::assign behavior
template <typename T>
class vector_adder
{
public:
std::vector<T>& v;
vector_adder(std::vector<T>& v):v(v)
{ }
vector_adder& operator,(const T& val)
{
v.push_back(val);
return *this;
}
};
template <typename T>
vector_adder<T> operator+=(std::vector<T>& v,const T& x)
{
return vector_adder<T>(v),x;
}
Then,
std::vector<int> v {1,2,3,4};
v += 11,12,13,14 ;
See here
You can do it by using the insert member function, as such:
vector<int> v;
int myarr[] {1, 2, 4, 5, 5, 6, 6, 8}; //brace initialization with c++11
v.insert(v.end(), myarr, myarr+8);
There was a previous answer that omitted the middle argument in the insert parameters, but that one does not work. There are several formats to follow for the insert method, which can be found here and the code snippet I wrote practically follows this format:
vectorName.insert(postion to start entering values, first value to enter, how many values to enter)
Note: That the last two arguments (i.e. myarr, myarr+8) use pointer arithmetics. myarr is the address in memory of the first element in the array, and the second value is the address of the 8th element. You can read about pointer arithmetic here.
Hope that helps!