本文介绍了C ++:初始化(新)不同初始大小的向量数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为帮助您理解这一点,我提供了我的代码:(main.cpp),仅涉及一个文件.

To help you get the point, I give my codes:(main.cpp),only one file involved.

#include <iostream>
#include <vector>
using namespace std;
class test{
public :
    int member {0};
    void fun(){cout << "member is " << member << endl;}
    test(){}
    //test(int param) : member(param){} //this line is commented.
};

int main()
{
    vector<test> *vp = new vector<test>[2] {{10},{20}};
    //vector<test> array[2] {{10},{20}};//this won't work either.
    cout << "size of vp[0] is " << vp[0].size() << endl;
    cout << "size of vp[1] is " << vp[1].size() << endl;
    return 0;
}

我打算将初始化为大小10,并将vp[1]初始化为大小20.但是,当我使用g++ -std=c++11 main.cpp -o mainmac上编译它时,它抱怨:

I intend to initialize vp[0] to size 10 and vp[1] to size 20. However when I compiled it on mac using g++ -std=c++11 main.cpp -o main it complained:

main.cpp:14:45: error: chosen constructor is explicit in copy-initialization
    vector<test> *vp = new vector<test>[2] {{10},{20}};
                                            ^~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:517:14: note:
      constructor declared here
    explicit vector(size_type __n);
             ^
main.cpp:14:50: error: chosen constructor is explicit in copy-initialization
    vector<test> *vp = new vector<test>[2] {{10},{20}};
                                                 ^~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:517:14: note:
      constructor declared here
    explicit vector(size_type __n);
         ^

在CentOS Linux中,使用相同的命令,我得到了

In CentOS Linux using the same command and I got

main.cpp: In function ‘int main()’:
main.cpp:14:54: error: converting to ‘std::vector<test>’ from initializer list would use explicit constructor ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = test; _Alloc = std::allocator<test>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<test>]’
     vector<test> *vp = new vector<test>[2] {{10},{20}};
                                                      ^
main.cpp:14:54: error: converting to ‘std::vector<test>’ from initializer list would use explicit constructor ‘std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = test; _Alloc = std::allocator<test>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<test>]’

这是怎么回事?为什么与关键字explicit有什么关系?我知道vector有几个构造函数(例如带有initializer-list类型参数的构造函数).如果我像vector<test> temp {10}那样初始化向量,这会将向量初始化为大小10,而没有任何explicit顾虑.我不知道vector<test> array[2] {{10},{20}}里面隐藏的什么导致了我的错误.

What is going on here? Why does it have anything to do with keyword explicit?I know vector have several constructors(such as the one with initializer-list type argument). If I initialize the vector like vector<test> temp {10}, this would initialize vector to be of size 10 without any explicit concerns. I don't know what is hidden inside when it comes to vector<test> array[2] {{10},{20}} that cause me the bug.

有趣的是,如果我为类test提供一个带有一个参数的构造函数(只需在代码中取消注释该行),则编译器完全不会抱怨.但是vector<test> array[2] {{10},{20}}的含义更改为使用2个分别用1020初始化的test类型对象初始化向量array[0].但是我稍后尝试的语法vector<test> array[2] {10,20}再次错误.

Interestingly, if I provide class test with a constructor with one argument(just uncomment the line in my code), the compiler does not complain at all. But the meaning of vector<test> array[2] {{10},{20}} changed to initialize the vector array[0] with 2 test type objects initialized with 10 and 20 respectively. But the syntax vector<test> array[2] {10,20},which i tried later, is wrong again.

我不知道这里发生了什么,完全迷路了.初始化列表类型的{10,20}也不是吗?

I don't know what is going on here and am totally lost. Isn't {10,20} of initializer-list type too?

如果您能解释这里发生的事情以及如何初始化不同大小的向量数组(请不要使用绕过的方式),我将非常感谢.我想知道语法的确切含义.

I really appreciate it if you can explain what's going on here, and how to initialize an array of vector of different size(please do not use circumventing ways). I want to know what does the syntax means exactly.

推荐答案

首先,直接初始化中允许使用explicit构造函数,但不能进行复制初始化.

Firstly, explicit constructor is allowed in direct-initialization, but not copy-initialization.

然后,在聚合初始化中,

(重点是我的)

这意味着对于new vector<test>[2] {{10},{20}};{10}{20}用于复制初始化 vector<test>元素;则不考虑explicit构造函数.

That means for new vector<test>[2] {{10},{20}};, {10} and {20} are used to copy-initialize the vector<test> elements; then the explicit constructors are not considered.

还有

因为直接初始化中允许使用explicit构造函数,

Because explicit constructor is allowed in direct initialization,

出于同样的原因,new vector<test>[2] {std::vector<test>{10},std::vector<test>{20}};也可以工作.

For the same reason new vector<test>[2] {std::vector<test>{10},std::vector<test>{20}}; works too.

顺便说一句:

如果提供的构造函数可用于将int隐式转换为test,则{10}可用于构造std::initializer_list<test>,则std::vector<test>的构造函数采用std::initializer_list<test>为被调用,因为它始终是首选.顺便说一句,如果您使test explicit的构造函数,代码也将失败.

If you provide a constructor which could be used to convert int to test implicitly, then {10} could be used to construct an std::initializer_list<test>, then the constructor of std::vector<test> which taking std::initializer_list<test> is invoked because it's always preferred. BTW If you make the constructor of test explicit the code would fail too.

这篇关于C ++:初始化(新)不同初始大小的向量数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 00:13
查看更多