473,888 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
In article <52b853c6-adcc-4628-9585-
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<int> 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<doublev1(5,1); vector<doublev2; v2 = v1; // 1 v2.assign(v1.begin(),v1.end()); // 2 Are 1 and 2 the same, or are their subtle differences between them. Which is preferable, if either? And yes, I know I could use the construction vector<doublev2(v1), but I'm giving an example above.
0
10778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10886
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10439
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5819
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6014
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4245
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3252
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.