473,888 Members | 1,419 Online

# vector assign

Hi all, personally I'd love to be able to do something like this:

vector<intv;
v.assign(1, 2, 5, 9, 8, 7) etc

without having to manually add elements by doing v[0] = 1, v[1] = 2 ..
etc.

it would make for much more readable code that is faster to write in
some situations. I've not seen this feature documented anywhere
though which I find curious. is there another way to achieve this?

thanks,
stephen.
Jun 27 '08
29 3453
47**********@m3 6g2000hse.googl egroups.com>, ke****@audiospi llage.com
says...

[ ... ]
I can't find a definition anwyhere of what the += operator does with
vectors.
Unless you provide an overloaded operator+= that does something, it
simply generates an error.
I'd be happy if, say, v += 2 iterated over the vector and
added the value 2 to each element. similarly if v *= 5 multiplied all
elements then I'd find that really useful too. In the name of brevity
of course.
If that's what you want, take a look at valarray.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #21
stephen b <ke****@audiosp illage.comwrote :
Checkout Boost's Assign Library. You can do these:
vector<intv;
v += 1,2,3,4,5,6,7,8 ,9;
Institutionaliz ed obfuscation, anyone? *That statement has a
predefined meaning in C++, without any library, and any code
which changes a predefined meaning should be avoided at all
costs.

I can't find a definition anwyhere of what the += operator does with
vectors. I'd be happy if, say, v += 2 iterated over the vector and
added the value 2 to each element. similarly if v *= 5 multiplied all
elements then I'd find that really useful too. In the name of brevity
of course.
vector doesn't have an op+= or op*=. Use transform instead:

transform(vec.b egin(), vec.end(), vec.begin(), bind2nd(plus<in t>(), 2));

The above adds two to each element.

transform(vec.b egin(), vec.end(), vec.begin(),
bind2nd(multipl ies<int>(), 5));

The above multiplies each element by 5.

You should probably consider using valarray if you are doing this a lot
though.
Jun 27 '08 #22
On May 25, 2:59 am, stephen b <ker...@audiosp illage.comwrote :
Checkout Boost's Assign Library. You can do these:
vector<intv;
v += 1,2,3,4,5,6,7,8 ,9;
Institutionaliz ed obfuscation, anyone? That statement has a
predefined meaning in C++, without any library, and any code
which changes a predefined meaning should be avoided at all
costs.
I can't find a definition anwyhere of what the += operator
does with vectors.
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.

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.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #23
James Kanze schrieb:
>>IIUC, the standard is extending initialization syntax
expressedly to deal with such cases (and will allow an
initializatio n list directly in the definition of the vector),
but I didn't think that it could be done today.
>Good, yet possibly bad. Good for vector. Bad if it isn't
more general.

It will definitely be more general. I've not really looked at
it myself, so I don't know the details, but I think the idea is
that it will work for any type which has a two iterator
constructor.
No, you have to write an extra sequence constructor in this form:

struct Container
{
Container(initi alizer_list<Typ eseq)
{
// do something with seq.begin(), .end() and .size(), ex.:
assign(seq.begi n(), seq.end());
}
}

From what I have read, an initializer can be used quite everywhere, so
you can pass it to a function or return it:

std::vector<int v = { 1, 2, 3 };
v.append( {4, 5, 6} );

SetColor( {1.0, 0.0, 0.0} ); // sets a RGB color value for red

--
Thomas
Jun 27 '08 #24
On May 25, 3:13*am, James Kanze <james.ka...@gm ail.comwrote:
On May 25, 2:59 am, stephen b <ker...@audiosp illage.comwrote :
Checkout Boost's Assign Library. You can do these:
vector<intv;
v += 1,2,3,4,5,6,7,8 ,9;
Institutionaliz ed obfuscation, anyone? *That statement has a
predefined meaning in C++, without any library, and any code
which changes a predefined meaning should be avoided at all
costs.
I can't find a definition anwyhere of what the += operator
does with vectors.

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.
Jun 27 '08 #25
On May 24, 7:18*pm, "Daniel T." <danie...@earth link.netwrote:
stephen b <ker...@audiosp illage.comwrote :
Checkout Boost's Assign Library. You can do these:
vector<intv;
v += 1,2,3,4,5,6,7,8 ,9;
Institutionaliz ed obfuscation, anyone? *That statement has a
predefined meaning in C++, without any library, and any code
which changes a predefined meaning should be avoided at all
costs.
I can't find a definition anwyhere of what the += operator does with
vectors. I'd be happy if, say, v += 2 iterated over the vector and
added the value 2 to each element. *similarly if v *= 5 multiplied all
elements then I'd find that really useful too. *In the name of brevity
of course.

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

transform(vec.b egin(), vec.end(), vec.begin(), bind2nd(plus<in t>(), 2));

The above adds two to each element.

transform(vec.b egin(), vec.end(), vec.begin(),
* * * bind2nd(multipl ies<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.
Jun 27 '08 #26
Hi!

stephen b schrieb:
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>::oper ator = (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
Jun 27 '08 #27
Hi!

Thomas J. Gritzan schrieb:
std::vector<int v = { 1, 2, 3 };
v.append( {4, 5, 6} );
Which would actually be a good way to append multiple known values.

Frank
Jun 27 '08 #28
On May 25, 7:35 pm, stephen b <ker...@audiosp illage.comwrote :
On May 25, 3:13 am, James Kanze <james.ka...@gm ail.comwrote:
On May 25, 2:59 am, stephen b <ker...@audiosp illage.comwrote :
Checkout Boost's Assign Library. You can do these:
vector<intv;
v += 1,2,3,4,5,6,7,8 ,9;
Institutionaliz ed obfuscation, anyone? That statement has a
predefined meaning in C++, without any library, and any code
which changes a predefined meaning should be avoided at all
costs.
I can't find a definition anwyhere of what the += operator
does with vectors.
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.
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.)
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?
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.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #29
Hi!

James Kanze schrieb:
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(i nt i)
{
InitWhatever();
return i;
}
Foo::Foo(int i)
: count(initAndRe turn(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
Jun 27 '08 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 5 7399 by: scooter | last post by: what happens memory wise with this scenerio: std::vector vecInt(20, 999); VecInt.assign(20, 0); do all the 999 values get overwritten or does the vector reassign the memory lso? 11 2444 by: nandor.sieben | last post by: I am trying to replace template < class T void set2vector (const set < T &s, vector < T &v) { typename set < T >::iterator it; for (it = s.begin (); it != s.end (); it++) { v.push_back (*it); } } 4 6972 by: Chris Roth | last post by: vector