Is it possible to avoid using push_back repeatedly when initializing a
vector? That is, is there a vector syntax that would be analogous to
the following array initialization syntax:
MyClass myArray[] = {
MyClass("Stan") ,
MyClass("Julie" )
};
Thanks for any ideas,
cpp 5 7528
"cppaddict" <he***@hello.co m> wrote... Is it possible to avoid using push_back repeatedly when initializing a vector? That is, is there a vector syntax that would be analogous to the following array initialization syntax:
MyClass myArray[] = { MyClass("Stan") , MyClass("Julie" ) };
No. But you should be able to declare an array of pointers to char
and then create your vector from it, like this:
char const* theNames[] = { "Stan", "Julie" };
std::vector<MyC lass> myVector(theNam es, theNames + 2);
(assuming your 'MyClass' class has a constructor that accepts a pointer
to const char)
If your MyClass requires a 'string' to be passed to the c-tor, you need
to construct an array of strings, and then your vector from it.
Victor
In article <ab************ *************** *****@4ax.com>,
cppaddict <he***@hello.co m> wrote: Is it possible to avoid using push_back repeatedly when initializing a vector? That is, is there a vector syntax that would be analogous to the following array initialization syntax:
MyClass myArray[] = { MyClass("Stan") , MyClass("Julie" ) };
Thanks for any ideas, cpp
You can get somewhat close with:
MyClass myArray[] = {
MyClass("Stan") ,
MyClass("Julie" )
};
std::vector<MyC lass> v(myArray,
myArray+sizeof( myArray)/sizeof(myArray[0]));
which unfortunately involves an extra copy of the data. There is
discussion of a language supported superior alternative on the committee
for C++0X, but no firm proposals I'm aware of (so it isn't even close to
a sure thing).
-Howard
On Tue, 27 Jul 2004 22:11:18 GMT, cppaddict <he***@hello.co m> wrote: Is it possible to avoid using push_back repeatedly when initializing a vector? That is, is there a vector syntax that would be analogous to the following array initialization syntax:
MyClass myArray[] = { MyClass("Stan") , MyClass("Julie" ) };
Thanks for any ideas, cpp http://www.codeproject.com/vcpp/stl/PGIL.asp
john
cppaddict wrote: Is it possible to avoid using push_back repeatedly when initializing a vector? That is, is there a vector syntax that would be analogous to the following array initialization syntax:
MyClass myArray[] = { MyClass("Stan") , MyClass("Julie" ) };
Thanks for any ideas, cpp
Leor Zolman's initialization library might be useful ! http://www.bdsoft.com/tools/initutil.html
"cppaddict" <he***@hello.co m> wrote in message news:ab******** *************** *********@4ax.c om... Is it possible to avoid using push_back repeatedly when initializing a vector? That is, is there a vector syntax that would be analogous to the following array initialization syntax:
MyClass myArray[] = { MyClass("Stan") , MyClass("Julie" ) };
std::vector<MyC lass> myVector (myArray, myArray + sizeof (myArray)/sizeof (*myArray)); Thanks for any ideas, cpp
--
Alex Vinokur http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: J. Campbell |
last post by:
I'm comfortable with arrays from previous programming, and understand
the advantages of c++ vectors...I just don't understand how to use
them :~( Can you help me to use a vector<string> in the following
compilable example instead of the string* array?
Thanks,
Joe
#include <iostream>
|
by: Ram Laxman |
last post by:
Hi all,
Can I assign an array to a vector if both(array and vector)
holds same data type?
|
by: Pelle Beckman |
last post by:
Hi all,
I have a few - beginners - questions:
* Why can't I put object references in a std::vector,
i.e std::vector<MyClass&> ?
At least in doesnt work in gcc (mingw, win32)
* What's the difference between passing
member inits in the c-tor funtion from
|
by: Marcin Kalicinski |
last post by:
int a; // #1 uninitialized value
int a(0) // #2 zero-initialized value
std::vector<int> v(10); // #3 zero-initialized values (why?)
std::vector<int> v(10, 0); // #4 zero-initialized values
How do I construct a vector of ints without initializing them to zero? Why
is #3 zero-initializing the ints? This is unintutitive, unexpected and slow.
If one wanted to intialize he would use syntax #4.
World would be better if int()...
|
by: Peter Olcott |
last post by:
I am trying to refer to the same std::vector in a class by two different names,
I tried a union, and I tried a reference, I can't seem to get the syntax right.
Can anyone please help? Thanks
| |
by: fatgirl.brown |
last post by:
Hi all,
I am attempting to initialize a vector of a vector in a constructor
with some clean-looking syntax and am not sure of how to go about this.
For instance:
double weight;
vector< vector<unsigned posInd;
Table() :
|
by: stephen b |
last post by:
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 = 1, v = 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
|
by: Mr. K.V.B.L. |
last post by:
I want to start a map with keys but an empty vector<string>. Not sure
what the syntax is here.
Something like:
map<string, vector<string MapVector;
MapVector.insert(make_pair("string1", new vector<string>));
MapVector.insert(make_pair("string2", new vector<string>));
MapVector.insert(make_pair("string3", new vector<string>));
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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,...
|
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...
| |
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |