vector assign

S

stephen b

There isn't any, of course but there is one for string, and it doesn't
take too much imagination to extend it to vector.

OK

v = vector<float>;
v += 0.1, 0.9, 2.3;

adds 3 elements to an emtpy vector.

v += 3.2, 4.2, 5.6;

adds 3 more elements.. same as String.

There is a well defined meaning for the comma operator,
however.  And the above breaks it.  It also breaks the rule that
x += y should have the same behavior as x = x + y, modulo the
fact that in one, x is evaluated twice, and in the other only
once.  The fact that:
    v += 1,2,3,4,5,6,7,8,9;
and
    v = 1,2,3,4,5,6,7,8,9;
mean completely different things is pure obfuscation, and won't
be allowed by any reasonable coding guideline.


You've lost me. What does v = 1,2,3,4,5,6,7,8,9; do?

Stephen.
 
S

stephen b

vector doesn't have an op+= or op*=. Use transform instead:

transform(vec.begin(), vec.end(), vec.begin(), bind2nd(plus<int>(), 2));

The above adds two to each element.

transform(vec.begin(), vec.end(), vec.begin(),
      bind2nd(multiplies<int>(), 5));

The above multiplies each element by 5.

You should probably consider using valarray if you are doing this a lot
though.

OK, in light of recent discussion expecting vector += 2 to add to each
element makes little sense really. Thanks for the valarray tip.

Stephen.
 
F

Frank Birbacher

Hi!

stephen said:
You've lost me. What does v = 1,2,3,4,5,6,7,8,9; do?

It won't compile. It would try to assign "1" to "v", then evaluate the
expression "2", then evaluate the expression "3", then evaluate the
expression "4", then evaluate the expression "5", then evaluate the
expression "6", then evaluate the expression "7", then evaluate the
expression "8", and then evaluate the expression "9".

Those expressions are obviously simple to evaluate. But if there was a
vector<T>::eek:perator = (T const t), it could return some strange object
(against all expectations) which could in turn implement the "operator
,". That way the expression could as well mean to first clear the vector
and then append all values.

Frank
 
J

James Kanze

v = vector<float>;
v += 0.1, 0.9, 2.3;
adds 3 elements to an emtpy vector.
v += 3.2, 4.2, 5.6;
adds 3 more elements.. same as String.

Except that that's not what it does with string. If s is an
std::string:
s += 'a', 'b', 'c' ;
adds one element to the string ('a'), then evaluates 'b' and
'c', and ignores the results. It's well defined behavior in
standard C++. And while it doesn't make much sense here, I have
seen things like:
s += f(), g(), h() ;
where the functions have side effects. (I don't like it. IMHO,
any use of comma as an operator is a source of confusion, and
should be banned. But I have seen it.)
You've lost me. What does v = 1,2,3,4,5,6,7,8,9; do?

It converts 1 to a vector (OK: the conversion is illegal), and
assigns it to v. It then evaluates the expression 2, and throws
out the results, then the expression 3, etc.

As written, it's not very useful, but if you replace the numeric
constants with more complex expressions, with side effects,
there are people who use it.

IMHO, the best solution is just to ban the use of comma as an
operator. Which still wouldn't make it acceptable as an
overloaded operator, because there'd still be the question as to
whether any given comma was an operator or punctuation.
 
F

Frank Birbacher

Hi!

James said:
And while it doesn't make much sense here, I have
seen things like:
s += f(), g(), h() ;
where the functions have side effects. (I don't like it. IMHO,
any use of comma as an operator is a source of confusion, and
should be banned. But I have seen it.)

Yeah. I tried to use the comma operator in the initializer list of a
class constructor. Like:

Foo::Foo(int i)
: count( (InitWhatever(), i) )
{}

Later I changed this to:

int initAndReturn(int i)
{
InitWhatever();
return i;
}
Foo::Foo(int i)
: count(initAndReturn(i))
{}

So, although I see a use for the operator here to make up for the
restrictive syntax for constructors, there always seems to be a better
way to trigger the same side effects (write a wrapper function). This
leads to the conclusion: the comma operator is only overloaded to
generate some "neat syntax" (e.g. for assigning values), and that always
breaks habit. Thus use it with care!

Frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top