Initializing a vector of vectors of ints in C++17
You can use squiggly brackets to let the compiler know you're trying to call a constructor:
std::vector<std::vector<int>> sudoku_field{9, std::vector<int>(9, 0)};
Alternatively, you could do this work in the initialization list of your default constructor:
SudokuSolver() : sudoku_field(9, std::vector<int>(9, 0)) {}
And then run your default constructor from every new constructor you make to ensure that gets set:
SudokuSolver(int thing) : SudokuSolver() { }
How to initialize a vector of vectors on a struct?
You use new
to perform dynamic allocation. It returns a pointer that points to the dynamically allocated object.
You have no reason to use new
, since A
is an automatic variable. You can simply initialise A
using its constructor:
vector<vector<int> > A(dimension, vector<int>(dimension));
What is the easiest way to initialize a std::vector with hardcoded elements?
One method would be to use the array to initialize the vector
static const int arr[] = {16,2,77,29};
vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );
Creating a vector of fixed sized queue (boost circular queue)
You want a vector of queues:
#include <boost/circular_buffer.hpp>
struct pkt { int data; };
int main() {
// Boost Circular Queue -- This works fine
typedef boost::circular_buffer<pkt> pkt_queue;
pkt_queue a_queue(3);
// Vector of queues - This has errors, i also wish to initialize the vector
std::vector<pkt_queue> per_port_pkt_queue;
per_port_pkt_queue.emplace_back(3);
per_port_pkt_queue.emplace_back(3);
per_port_pkt_queue.emplace_back(3);
// or
per_port_pkt_queue.assign(20, pkt_queue(3)); // twenty 3-element queues
}
See it Live On Coliru
Constant-sized vector
The std::vector can always grow dynamically, but there are two ways you can allocate an initial size:
This allocates initial size and fills the elements with zeroes:
std::vector<int> v(10);
v.size(); //returns 10
This allocates an initial size but does not populate the array with zeroes:
std::vector<int> v;
v.reserve(10);
v.size(); //returns 0
syntax failure with vector of vectors
I will guess here that you are forgetting to tell us that you are trying to declare a data member for a class. So you are really trying to compile something like:
struct A {
vector <vector <int> > v(10, vector <int>(10,1));
};
and
struct A {
vector <vector<int> > v = vector <vector<int> >(1, vector<int>(1, 0));
};
A data member can only be initialized in a class definition with an equal sign or with braces. The initialization with parentheses is not allowed.
Try:
struct A {
vector<vector<int>> v{10, vector<int>(10,1)};
};
In any case you need at least C++11 for the brace-initialization, as well as non-static member initialization in the declaration (your second error where you use equals instead of parentheses, but with older C++ standard).
Initialize multidimensional vector with fixed value
You were close:
std::vector<std::vector<int>> vec(10, std::vector<int>(15, fixed_value));
// ^^^^^^^^^^^
Live demo
initialize 3D std::vector with size and value
It's the same thing as a 1D vector, but you have to change the parameters a bit:
vector<vector<vector<int>>> vsfl(200, vector<vector<int>>(200, vector<int>(200, 1)));
Because the std::vector
constructor takes T
as the second argument and it's a 3D vector, so you have to call T
s constructor if you want to have it initialized.
Related Topics
Why Is the Console Closing After I'Ve Included Cin.Get()
How Does Calling Srand More Than Once Affect the Quality of Randomness
Troubles with Boost::Spirit::Lex & Whitespace
C++11 Std::Async Doesn't Work in Mingw
How to Declare Std::Unique_Ptr and What Is the Use of It
Read Func Interp of a Z3 Array from the Z3 Model
Cannot Open Output File, Permission Denied
Why Is a C++ Reference Considered Safer Than a Pointer
C++: Difference Between Member and Non Member Functions
Visual Studio 2013 Fatal Error C1041 /Fs
The Fastest Way to Retrieve 16K Key-Value Pairs
How to Send Keystrokes to a Window
Pointer Comparisons ">" with One Before the First Element of an Array Object
How to Alter Qt Widgets in Winapi Threads
Receiving Only Necessary Data with C++ Socket
Is Substitution Performed on a Variadic Parameter Pack Type If the Pack Is Empty
Are the Character Digits ['0'..'9'] Required to Have Contiguous Numeric Values