官术网_书友最值得收藏!

19. Adding a range of values to a container

Writing functions with any number of arguments is possible using variadic function templates. The function should have the container as the first parameter, followed by a variable number of arguments representing the values to be added at the back of the container. However, writing such a function template can be significantly simplified using fold expressions. Such an implementation is shown here:

template<typename C, typename... Args>
void push_back(C& c, Args&&... args)
{
(c.push_back(args), ...);
}

Examples of using this function template, with various container types, can be seen in the following listing:

int main()
{
std::vector<int> v;
push_back(v, 1, 2, 3, 4);
std::copy(std::begin(v), std::end(v),
std::ostream_iterator<int>(std::cout, " "));

std::list<int> l;
push_back(l, 1, 2, 3, 4);
std::copy(std::begin(l), std::end(l),
std::ostream_iterator<int>(std::cout, " "));
}
主站蜘蛛池模板: 修文县| 松桃| 长子县| 清远市| 青阳县| 榆树市| 阿瓦提县| 正定县| 田林县| 将乐县| 望都县| 皮山县| 福州市| 灵山县| 堆龙德庆县| 洛扎县| 历史| 旬邑县| 嘉祥县| 广灵县| 无锡市| 上栗县| 铜陵市| 股票| 湖北省| 泸水县| 新和县| 东乌| 军事| 夹江县| 崇文区| 江阴市| 滦南县| 景宁| 长宁县| 特克斯县| 新郑市| 西乌珠穆沁旗| 都江堰市| 昭平县| 通山县|