英文:
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<int> with vector size=3
i tried writing
``typedef vector<int> vec;
list<vec(3)> 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<int, 3>
:
#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 << "The number of arrays in the list are: " << l3.size() << "\n";
for (auto& a : l3)
{
for (auto i : a)
std::cout << i << " ";
std::cout << "\n";
}
}
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<int>
with size 3, you just need to create a list of std::vector<int>
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<std::vector<int>> ls(4, std::vector<int>(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<std::array<int, 3>> ls;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论