简体   繁体   English

如何在C ++中创建向量的向量图?

[英]How to create a map of vectors of vectors in C++?

I'm trying to make a map containing vectors containing vectors but I am getting an error when trying to access an element. 我正在尝试制作包含向量的地图,但其中包含向量,但是在尝试访问元素时出现错误。

This is my code: 这是我的代码:

#include <iostream>
#include <map>
#include <vector>
#include <string>

using namespace std;

int main()
{
    map<int, vector<vector<string>>> m;
    m[0][0].push_back("hi");
    return 0;
}

You can try it here: https://onlinegdb.com/HytLmjQJV 您可以在这里尝试: https : //onlinegdb.com/HytLmjQJV

The error I'm getting is in Visual Studio: 我收到的错误是在Visual Studio中:

Debug assertion failed.Program : Expression:vector subscript out of range. 调试断言失败。程序:Expression:vector下标超出范围。

m[0] is an empty vector in your code so you cannot access its first element (m[0][0]). m [0]在您的代码中为空向量,因此您无法访问其第一个元素(m [0] [0])。 You need to create a vector inside, eg 您需要在内部创建一个矢量,例如

m[0].push_back({});
m[0][0].push_back("hi");

The "problem" here is that you have 3 nested containers, so you need to add values to all of them to store your "hi" value. 这里的“问题”是您有3个嵌套容器,因此您需要向所有容器中添加值以存储"hi"值。
When you do this m[0][0].push_back("hi"); 当您执行此操作时, m[0][0].push_back("hi"); you are basically accessing the position 0 of each vector inside the map , but you didn't set any values in them, they are empty. 您基本上是在访问map中每个vector的位置0 ,但是您没有在其中设置任何值,它们为空。

I will first break it down to better explain. 我首先将其分解以更好地解释。

With nested containers, like map<int, vector<vector<string>>> , the easiest way to understand how they work is to break it down. 对于诸如map<int, vector<vector<string>>>类的嵌套容器,了解它们如何工作的最简单方法是将其分解。

map<int, vector<vector<string>>> m;

vector<string> inner; // Inner vector
vector<vector<string>> outer; // Outer vector


inner.push_back("Hi"); // Inner vector value
outer.push_back(inner); // Outer vector value

    // Map holds pairs
m.insert(pair<int, vector<vector<string>>>(10, outer));

    // Accessing data
cout << m[10][0][0] << endl;

Output 产量

> Hi

Simplifying 简化

That said you can easily and more cleanly write something like this. 也就是说,您可以轻松,干净地编写类似这样的内容。

m.insert({20, {{"Hi2"}}});

    // Accessing data
cout << m[20][0][0] << endl << endl; 

Output 产量

> Hi2 

Where the first set of curly braces, {20, {...}} , is the pair that is inserted into the map . 第一组花括号{20, {...}}是插入map Think of it as the map only caring about a pair of an int and a vector . 可以将其视为map它只关心一对intvector
Then you have that vector , outer curly braces {{...}} , knowing it holds another vector . 然后,您知道了该vector ,外花括号{{...}} ,知道它包含另一个vector
This vector , inner curly braces {""} , knows it holds strings. 内花括号{""}这个vector知道它包含字符串。

Essentially with the curly braces we are doing the same as described above, creating separate values for each of the containers, a pair and two vectors. 本质上,使用花括号,我们如上所述进行相同的操作,分别为每个容器,一对和两个向量创建单独的值。

Extending 扩展

Then you can easily scale it up with more data. 然后,您可以轻松扩展更多数据。

m.insert({25, {{"Hi2", "Hi3", "Hi4"}, {"By1", "By2"}}});

    // Accessing data
cout << m[25][0][0] << endl;
cout << m[25][0][1] << endl;
cout << m[25][0][2] << endl << endl;

cout << m[25][1][0] << endl;
cout << m[25][1][1] << endl;

Output 产量

> Hi2 
> Hi3
> Hi4 
>
> By1
> By2

Note 注意

You can check the project here to see it working. 您可以在此处检查项目以查看其工作情况。
Also in c++ a map doesn't have a push_back() but if you want something similar to it you can use emplace() and do something like this m.emplace(20, outer) , although it doesn't work with the shorter versions with the curly braces. 同样在c ++中, map没有push_back()但是如果您想要类似的地图,则可以使用emplace()并执行类似m.emplace(20, outer) ,尽管不适用于较短的地图。花括号的版本。

While you are trying to add to this vector properly, you haven't added anything to your vector. 当您尝试正确添加到此向量中时,您尚未添加任何内容。 You're attempting to grab something that isn't there. 您正在尝试获取不存在的内容。

You'll need to put something in that first vector. 您需要在第一个向量中添加一些内容。

m[0].pushback();
m[0][0].pushback("hi");

alternatively, you could create the vector that you're going keep inside that first vector, first. 或者,您可以先创建要保留在第一个向量中的向量。

vector<string> vec1;
vec1.pushback("hi");

m[0].pushback(vec1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM