initializing a vector of vectors of a user defined - c++

I have this struct
struct myStruct {
int a;
int b;
}
I want to create a vector <vector<myStruct> > V and initialize it to n empty vectors of type vector<myStruct>
I'm trying to use the the fill constructor
like this:
vector<edge> temp;
vector<vector<edge> > V(n, temp);
This code works fine in main, but when I have V inside a class how can I do that inside the class constructor.
EDIT:
when I do it in my class constructor I get the following error:
no match for call to '(std::vector<std::vector<edge> >) (int&, std::vector<edge>&)'
the code generating the error is:
vector<myStruct> temp;
V(n, temp); // n is a parameter for the constructor

First, note that temp is not necessary: your code is identical to
vector<vector<edge> > V(n);
Now to your main question: When your vector is inside a class, use initializer list if the member is non-static, or initialize the member in the declaration part if it is static.
class MyClass {
vector<vector<edge> > V;
public:
MyClass(int n) : V(n) {}
};
or like this:
// In the header
class MyClass {
static vector<vector<edge> > V;
...
};
// In a cpp file; n must be defined for this to work
vector<vector<edge> > MyClass::V(n);

Just omit temp. The constructor for the class that V is inside should look like:
MyClass(size_t n) : V(n) {}

class A
{
private:
std::vector<std::vector<myStruct>> _v;
public:
A() : _v(10) {} // if you just want 10 empty vectors, you don't need to supply the 2nd parameter
A(std::size_t n) : _v(n) {}
// ...
};
You use initializer lists for this kind of initialization.

Related

data member should be written in initialization list in the constructor? Different implementaion for array and vector? [duplicate]

