简体   繁体   English

2d向量和向量图之间有什么区别?

[英]What's the difference between 2d vector and map of vector?

Let's say I've declared 假设我已经宣布

map< int , vector<int> > g1;
vector< vector<int> > g2;

What are the similarities and dissimilarities between these two ? 两者之间有什么异同?

The similarity is the way you access data, it can be the same syntax: 相似之处在于访问数据的方式,语法可以相同:

std::cout << g1[3][2] << std::endl;
std::cout << g2[3][2] << std::endl;

The main difference is the following: the map of vector doesn't have to contain all the indices. 主要区别如下:向量映射不必包含所有索引。 Then, you can have, as example, only 3 vectors in your map accessed with keys '17', '1234' and 13579 : 然后,例如,您只能用键“ 17”,“ 1234”和13579访问地图中的3个矢量:

g2[17].resize(10);
g2[1234].resize(5);
g2[13579].resize(100);

If you want the same syntax with a vector of vectors, you need to have at least 13579 vectors (including 13576 empty vector) in your main vector. 如果要使用向量向量相同的语法,则主向量中至少需要13579个向量(包括13576个空向量)。 But this will use a lot of unused space in the memory. 但这会占用内存中大量未使用的空间。

Moreover, in your map, you also can access your vectors with negative keys (which is not possible in the vector of vectors): 此外,在地图中,您还可以使用负号访问向量(在向量的向量中是不可能的):

g2[-10].resize(10);

After this obviously high difference, the storage of data is different. 经过这个明显的高差之后,数据的存储就不同了。 The vector allocates contiguous memory, while the map is stored as tree. 向量分配连续的内存,而映射存储为树。 The complexity of access in the vector is O(1) , while it's O(log(n)) in the map. 向量中访问的复杂度为O(1) ,而在映射中为O(log(n)) I invite you to learn some tutorial about containers in C++ to understand all the differences and the usual way to use them. 我邀请您学习有关C ++容器的一些教程,以了解所有差异以及使用它们的常用方法。

They are fundamentally different. 它们根本不同。 While you may be able to do both g2[0] and g1[0] , the behavior is vastly different. 尽管您既可以执行g2[0]也可以执行g1[0] ,但行为却大不相同。 Assume there is nothing at index 0, then std::map will default construct a new value_type, in this case a vector, and return a reference, whereas std::vector has undefined behavior, but typically either segfaults or returns garbage. 假设索引0处没有任何内容,则std::map将默认构造一个新的value_type,在这种情况下为向量,并返回引用,而std::vector具有未定义的行为,但通常是段错误或返回垃圾。

They are also completely different in terms of memory layout. 它们在内存布局方面也完全不同。 While std::map is backed by a red-black tree, std::vector is contiguous in memory. 虽然std::map由红黑树支持,但std::vector在内存中是连续的。 So inserting into the map will always result in dynamic allocation somewhere in memory, whereas the vector would be resized in case its current capacity is exceeded. 因此,插入映射将始终导致在内存中某处的动态分配,而在超出其当前容量的情况下将调整向量的大小。 Note however, that a vector of vectors is not contiguous in memory. 但是请注意,向量的向量在内存中并不连续。 The first vector, which itself is contiguous in memory is made up of vectors which look roughly like this in terms of data: 第一个向量本身在内存中是连续的,这些向量由在数据方面大致如下的向量组成:

struct vector 
{
    T* data;
    size_t capacity;
    size_t size;
};

Where each of the vectors owns its dynamic memory allocation at data . 每个向量在data处拥有其动态内存分配的位置。

The advantage of the map is that is does not have to be densely populated, ie you can have something at index 0 and 12902 without all the stuff in between, plus it is sorted. 该地图的优点是不必填充密集的地图,即您可以在索引0和12902处放置一些内容,而不必在两者之间添加所有内容,并且对其进行排序。 If you don't need the sorted property and can use c++11 consider std::unordered_map . 如果您不需要sorted属性并且可以使用c ++ 11,请考虑std::unordered_map The vector is always densely populated, ie at size 10000, elements 0-9999 exist. 向量始终是密集填充的,即大小为10000时,存在元素0-9999。

With example you can understand the difference. 通过示例,您可以了解不同之处。 Lets say the vector<int> stores unique ids of people, and map stores respective pincode as key. 可以说vector<int>存储人员的唯一ID,而map存储相应的pincode作为密钥。

map< int , vector<int> > listOfPeopleAtRespectivePinCode;
vector< vector<int> > bunchOfGroupsOfPeople;

Evidently, map are capable to associate key and value (here a list of values), while vector can efficiently store a bunch of data. 显然, map能够关联键和值(此处为值列表),而vector可以有效地存储一堆数据。

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

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