You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何向期望迭代器范围的函数传递单个参数?

Hey there! If you're working with a template function designed for iterator ranges like this:

template<class Iter> 
void function(Iter first, Iter last) { 
    // Your core logic here
}

and need to pass a single argument to it, the trick is to construct a tiny [first, last) range that only includes that one element. Here are the most common, practical approaches:

1. Use pointers (for addressable values)

If your single value is a variable you can take the address of, pointers work perfectly since they're valid STL iterators. Just pass the value's address as first, and address + 1 as last:

int my_num = 42;
function(&my_num, &my_num + 1);

This is super straightforward and works for any type where & gives you a valid pointer—no extra headers needed.

2. Wrap in a temporary std::array

If you want to stick to a container-style approach (matching how you'd call the function with a regular container), use a single-element std::array:

#include <array>

// C++11/14 version
function(std::begin(std::array<int, 1>{42}), std::end(std::array<int, 1>{42}));

// C++17+ (template argument deduction)
function(std::begin(std::array{42}), std::end(std::array{42}));

This feels consistent with standard container usage and has no heap overhead (unlike vector).

3. Temporary std::vector (less ideal for single elements)

You can also use a temporary vector with one element, but note this will allocate heap memory (unnecessary for a single value):

#include <vector>

function(std::begin(std::vector<int>{42}), std::end(std::vector<int>{42}));

Only use this if you already have a vector handy or need compatibility with older code that doesn't support std::array properly.

4. Create a helper function (for repeated use)

If you find yourself doing this often, write a small helper to clean up your code:

#include <utility> // for std::make_pair
#include <memory>  // for std::addressof

template<typename T>
auto single_element_range(T& val) {
    // Use std::addressof to avoid issues with overloaded operator&
    return std::make_pair(std::addressof(val), std::addressof(val) + 1);
}

// Usage:
int x = 100;
auto [first, last] = single_element_range(x); // C++17 structured bindings
function(first, last);

You can even add an overload for rvalues (temporary values) if needed:

template<typename T>
auto single_element_range(T&& val) {
    auto temp = std::forward<T>(val);
    return std::make_pair(std::addressof(temp), std::addressof(temp) + 1);
}

Quick Notes

  • Always ensure your iterators stay valid during the function call: temporary containers (like the array/vector examples above) are fine because they live until the function call completes.
  • For non-copyable/movable types, stick to the pointer or reference-based approaches to avoid unnecessary copies.

内容的提问来源于stack exchange,提问作者piripiri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:03:47