Correct way to work with vector of arrays
You cannot store arrays in a vector
or any other container. The type of the elements to be stored in a container (called the container's value type) must be both copy constructible and assignable. Arrays are neither.
You can, however, use an array
class template, like the one provided by Boost, TR1, and C++0x:
std::vector<std::array<double, 4> >
(You'll want to replace std::array
with std::tr1::array
to use the template included in C++ TR1, or boost::array
to use the template from the Boost libraries. Alternatively, you can write your own; it's quite straightforward.)
How can i correctly create a vector of arrays?
On each iteration of the loop, you're not pushing back the array, rather you are pushing back a pointer to an array with automatic storage duration which then goes out of scope. (Conceptually a new array is created on each iteration although in practice a compiler will optimise that out.)
So you end up with a std::vector
of dangling pointers, and the behaviour on attempting to dereference those pointers is undefined.
A solution is to use a std::array
for the std::vector
payload. Note that std::array
has a data()
function which yields the underlying array - this will provide the compatibility with your functions that require an int*
parameter.
C++ vector of arrays
Unfortunately, std::array
does not have an initializer list constructor. Indeed, it has no user-defined constructor whatsoever -- this "feature" is a leftover from C++03 where omitting all user-defined constructors was the only way to enable the C-style brace initialization. It is IMHO a defect in the current standard.
So why doesn't built-in brace initialization work in this case? Let's see what std::array
looks like under the hood:
template <typename T, int i> struct array {
T data[i];
// ...
}
Ok, so doesn't that mean we'd have to use double braces in the initializer (one pair for array
, another pair for the data
member?
std::array<int, 2> a = { {1, 2} };
C (and consequently C++) has a special rule about brace elision, permitting the omission of the inner braces unless there is an ambiguity. array
exploits this feature, allowing us to write
std::array<int, 2> a = { 1, 2 };
So why doesn't the example in the original post work? Because brace elision is only permitted in the context of a C-style aggregate initialization, not if there's anything more complicated involved, such as an user-defined initializer list constructor.
The following should work, though, as ugly as it is:
std::vector<std::array<int, 2>> vp = { {{1,2}}, {{3,4}} };
The fact that it does not, at least on gcc 4.5 and gcc 4.6, seems to me to indicate a compiler bug. I'm not completely sure about it, though.
This question is somewhat relevant: How do I initialize a member array with an initializer_list?
array of vectors or vector of arrays?
So does this (
vector<int> adj[N];
) create an array of type vector or does this create a
vector of arrays?
It creates array of vectors
What is the difference between
vector< vector<int> > N;
and
vector<int> F[N]
In the first case you are creating a dynamic array of dynamic arrays (vector of vectors). The size of each vector could be changed at the run-time and all objects will be allocated on the heap.
In the second case you are creating a fixed-size array of vectors. You have to define N
at compile-time, and all vectors will be placed on the stack†, however, each vector will allocate elements on the heap.
I'd always prefer vector of vectors case (or the matrix, if you could use third-party libraries), or std::array
of std::array
s in case of compile-time sizes.
I'm new to C++ STL, and I'm having trouble comprehending the graph
representation.
You may also represent graph as a std::unordered_map<vertex_type,std::unordered_set<vertex_type>>
, where vertex_type
is the type of vertex (int
in your case). This approach could be used in order to reduce memory usage when the number of edges isn't huge.
†: To be precise - not always on stack - it may be a part of a complex object on the heap. Moreover, C++ standard does not define any requirements for stack or heap, it provides only requirements for storage duration, such as automatic, static, thread or dynamic.
Using a Vector of an Array of Vectors in C++
Use std::array
instead of the array For example
#include <array>
#include <vector>
//...
std::vector<std::array<std::vector<int>, 2>> tracked;
Or
#include <array>
#include <vector>
//...
typedef std::array<std::vector<int>, 2> feature_points;
std::vector< feature_points >tracked;
When do we use arrays over vectors in C++ and vice versa?
If vectors can do so much, under what circumstances do we still prefer arrays?
A good design is not when there is nothing left to add, rather when there is nothing left to remove. Or introducing extra complexity only when it is needed.
Accessing array inside vector in c++
Why don't you use std::pair
?
You can do it like this:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
typedef std::pair<int,int> intPair;
typedef std::vector<intPair> intPairVec;
intPairVec aVector;
for (unsigned int j = 0; j < 100; j += 10) {
for (unsigned int i = 0; i < 100; i += 3) {
aVector.push_back(std::make_pair(j,i));
}
}
int i=0;
for (intPairVec::iterator it = aVector.begin(); it != aVector.end();it++) {
std::cout << "aVector[" << i << "].1st = " << it->first << std::endl;
std::cout << "aVector[" << i << "].2nd = " << it->second<< std::endl;
i++;
}
return 0;
}
how to initialize a vector of array
I don't know what data type you used a BYTE. Here how it works with int array.
#include <iostream>
#include <vector>
int main()
{
std::vector<int*> cKey;
int keyTemp[8] = { 1,2,3,4,5,6,7,8 };
cKey.push_back(keyTemp);
std::cout << cKey[0][6] << std::endl;
system("pause");
return 0;
}
Related Topics
Concatenating Two Std::Vectors
How to List-Initialize a Vector of Move-Only Type
How to Make Cin Take Only Numbers
Which Is Faster: Stack Allocation or Heap Allocation
Is Std::Unique_Ptr≪T≫ Required to Know the Full Definition of T
How to Pass a Member Function Where a Free Function Is Expected
Difference Between Char A[] = String; and Char *P = String;
Why Do C and C++ Compilers Allow Array Lengths in Function Signatures When They'Re Never Enforced
How to Use Boost in Visual Studio 2010
Why How to Not Push_Back a Unique_Ptr into a Vector
Are There Benefits of Passing by Pointer Over Passing by Reference in C++
Pointer Expressions: *Ptr++, *++Ptr and ++*Ptr
Initializing a Static Std::Map≪Int, Int≫ in C++
How to Debug Heap Corruption Errors
Windows Threading: _Beginthread VS _Beginthreadex VS Createthread C++