Initializing a two dimensional std::vector
Use the std::vector::vector(count, value)
constructor that accepts an initial size and a default value:
std::vector<std::vector<int> > fog(
ROW_COUNT,
std::vector<int>(COLUMN_COUNT)); // Defaults to zero initial value
If a value other than zero, say 4
for example, was required to be the default then:
std::vector<std::vector<int> > fog(
ROW_COUNT,
std::vector<int>(COLUMN_COUNT, 4));
I should also mention uniform initialization was introduced in C++11, which permits the initialization of vector
, and other containers, using {}
:
std::vector<std::vector<int> > fog { { 1, 1, 1 },
{ 2, 2, 2 } };
How to create a 2D vector array with default values?
It works fine if you remove what's in the parenthesis. The dimensions are determined by the size of the initializer lists. If you want to specify the size yourself, you can use std::array
.
std::vector< std::vector<int> > m= {
{1,2,9},
{8,4,7},
{5,6,0}
};
Initializing arrays is a bit different. See this question. You need double braces.
#include <array>
std::array< std::array<int, 3>, 3 > m= {{
{1,2,9},
{8,4,7},
{5,6,0}
}};
How to declare a 2D vector parameter which will accept any size?
For statically sized arrays, use std::array. For dynamically sized arrays use std::vector. Don't ever use raw C arrays unless some weird situation forces it on you.
If you need a multi dimensional array, you can of course use std::vector<std::vector<int>>
or similar for std::array
. This is easy and convenient since you can do myarray[row][column]
(and possibly good enough). But a better performing option is usually to just declare a 1D std::vector<int>
with a size of "dimension 1 * dimension 2" and then, when indexing into it, do myvector[row_number * size_of_row + column]
. Treating a 1D array as a 2D one is as easy as that and it is likely to perform better since it's friendlier to your CPUs prefetcher and cache hierarchy.
As for declaring a function to accept such arrays - it's straight forward. For example:
void f(const std::array<int, 666>& myarray);
void f(const std::array<std::array<int, 42>, 666>& myarray);
void f(const std::vector<int>& myarray);
void f(const std::vector<std::vector<int>>& myarray);
Declaring a 2D vector
std::vector
has a fill constructor which creates a vector of n elements and fills with the value specified. a
has the type std::vector<std::vector<int>>
which means that it is a vector of a vector. Hence your default value to fill the vector is a vector itself, not an int
. Therefore the second options is the correct one.
std::vector<std::vector<int>> array_2d(rows, std::vector<int>(cols, 0));
This creates a rows * cols 2D array where each element is 0. The default value is std::vector<int>(cols, 0)
which means each row has a vector which has cols
number of element, each being 0.
How to format and access data in 2D vector arrays in c++
std::vector<float>
is a 1D vector. Hence your errors.
To be accepted by the compiler, your code can be corrected as follows:
class movmentCalculator
{
private:
std::vector<std::vector<float>> toSim { {5, 5, 5, 5}, {6, 6, 6, 6}, {7, 7, 7, 7} };
public:
void printStack()
{
for (unsigned int i = 0; i < toSim.size(); ++i)
for (unsigned int j = 0; j < toSim[i].size(); ++j)
{
std::cout << "item" << i << ": " << toSim[i][j] << std::endl;
}
}
};
But you could easily have solved it on your own (in my opinion).
Related Topics
Accessing a Protected Member of a Base Class in Another Subclass
Tell Gdb to Skip Standard Files
Undefined Reference Error for Template Method
How to Escape a String for Use in Boost Regex
Is the Backslash Acceptable in C and C++ #Include Directives
Splitting a String into an Array in C++ Without Using Vector
Why Is Std::Unordered_Map Slow, and How to Use It More Effectively to Alleviate That
How to Create a New Operator in C++ and How
What Is an In-Place Constructor in C++
What Is the Datatype of String Literal in C++
Does Moving a Vector Invalidate Iterators
Std::Vector, Default Construction, C++11 and Breaking Changes
Does Std::Vector *Have* to Move Objects When Growing Capacity? Or, Can Allocators "Reallocate"
Aliasing T* with Char* Is Allowed. Is It Also Allowed the Other Way Around