0

vector<int,int> a, vector<pair<int,int>> a, vector<vector<int>> a - is same or diff ?

Using a[][] - this is the first, second or third variant ?

4th Mar 2020, 2:15 PM
stamb
stamb - avatar
9 Antworten
+ 3
No, they are completely different: vector<int, int> - it is not valid declaration of vector. vector<pair<int,int>> it is a vector of pair of integers. it is something like int array[n][2] where n - is size and can be dynamically change, 2 - it has two integers, but access to them should be through member names (first and second), for example: vector<pair<int,int>> v(10); v[0].first = 1; v[0].second = 10; ... vector<vector<int>> is a vector of vectors. it's a bit like 2-dimensional array, but each row of that array is a vector itself and can have different size. for example: vector<vector<int>> v2(10, vector<int>(5)); v2[3].resize(30); v2[3][10] = 3; v2 has 10 rows, each row has 5 elements except 4th row, which has 30 elements
4th Mar 2020, 2:50 PM
andriy kan
andriy kan - avatar
+ 2
If you were asking which vector definition is equally functional to `int a[][]`, then I guess the third one is a close match. But unlike array, vectors are dynamic. That is, you can grow or shrink individual row (I think you know this already). As for "same or diff", the three are different. But TBH I'm not sure about the first one `vector<int, int>`, I haven't seen something like that this far. Look forward to some enlightenment.
4th Mar 2020, 2:30 PM
Ipang
+ 2
Ipang, As for thrird and a[][] , I agree. But why the first isn't same ? What is significance for vector<int,int> ? Is any reference to documentation by multi-dimentional vectors ?
4th Mar 2020, 3:01 PM
stamb
stamb - avatar
+ 2
stamb Sorry buddy, I don't have any reference to support, but as Andriy Kan said, `vector<int, int>` is not a valid declaration. I didn't get what you mean by n-dimensional vectors, can you elaborate on your thought?
4th Mar 2020, 3:52 PM
Ipang
+ 2
Just regarding std::vector< int, int >, it is not a valid declaration because the vector container is actually templated for two types: T, which is the type of the elements the vector stores. As has been stated before, this can be a vector again, to generate multi-dimensional vectors, as in std::vector< std::vector< ... > >; Allocator, which is an allocator that is used to allocate and deallocate memory for the container, as well as construct and destruct the elements. You usually never see this template parameter used as it defaults to the standard allocator std::allocator< T >. Replacing it requires you to somewhat know what you are doing, although it might grant performance benefits when writing a custom allocator that is tailored for your own needs. Since int obviously does not satisfy the allocator requirements, the instantiation fails.
4th Mar 2020, 4:09 PM
Shadow
Shadow - avatar
+ 2
Shadow, I see. Thankee.
4th Mar 2020, 4:21 PM
stamb
stamb - avatar
+ 2
stamb No, vector<vector<int>> v2(10, 5) is not valid. If you want to initialize it, you can declare it like this: vector<vector<int>> v2 = { {1,2,3}, {11, 12, 13, 14, 15}, {21, 22} }; or like this: vector<vector<int>> v3(10, {1, 2 , 3}); or vector<vector<int>> v4(10, vector<int> (5, -1)); // initialize all elements to -1
4th Mar 2020, 4:27 PM
andriy kan
andriy kan - avatar
+ 1
andry kan , just: vector<vector<int>> v2(10, vector<int> (5)) or ...v2(10, 5) one may ?
4th Mar 2020, 3:56 PM
stamb
stamb - avatar
+ 1
Wow, andriy kan
4th Mar 2020, 4:37 PM
stamb
stamb - avatar