创建一个固定大小的类型向量列表。

huangapple go评论68阅读模式
英文:

creating a list of type vectors of fixed size

问题

我想要一个类型为 vector<int> 大小为 3 的列表。

我尝试写成:

```cpp
typedef vector<int> vec;
list<vec(3)> q;

但是它给了我这个错误:

sol.cpp:22:12: error: temporary of non-literal type ‘vec’ {aka ‘std::vector<int>’} in a constant expression
   22 |       list<vec(3)> q;
      |            ^~~~~~
In file included from /usr/include/c++/11/vector:67,
                 from /usr/include/c++/11/functional:62,
                 from /usr/include/c++/11/pstl/glue_algorithm_defs.h:13,
                 from /usr/include/c++/11/algorithm:74,
                 from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:65,
                 from sol.cpp:3:
/usr/include/c++/11/bits/stl_vector.h:389:11: note: ‘std::vector<int>’ is not literal because:
  389 |     class vector : protected _Vector_base<_Tp, _Alloc>
      |           ^~~~~~

<details>
<summary>英文:</summary>

i want a list of type vector&lt;int&gt; with vector size=3

i tried writing

``typedef vector&lt;int&gt; vec;
list&lt;vec(3)&gt; q;``

but it gave me this error

sol.cpp:22:12: error: temporary of non-literal type ‘vec’ {aka ‘std::vector<int>’} in a constant expression
22 | list<vec(3)> q;
| ^~~~~~
In file included from /usr/include/c++/11/vector:67,
from /usr/include/c++/11/functional:62,
from /usr/include/c++/11/pstl/glue_algorithm_defs.h:13,
from /usr/include/c++/11/algorithm:74,
from /usr/include/x86_64-linux-gnu/c++/11/bits/stdc++.h:65,
from sol.cpp:3:
/usr/include/c++/11/bits/stl_vector.h:389:11: note: ‘std::vector<int>’ is not literal because:
389 | class vector : protected _Vector_base<_Tp, _Alloc>
| ^~~~~~


</details>


# 答案1
**得分**: 2

没有固定大小的`std::vector`。

如果您必须限制条目数量为3,则使用`std::array<int, 3>`:

```cpp
#include <array>
#include <list>
#include <iostream>

using Array3 = std::array<int, 3>;
using ListArray3 = std::list<Array3>;

int main()
{
   ListArray3 l3 = {{1,2,3},{4,5,6}};
   std::cout << "列表中的数组数量为: " << l3.size() << "\n";
   for (auto& a : l3)
   {
      for (auto i : a)
        std::cout << i << " ";
      std::cout << "\n";        
   }
}

输出:

列表中的数组数量为: 2
1 2 3 
4 5 6 
英文:

There is no such thing as a std::vector of fixed size.

If you must limit the number of entries to 3, then use a std::array&lt;int, 3&gt;:

#include &lt;array&gt;
#include &lt;list&gt;
#include &lt;iostream&gt;

using Array3 = std::array&lt;int, 3&gt;;
using ListArray3 = std::list&lt;Array3&gt;;

int main()
{
   ListArray3 l3 = {{1,2,3},{4,5,6}};
   std::cout &lt;&lt; &quot;The number of arrays in the list are: &quot; &lt;&lt; l3.size() &lt;&lt; &quot;\n&quot;;
   for (auto&amp; a : l3)
   {
      for (auto i : a)
        std::cout &lt;&lt; i &lt;&lt; &quot; &quot;;
      std::cout &lt;&lt; &quot;\n&quot;;        
   }
}

Output:

The number of arrays in the list are: 2
1 2 3 
4 5 6 

答案2

得分: 1

如果您实际上想要一个大小为3的std::vector<int>列表,您只需创建一个std::vector<int>列表,然后构造每个大小为3的向量。例如,以下行创建了一个包含4个大小为3的向量的列表:

std::list<std::vector<int>> ls(4, std::vector<int>(3));

但是,没有任何阻止您更改这些向量的大小或添加大小不等于3的更多向量到列表中。如果您希望在编译时保证此列表中的每个对象都具有大小3,则应使用std::array而不是std::vector。例如,以下行创建了一个大小为3的空数组列表:

std::list<std::array<int, 3>> ls;
英文:

If you actually want a list of std::vector&lt;int&gt; with size 3, you just need to create a list of std::vector&lt;int&gt; and then construct each vector with size 3. For example, the following line creates a list of 4 vectors each of which has size 3:

std::list&lt;std::vector&lt;int&gt;&gt; ls(4, std::vector&lt;int&gt;(3));

However, there is nothing to prevent you from changing the size of these vectors or adding more vectors to the list with a size other than 3. If you want a compile-time guarantee that every object in this list should have size 3, then you should use std::array instead of std::vector. For example, the following line creates an empty list of arrays of size 3:

std::list&lt;std::array&lt;int, 3&gt;&gt; ls;

huangapple
  • 本文由 发表于 2023年7月17日 23:15:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定