+= 在没有 boost 的向量上

发布于 2024-09-13 06:29:04 字数 242 浏览 2 评论 0原文

有没有办法在不使用 boost 或使用派生类的情况下将 += 运算符与向量一起使用?

例如。

somevector += 1, 2, 3, 4, 5, 6, 7;

实际上会是

somevector.push_back(1);
somevector.push_back(2);
somevector.push_back(3);
etc.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风追烟花雨 2024-09-20 06:29:04

通过一些丑陋的运算符重载,这并不是太难实现。这个解决方案可以很容易地变得更加通用,但它应该作为一个足够的例子。

#include <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_;
};

然后,为了将其与 += 结合使用 在向量上,我们重载向量的 += 运算符。我们返回一个 push_back_wrapper 实例,以便我们可以使用逗号运算符链接推回:

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);
}

现在我们可以编写示例中的代码:

int main()
{
    std::vector<int> v;
    v += 1, 2, 3, 4, 5, 6, 7;
}

v += 1 将调用我们的 operator+= 重载,它将返回 push_back_wrapper 的实例。然后将逗号运算符应用于“列表”中的每个后续元素。

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."

冰雪梦之恋 2024-09-20 06:29:04

不是这样的语法,不。但你可以这样做:

int tmparray[] = {1, 2, 3, 4, 5, 6, 7};

somevector.insert(somevector.end(), 
                  tmparray, 
                  tmparray + (sizeof(tmparray) / sizeof(tmparray[0])));

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])));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文