As a vector <int> + = 1,1,2,2,2,3,4,5,6; possible?

I found this strange syntax in the documentation.

std::vector<int> input;
input += 1,1,2,2,2,3,4,5,6; // <--- How is this possible?
+4
source share
1 answer

This is just the Boost.Assignment library . It uses congestion operator+=and operator,to simplify the purpose of containers.

A syntax break can be specified in the operator priority table .

Essentially it input += 1will return a proxy object with an overloaded one operator,, which will perform sequential inserts, roughly equivalent:

auto x = (input += 1); // input.push_back(1);
x,2; // input.push_back(2);
x,3; // input.push_back(3);

This was in C ++ 98 when they did not have std::initializer_listto directly assign the contents of the container, for example. std::vector<int> x = { 1, 2, 3 };.

+6
source

All Articles