简体   繁体   English

如何将初始化列表构造函数添加到STL向量

[英]How to add an Initializer List constructor to STL vector

So what I want to do is extend the existing vector class in my program to allow me to say this, 所以我想要做的是扩展程序中现有的vector类,让我这样说,

vector<string>* vec = new vector<string>(){"Value1","Value2"};

or 要么

vector<string>* vec = new vector<string>({"Value1","Value2"});

or 要么

vector<string> vec = {"Value1","Value2"};

I know I can accomplish something like this but doing this, 我知道我可以做到这样的事情,但这样做,

string temp[] = {"Value1","Value2"};
vector<string> vec(temp, temp+ sizeof(temp) /  sizeof( temp[0] ) ); 

This uses the vectors iterator constructor but can't I remove the extra line? 这使用向量迭代器构造函数但不能删除额外的行吗?

I know in C# you can add whatever you want to existing things by using the partial key word like this, 我知道在C#中你可以使用像这样的partial关键字来添加你想要的任何东西,

public partial class ClassName
{
   ClassName(Stuff stuff)
   {

   }
   void AmazingNewMethod()
   {

   }    
}

Does C++ have a nifty trick like this somewhere? C ++在某个地方有一个漂亮的技巧吗?

Do I have to inherit vector and build a customVector that has a constructor that behind the scenes does the iterator constructor thing? 我是否必须继承vector并构建一个customVector ,它具有一个构造函数,在幕后做迭代器构造函数的事情?

Maybe wrap those lines in a static Helper Function call that sets it by Reference and add it to a toolbox class somewhere? 也许将这些行包装在一个静态Helper函数调用中,该调用通过Reference设置它并将其添加到某个工具箱类中?

I feel like lots of programmers have hit this problem. 我觉得很多程序员都遇到过这个问题。 Are there any elegant solutions out there? 那里有优雅的解决方案吗?

Thanks. 谢谢。

Edit: fixed the title to mention this is an Initializer List constructor. 编辑:修复标题,提到这是一个初始化列表构造函数。

In C++11 there would be initializer lists to suite this approach. 在C ++ 11中,会有initializer lists来配合这种方法。 As you're mentioning .NET I now assume that you're using MS Visual Studio. 正如你提到.NET我现在假设你正在使用MS Visual Studio。 Microsoft does NOT implement initializer lists, therefore the easiest way to accomplish something like that would be a function that returns the vector with all the elements added. 微软没有实现初始化列表,因此有所建树最简单的方法一样,将返回所有添加的元素的向量函数。

On the partial thing: C++ does not offer a feature in the same vein as .NET's partial classes. partial事情:C ++没有提供与.NET的部分类相同的功能。

The C++ 2011 way is to accept an std::initializer_list<T> as a constructor argument: C ++ 2011方法是接受std::initializer_list<T>作为构造函数参数:

#include <initializer_list>

template <typename T>
class foo {
    T *d_array;
public:
    foo(std::initializer_list<T> list)
        : d_array(new T[list.size()]) {
        std::copy(list.begin(), list.end(), this->d_array);
    }
    foo(foo const&);
    foo& operator= (foo const&);
    ~foo() { delete[] this->d_array; }
};

The above clearly only concentrates on how use an std::initializer_list<T> . 以上显然只关注如何使用std::initializer_list<T> To actually do the allocation internally you'd allocate raw memory and construct the object in-place. 要在内部实际进行分配,您需要分配原始内存并就地构造对象。 However, this wasn't what the question is about. 然而,这不是问题所在。

With respect to adding this support to std::vector<T> : you don't have to! 关于将此支持添加到std::vector<T> :您不必! In C++ 2011 std::vector<T> can be initialized with an std::initializer_list<T> . 在C ++ 2011中, std::vector<T>可以用std::initializer_list<T> In C++ 2003 you can't do this. 在C ++ 2003中,你无法做到这一点。 The best you could do is to support construction from an array, using a constructor looking something like this: 您可以做的最好的事情是使用构造函数来支持数组构造,如下所示:

template <typename T>
    template <typename S, int Size>
foo<T>::foo(S const (&array)[Size])
    d_array(new T[Size]) {
    std::copy(array, array + Size, d_array);
};

However, there is no way to extend an existing without changing its type. 但是,如果不更改其类型,则无法扩展现有内容。 To avoid reimplementing most of the members you could publically inherit from the type you want to extend and add all required constructors (these are not inherited; in C++ 2011 you could inherit the existing constructors but then, with C++ 2011 you wouldn't need to do any of this anyway). 为了避免重新实现大多数成员,你可以公开地继承你想要扩展的类型并添加所有必需的构造函数(这些都不是继承的;在C ++ 2011中你可以继承现有的构造函数,但是,使用C ++ 2011你不需要无论如何都要这样做)。

An alternative you might want to use with C++ 2003 is to create a factor function taking a built-in array, ie something like this: 您可能希望与C ++ 2003一起使用的另一种方法是创建一个采用内置数组的因子函数,即:

template <typename T, typename S, int Size>
std::vector<T>
make_vector(S const (&array)[Size]) {
    return std::vector<T>(array, array + Size);
}

char const* init[] = { "Hello", "World" };
std::vector<std::string> value = make_vector<std::string>(init);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM