使用C ++ 11中的初始化列表初始化2D向量 [英] Initializing a 2D vector using initialization list in C++11

查看:408
本文介绍了使用C ++ 11中的初始化列表初始化2D向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用初始化列表初始化2D向量?
for a normal vector doing:

  vector< int> myvect {1,2,3,4}; 

就足够了。但是对于2D来说:

  vector< vector< int> myvect {{10,20,30,40},
{50,60,70,80}
};

这是正确的方法是什么?

如何迭代通过它使用为?

  for(auto x:myvect)
{
cout<< x [j ++] < endl;
}

仅适用于节目:
10,1!



这是什么意思?

 矢量< int> myvect [5] {1,2,3,4}; 

我在这里看到并且不能理解它! 链接

解决方案


这是正确的做法是什么?


你展示的方式是一种可能的方式。您也可以使用:

  vector< vector< int>> myvect = {{10,20,30,40},
{50,60,70,80}};

vector< vector< int>> myvect {vector< int> {10,20,30,40},
vector< int> {50,60,70,80}};

第一个构造一个 std :: initializer_list< std :: vector< int>> 其中元素从内部支撑初始化列表直接初始化。第二个显式构造临时向量,然后将其移动到 std :: initializer_list< std :: vector< int>>



无论如何, std :: initializer_list< std的元素:: vector< int>> 被复制回 myvect (你不能移出 std :: initializer_list )。







使用for?


你实际上有一个向量的向量 ,因此你需要两个循环:

  for(vector< int> const& innerVec:myvect)
{
for(int element:innerVec)
{
cout<<元素<< ',';
}
cout<< endl;
}

我不使用 auto 以显式显示结果类型。








这可能是拼写错误。因为它是,它是非法的。声明 vector< int> myvect [5]; 声明一个数组5 vector< int> 。因此,以下列表初始化需要初始化数组,但是该列表的元素不能隐式转换为 vector (有一个ctor, $ c> size_t ,但它是显式的)。



这已经被指出 。 / p>

我想作者想写 std :: vector< int> vArray = {3,2,7,5,8};


How can i initialize a 2D vector using an initialization list? for a normal vector doing :

vector<int> myvect {1,2,3,4};

would suffice. But for a 2D one doing :

vector<vector<int>> myvect{ {10,20,30,40},
                            {50,60,70,80}
                          };

What is a correct way of doing it?
And how can i iterate through it using for?

for(auto x: myvect)
{
    cout<<x[j++]<<endl;
}

this for only shows: 10,1 !

And by the way what does this mean ?

   vector<int> myvect[5] {1,2,3,4};

i saw it here and cant understand it! Link

解决方案

What is a correct way of doing it?

The way you showed is a possible way. You could also use:

vector<vector<int>> myvect = { {10,20,30,40},
                               {50,60,70,80} };

vector<vector<int>> myvect{ vector<int>{10,20,30,40},
                            vector<int>{50,60,70,80} };

The first one constructs a std::initializer_list<std::vector<int>> where the elements are directly initialized from the inner braced-initializer-lists. The second one explicitly constructs temporary vectors which then are moved into a std::initializer_list<std::vector<int>>. This will probably not make a difference, since that move can be elided.

In any way, the elements of the std::initializer_list<std::vector<int>> are copied back out into myvect (you cannot move out of a std::initializer_list).


And how can i iterate through it using for?

You essentially have a vector of vectors, therefore you need two loops:

for(vector<int> const& innerVec : myvect)
{
    for(int element : innerVec)
    {
        cout << element << ',';
    }
    cout << endl;
}

I refrained from using auto to explicitly show the resulting types.


And by the way what does this mean ?

This is probably a typo. As it stands, it's illegal. The declaration vector<int> myvect[5]; declares an array of 5 vector<int>. The following list-initialization therefore needs to initialize the array, but the elements of this list are not implicitly convertible to vector<int> (there's a ctor that takes a size_t, but it's explicit).

That has already been pointed out in the comments of that side.

I guess the author wanted to write std::vector<int> vArray = {3, 2, 7, 5, 8};.

这篇关于使用C ++ 11中的初始化列表初始化2D向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