vector resize() automatic fill
Solution 1
New elements take the vector
member's default value, or a specific value if you use the overload of resize
with two parameters.
void resize(
size_type _Newsize,
Type _Val
);
In your case, the default will be an empty vector<double>
- if that is not what you want, pass what you DO want to put there to the overload above.
Existing elements are not modified.
Solution 2
If you want to zero out the entire 2d array, you can use the assign function of vector:
v.assign(size, vector<double>(size, 0.));
This will make a 2d vector of sizeXsize filled with zeros.
In your case:
RegMatrix& RegMatrix::operator=(const SparseMatrix rhs){
if(*this != rhs){
_matrix.assign(rhs.getRow(), vector<double>(rhs.getCol(), 0.));
for(i=0;i<rhs.getSize();++i){
Element e = rhs.getElement(i);
_matrix[e._row][e._col] = e._val;
}
}
return *this;
}
limlim
Updated on June 04, 2022Comments
-
limlim almost 2 years
I'm writing a class that holds a matrix (of double values), represented as
vector<vector<double>>
;I want to implement the
operator=
, to re-fill my matrix with the details of a given sparse matrix. I'm writing the following code:RegMatrix& RegMatrix::operator=(const SparseMatrix rhs){ if(*this != rhs){ _matrix.clear(); _matrix.resize(rhs.getRow()); int i; for(i=0;i<rhs.getRow();++i){ _matrix.at(i).resize(rhs.getCol()); } for(i=0;i<rhs.getSize();++i){ Element e = rhs.getElement(i); _matrix[e._row][e._col] = e._val; } } return *this; }
Does the
resize()
method fill automatically the vector with zeros? Is my implementation ok? -
limlim over 13 yearsso, if i want a two-dimensional vector of the zise RowXCol, and each inner vector's element will have the value of 0, i should change the line so it would look like that: _matrix.at(i).resize(rhs.getCol(),0); ?
-
c-urchin over 13 yearsNo, only the newly-added elements (if any) will be zero.
-
AnT stands with Russia over 13 years@limlim: You don't need to change it. You are already passing that zero implicitly. You can also pass that zero explicitly, but it will not change anything. If you believe that passing that 0 explicitly makes your code clearer, then you can go ahead and put it there. But it will make no difference functionality-wise.
-
AnT stands with Russia over 13 years@c-urchin: Huh? Firstly, when I say "new elements" a mean newly-added elements, which is sufficiently clear. Secondly, my last remark applies to the specific OP's code, where the vector is empty initially. So, it will be filled with zeros after resize, as I said.
-
JoshD over 13 yearsIf I'm not mistaken, resize won't zero the existing elements. If that's what you want to do, use assign (check out my answer for more).
-
AnT stands with Russia over 13 years@JoshD: The OP's vectors do not have any "existing elements". They are always empty before
resize
call. -
AnT stands with Russia over 13 yearsNo formal need for an explicit
0
as well.v.assign(size, vector<double>(size))
will do the same thing. -
JoshD over 13 years@AndreyT, then why does he bother to call clear()? That's a wasteful step. Because this is an assignment operator, it is reasonable to assume that there may be some contents in the _matrix. A call to assign covers both cases and is preferred to calling clear then resize.
-
c-urchin over 13 yearsWhy so touchy? In point of fact, the OP says (please reread it) "re-fill" which implies that some values have already been set.
-
c-urchin over 13 years@AndreyT: What part of "re-fill" do you not understand? (And no need to be so loud.)
-
AnT stands with Russia over 13 years@JoshD: Er... He doesn't have any exiting elements because he called
clear
in advance. That's what I'm talking about. Why you call it a "wasteful step" is not entirely clear to me. The solution you suggested yourself (withassign
) is just a wrapper overclear
(at least by specification) followed by a sequence of even less efficient steps. -
AnT stands with Russia over 13 years@c-urchin: Huh? What are you talking about?
-
AnT stands with Russia over 13 years@c-urchin: Please, pay attention. The OP's explicitly calls
clear
before doing any other actions. So, there are no existing elements at the moment ofresize
. What part of that don't you understand?