实现数组初始化器

发布于 2024-10-25 12:55:28 字数 426 浏览 5 评论 0原文

您可以在同一行声明和初始化常规数组,如下所示:

int PowersOfTwo[] = {1, 2, 4, 8, 16, 32, 64, 128};

Is there a way to在自定义类中复制此行为?因此,例如:

MyClass<int> PowersOfTwo = {1, 2, 4, 8, 16, 32, 64, 128};

您可以让复制构造函数将数组作为其参数,但您仍然必须在上一行声明该数组。

int InitializationArray[] = {1, 2, 4, 8, 16, 32, 64, 128};
MyClass<int> PowersOfTwo = InitializationArray; 

You can declare and initialize regular arrays on the same line, like so:

int PowersOfTwo[] = {1, 2, 4, 8, 16, 32, 64, 128};

Is there a way to replicate this behavior in custom classes? So, for example:

MyClass<int> PowersOfTwo = {1, 2, 4, 8, 16, 32, 64, 128};

You can have a copy constructor take an array as its parameter, but you still have to declare the array on the previous line.

int InitializationArray[] = {1, 2, 4, 8, 16, 32, 64, 128};
MyClass<int> PowersOfTwo = InitializationArray; 

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

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

发布评论

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

评论(2

蔚蓝源自深海 2024-11-01 12:55:28

您可以以这样的方式实现您的类:

MyClass<int> array;
array = 1,2,3,4,5,6,7,8,9,10;//dont worry - all ints goes to the array!!!

这是我的实现:

template <class T>
class MyClass
{
   std::vector<T> items;
public:

    MyClass & operator=(const T &item)
    {
       items.clear();
       items.push_back(item);
       return *this;
    }
    MyClass & operator,(const T &item)
    {
       items.push_back(item);
       return *this;
    }
    size_t Size() const { return items.size(); }
    T & operator[](size_t i) { return items[i]; }
    const T & operator[](size_t i) const { return items[i]; }

};

int main() {

        MyClass<int> array;
        array = 1,2,3,4,5,6,7,8,9,10;
        for (size_t i = 0 ; i < array.Size() ; i++ )
           std::cout << array[i] << std::endl;
        return 0;
}

输出:

1
2
3
4
5
6
7
8
9
10

请参阅在线演示: http://www.ideone.com/CBPmj

您可以在这里看到我昨天发布的两个类似的解决方案:

使用值列表进行模板数组初始化


编辑:

您可以使用类似的技巧来填充现有的 STL 容器。例如,您可以这样写:

std::vector<int> v;
v+=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15; //push_back is called for each int!

重载 (), 运算符所需的全部内容为:

template<typename T>
std::vector<T>& operator+=(std::vector<T> & v, const T & item)
{
    v.push_back(item); return v;
}
template<typename T>
std::vector<T>& operator,(std::vector<T> & v, const T & item) 
{
    v.push_back(item); return v;
}

工作演示:http://ideone.com/0cIUD


再次编辑:

我是 享受 C++ 运算符的乐趣。现在这个:

std::vector<int> v;
v << 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15; //inserts all to the vector!

我觉得这个看起来更好!

You can implement your class in such a way that you can write this:

MyClass<int> array;
array = 1,2,3,4,5,6,7,8,9,10;//dont worry - all ints goes to the array!!!

Here is my implementation:

template <class T>
class MyClass
{
   std::vector<T> items;
public:

    MyClass & operator=(const T &item)
    {
       items.clear();
       items.push_back(item);
       return *this;
    }
    MyClass & operator,(const T &item)
    {
       items.push_back(item);
       return *this;
    }
    size_t Size() const { return items.size(); }
    T & operator[](size_t i) { return items[i]; }
    const T & operator[](size_t i) const { return items[i]; }

};

int main() {

        MyClass<int> array;
        array = 1,2,3,4,5,6,7,8,9,10;
        for (size_t i = 0 ; i < array.Size() ; i++ )
           std::cout << array[i] << std::endl;
        return 0;
}

Output:

1
2
3
4
5
6
7
8
9
10

See online demo : http://www.ideone.com/CBPmj

Two similar solutions you can see here which I posted yesterday :

Template array initialization with a list of values


EDIT:

Similar tricks you can do to populate existing STL containers. For example, you can write this:

std::vector<int> v;
v+=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15; //push_back is called for each int!

All you need to overload () and , operator as:

template<typename T>
std::vector<T>& operator+=(std::vector<T> & v, const T & item)
{
    v.push_back(item); return v;
}
template<typename T>
std::vector<T>& operator,(std::vector<T> & v, const T & item) 
{
    v.push_back(item); return v;
}

Working demo : http://ideone.com/0cIUD


AGAIN EDIT:

I'm having fun with C++ operator. Now this:

std::vector<int> v;
v << 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15; //inserts all to the vector!

I think this looks better!

月亮坠入山谷 2024-11-01 12:55:28

仅当您的编译器提供对 初始化程序列表的支持时才能完成此操作,C++0x 功能。

否则,必须使用其他语法,如 boost.assign 库。

This can be done only if your compiler provides support for initializer lists, a C++0x feature.

Otherwise, some other syntax would have to be used, as in the boost.assign library.

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