473,836 Members | 1,464 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 3441
Hi!

Alf P. Steinbach schrieb:
I don't think it's worth it or even a good idea to add more features,
but it's easy to do that also, e.g.

struct Clear {};

template< typename T >
std:vector<T>& operator <<( std::vector<T>& v, Clear )
{
std::vector<T>( ).swap( v );
return v;
}

enabling things like

v << Clear() << 1 << 2 << 3;
Right, no a good idea, in my opinion.

v1 << 1 << 2 << Swap(v2 << Clear() << 3 << 3) << v3 << Print(std::cout );

o_O

Frank
Jun 27 '08 #11
Hi!

Alf P. Steinbach schrieb:
>IIUC, the standard is extending initialization syntax
expressedly to deal with such cases (and will allow an
initializati on 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.
Meaning from an expression explicitly constructing an object, the final
type can be deduced from the arguments passed to the constructor? like:

array<int>({4,5 ,6}) //deduce size n from use of ctor array(T[n])??

or:

ostream_iterato r<int>(std::wco ut) //deduce wchar_t usage

?

Frank
Jun 27 '08 #12
On 2008-05-24 05:04:08 -0400, James Kanze <ja*********@gm ail.comsaid:
On May 24, 8:01 am, Jerry Coffin <jcof...@taeus. comwrote:
>In article <54590b15-eafb-45ca-83ac-3768a65f8e65@
2g2000hsn.goog legroups.com>, ker...@audiospi llage.com says...
>If you will/might need to expand the collection later, you'll
still need to use a vector, but you can initialize the array
from the constants, and then initialize the vector from the
array:
>TR1::array<int a = {1, 2, 5, 9, 8, 7};

Can TR1::array deduce the length from the number of elements in
the initialization list? (And if so, how?)
No, you have to supply the length.

tr1::array<int, 7a = {1, 2, 5, 9, 8, 7}; // last element is 0...
>
IIUC, the standard is extending initialization syntax
expressedly to deal with such cases (and will allow an
initialization list directly in the definition of the vector),
but I didn't think that it could be done today.
Instantiations of tr1::array (and std::array in C++0x) are PODS, so can
be initialized with that syntax. That was one of the goals in its
design.

In addition, as you say, for C++0x there's a proposal working its way
through the process to support that sort of initialization for non-POD
types. I'm not familiar with the details,

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #13
Frank Birbacher wrote:
Darío Griffo schrieb:
Daniel T. wrote:
BTW inheritance is not necessary.
I know, but since we program in OOP paradigm, it seems to me the
best way to do that.

No. It is not the best way although we have "OOP" at our disposal
(I think of "OOP" here as "derive from a base class"). A vector
makes no good base class. You must not derive from it. Value based
classes are usually not suited for inheritance. Think of a "long"
derived from an "int"!?
I guess you would have a real problem with the following then? :-)

template<class Tclass Vec : public vector<T{
public:
Vec() : vector<T>() { }
Vec(int s) : vector<T>(s) { }

T& operator[](int i) { return at(i); }
const T& operator[](int i) const { return at(i); }
}; //(1)

Seriously though, I do agree with you. Despite the citation of the above
source, I too think that such derivation is generally inappropriate.
Especially in this case because the assign(...) function would be useful
on several different containers, not just vectors so why limit its use
artificially?

That said, I see now that the functionality that the OP requested is
already in the vector class (as pointed out by James Kanze.)

(1) Stroustrup, Bjarne. "The C++ Programming Language (3rd ed.) 1997
Jun 27 '08 #14
Hi!

Daniel T. schrieb:
I guess you would have a real problem with the following then? :-)

template<class Tclass Vec : public vector<T{
Yes. It doesn't work polymorphically , you know. And you agree.

void foo(vector<Tcon st& data)
{
data[3]; //should throw, but doesn't ??

Regards,
Frank
Jun 27 '08 #15
In article <5a4cc544-b89a-4c82-8b1f-
7a**********@l6 4g2000hse.googl egroups.com>, ja*********@gma il.com
says...
On May 24, 8:01 am, Jerry Coffin <jcof...@taeus. comwrote:
[ ... ]
TR1::array<inta = {1, 2, 5, 9, 8, 7};

Can TR1::array deduce the length from the number of elements in
the initialization list? (And if so, how?)
Oops -- sorry, but no. You have to supply the size as a template
parameter. My apologies for the typo.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #16
On May 24, 12:34 pm, "Alf P. Steinbach" <al...@start.no wrote:
* James Kanze:
On May 24, 1:48 am, acehr...@gmail. com wrote:
On May 23, 3:40 pm, stephen b <ker...@audiosp illage.comwrote :
>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
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 tend to use "<<" for adding things to some logical
container.
There are arguments both ways. According to some, the
"abstractio n" of << is formatting. At least one specialist even
argued that it was formatting text. Others consider it simply
"insertion" . If you are of the latter school, it's the obvious
solution.

--
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 #17
On May 24, 12:45 pm, "Alf P. Steinbach" <al...@start.no wrote:
* James Kanze:
On May 24, 8:01 am, Jerry Coffin <jcof...@taeus. comwrote:
In article <54590b15-eafb-45ca-83ac-3768a65f8e65@
2g2000hsn.googl egroups.com>, ker...@audiospi llage.com says...
If you will/might need to expand the collection later, you'll
still need to use a vector, but you can initialize the array
from the constants, and then initialize the vector from the
array:
TR1::array<inta = {1, 2, 5, 9, 8, 7};
Can TR1::array deduce the length from the number of elements
in the initialization list? (And if so, how?)
No, it's just ordinary POD initialization (no constructors in
that class).
That was my believe as well, but every time I think something
can't be done, someone comes up with some metaprogramming trick
to do it.
So above seems to be a typo, missing size parameter.
IIUC, the standard is extending initialization syntax
expressedly to deal with such cases (and will allow an
initialization 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.

--
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 #18
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.

Stephen.

Jun 27 '08 #19
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.

Stephen.
Jun 27 '08 #20

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

Similar topics

5
7395
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
2441
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
6959
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
9812
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10244
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
9364
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7775
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6975
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
5814
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4444
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
4004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3103
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.