我在 boost documentation 中发现了这个奇怪的语法.
std::vector<int> input;
input += 1,1,2,2,2,3,4,5,6; // <--- How is this possible?
最佳答案
这只是 Boost.Assignment图书馆。它使用 operator+=
和 operator,
重载使容器的分配更容易。
语法分解可以由 operator precedence 给出表。
本质上 input += 1
将返回一个具有 operator,
的代理对象重载将执行顺序插入,大致相当于:
auto x = (input += 1); // input.push_back(1);
x,2; // input.push_back(2);
x,3; // input.push_back(3);
这是在 C++98 中还没有 std::initializer_list
的时候直接分配容器的内容,例如std::vector<int> x = { 1, 2, 3 };
.
关于c++ - 如何 vector<int> += 1,1,2,2,2,3,4,5,6;可能的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26207646/