[英]How to store unordered_set to a vector?
I want to use a vector to store several unordered_set.我想用一个向量来存储几个 unordered_set。
Here is my test codes:这是我的测试代码:
#include <unordered_set>
#include <vector>
using namespace std;
int main(){
vector<unordered_set<int>> v1;
unordered_set<int> s1 = {1,2}, s2 = {3,2};
v1[0] = s1;
v1[1] = s2;
for (auto &s : v1[0]) {
cout << s << " ";
}
}
And I get Segmentation fault (core dumped)
.我得到
Segmentation fault (core dumped)
。
My question is: How should I modify my codes?我的问题是:我应该如何修改我的代码?
The problem with your code is that your vectors does not have elements 0 and 1. Either initialize the vector with required number of elements, or insert the elements into an empty vector as below:您的代码的问题是您的向量没有元素 0 和 1。请使用所需数量的元素初始化向量,或者将元素插入到空向量中,如下所示:
int main(){
vector<unordered_set<int>> v1;
unordered_set<int> s1 = {1,2}, s2 = {3,2};
v1.push_back(s1);
v1.push_back(s2);
for (auto &s : v1[0]) {
cout << s << " ";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.