473,395 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Vector Initialization Syntax

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
Jul 22 '05 #1
5 7517
"cppaddict" <he***@hello.com> 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<MyClass> myVector(theNames, 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
Jul 22 '05 #2
In article <ab********************************@4ax.com>,
cppaddict <he***@hello.com> 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<MyClass> 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
Jul 22 '05 #3
On Tue, 27 Jul 2004 22:11:18 GMT, cppaddict <he***@hello.com> 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
Jul 22 '05 #4
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
Jul 22 '05 #5

"cppaddict" <he***@hello.com> wrote in message news:ab********************************@4ax.com...
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<MyClass> 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

Jul 22 '05 #6

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

Similar topics

9
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...
6
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?
3
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...
3
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...
56
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
4
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<...
29
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...
6
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",...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
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...
0
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...

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.