This question already has answers here:
Why can't member initializers use parentheses?
(2 answers)
Closed 1 year ago.
While creating a vector of defined size for further use in class, is it neccessary that the parameter for constructors of data members should be written in the initializer list of my class' constructor?
Why does
class MyHashMap {
public:
vector<int> vec;
MyHashMap():vec(1000001) {
fill(vec.begin(), vec.begin() + 1000000, -1);
}
works but,
class MyHashMap {
public:
vector<int> vec(1000001);
MyHashMap() {
fill(vec.begin(), vec.begin() + 1000000, -1);
}
does not work and gives an error like
Line 4: Char 22: error: expected parameter declarator
vector<int> vec (1000001);
^
If the same implementation is done using arrays,
class MyHashMap {
public:
int data[1000001];
MyHashMap() {
fill(data, data + 1000000, -1);
}
there is no issue.
I'm still a beginner at c++ if someone could help me that'd be awesome. Thanks
You can't use parenthesis to pass parameters to a class member's constructor directly in its declaration inline, like you are trying to. You need to either:
use the parent class's member initialization list:
class MyHashMap {
public:
vector<int> vec;
MyHashMap() : vec(1000001, -1) {}
in C++11 and later, you can either use an initializer:
class MyHashMap {
public:
vector<int> vec = vector<int>(1000001, -1);
MyHashMap() = default;
or, you can use curly braces instead of parenthesis:
class MyHashMap {
public:
vector<int> vec{1000001, -1};
MyHashMap() = default;
Note, however, that in this last case, because std::vector has a constructor that takes a std::initializer_list as input, this syntax will actually create the vector using just 2 elements whose values are 1000001 and -1, it will not call the 2-param constructor to create the vector with 1000001 elements of value -1.

How to use () initializers for vector in class?

This code gives an error
class Board {
vector<vector<int>> sudoku(9, vector<int>(9));
// Error; Expected a parameter declarator
};
How can I fix it?
You can't use parentheses to directly initialize a member when you do in class initialization. You have to either brace ({}) or equal (= ...;) initialize the member. That means you need to refactor it to have the form of
vector<vector<int>> sudoku = vector<vector<int>>(9, vector<int>(9));
// or
vector<vector<int>> sudoku{9, vector<int>(9)};
One way:
class Board {
vector<vector<int>> sudoku = vector<vector<int>>(9, vector<int>(9));
};
Second way:
class Board {
public:
Board() : sudoku(9, vector<int>(9)) {}
private:
vector<vector<int>> sudoku;
};

2D Std::vector of a given class

I'm trying to use a vector to store x and y coordinates of certain data in my vector. I assumed the following would work but that is not the case. I have spent a lot of time searching hoping to get it to work but in vain. I appreciate any help whatsoever.
class A {
public:
A(size_t x, size_t y); //ctor
};
If I want to create a vector of type class A, so 2D, why is
std::vector<A> vec(10); not working?
void count(size_t x, size_t y) {
vec.at(x,y);
}
ERROR: error: no matching function for call to ‘std::vector<Board>::at(size_t, size_t&)
note: candidate expects 1 argument, 2 provided
Since class A's constructor has 2 variables, shouldn#t my vector of type A take 2 variables as well?
If not, what is the correct way of getting a 2d vector of class A such that I can call the .at()-function at x,y and get whatever is stored there?
This vector overload:
std::vector<A> vec(10);
makes 10 copies of type A by calling A's default constructor. Since you didn't provide a default constructor an error occurs. Provide the default constructor:
class A {
public:
A() = default;
};
int main() {
std::vector<A> v(10);
}
or use an appropriate constructor as a second parameter:
class A {
public:
A(size_t x, size_t y) {
// your code
}
};
int main() {
std::vector<A> v(10, A(1, 2));
}
That being said, don't confuse the vector of vectors:
std::vector<std::vector<T>> v;
with a simple constructor taking two parameters or a simple class having two data members.

Problems Initializing Vector

I'm new to c++ so I'm kind of consfused
I wanted to do something like that:
`
int max = 30;
class MyClass{
vector<int> data(max);
};
but it wasn't working, because it was not recognizing that "max" was that int I had just initialized.
so i changed to that:
class MyClass{
MyClass();
int max;
vector<int> data(max);
}
MyClass::MyClass(){
max = 40;}
Don't work unless I initialize the vector in the constructor, but I don't know the correct sintax.
How can I make this work? All I want is to initialize "max" and then use it as the initial size of the vector.
You prof/teacher should have told you about initializer lists. The syntax looks something like this:
class MyClass {
std::vector<int> data;
public:
MyClass(int max) : data(max) { }
};

Creating 2-dimensional vector in class C++

I need to create a vector of vectors full of integers. However, I continuously get the errors:
error: expected identifier before numeric constant
error: expected ',' or '...' before numeric constant
using namespace std;
class Grid {
public:
Grid();
void display_grid();
void output_grid();
private:
vector<int> row(5, 0);
vector<vector<int> > puzzle(9, row);
int rows_;
int columns_;
};
You cannot initialize the member variables at the point where you declare them. Use an initialization list in the constructor for that:
Grid::Grid()
: row(5,0), puzzle(9, row),
rows_(5), columns_(9)
{
}
C++ class definitions are limited in that you cannot initialise members in-line where you declare them. It's a shame, but it's being fixed to some extent in C++0x.
Anyway, you can still provide constructor parameters with the ctor-initializer syntax. You may not have seen it before, but:
struct T {
T() : x(42) {
// ...
}
int x;
};
is how you initialise a member, when you might have previously tried (and failed) with int x = 42;.
So:
class Grid {
public:
Grid();
void display_grid();
void output_grid();
private:
vector<int> row;
vector<vector<int> > puzzle;
int rows_;
int columns_;
};
Grid::Grid()
: row(5, 0)
, puzzle(9, row)
{
// ...
};
Hope that helps.
You can't initialize a member in a class declaration unless it's const static, because in C++ no code is being run/generated when you are declaring a class. You'll have to initialize them in your constructor.
You should initialize members in the class constructor, not the declaration. The following doesn't seem to be right in any way:
vector<int> row(5, 0);
vector<vector<int> > puzzle(9, row);
If row and puzzle are functions - the parameters should be types. If they're member variables - initialize them in the class constructor.
You cannot initialize mutable members as a part of class definition itself. Instead do assign it in in the constructor.
// ....
Grid()
{
row.resize(5,0) ;
puzzle.resize(9,row) ;
}
private:
vector<int> row;
vector<vector<int> > puzzle ;
// ..