473,405 Members | 2,141 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,405 software developers and data experts.

initialising a vector

Hi,

is there a way of initialising a vector in a similar fashion to
initialising an array?

consider:
Shape* triangle = new Triangle ();
Shape* square = new Square ();
Shape* shape_arr [] = {&triangle, &square};

is there some of doing something like this?:
vector<Shape*> shape_vec = {&triangle, &square};
--
Nick Keighley

Jul 23 '05 #1
8 2612
How about if you combine the two?

Shape* init[] = {new Triangle, new Square};
vector<Shape*> shape_vec(init, init + 2);

Jul 23 '05 #2
[ initialising a vector from an "preset" array ]

James Daughtry wrote:
How about if you combine the two?

Shape* init[] = {new Triangle, new Square};
vector<Shape*> shape_vec(init, init + 2);


yes

thanks!
--
Nick Keighley

Jul 23 '05 #3
Nick Keighley wrote:
Hi,

is there a way of initialising a vector in a similar fashion to
initialising an array?

consider:
Shape* triangle = new Triangle ();
Shape* square = new Square ();
Shape* shape_arr [] = {&triangle, &square};

is there some of doing something like this?:
vector<Shape*> shape_vec = {&triangle, &square};


See http://www.boost.org/libs/assign/doc/index.html

which allows you to:

vector<Shape*> shape_vec = List_of(&triangle)(&square};

Jeff Flinn
Jul 23 '05 #4
Jeff Flinn wrote:
See http://www.boost.org/libs/assign/doc/index.html

which allows you to:

vector<Shape*> shape_vec = List_of(&triangle)(&square};


And what is the advantage compared to James Daughtry's clean and simple
solution?

Jul 23 '05 #5
Panjandrum wrote:
Jeff Flinn wrote:
See http://www.boost.org/libs/assign/doc/index.html

which allows you to:

vector<Shape*> shape_vec = List_of(&triangle)(&square};


And what is the advantage compared to James Daughtry's clean and
simple solution?


Well unless my eyes deceive me, the above is accomplished in a single
statement, which is my understanding as to what the OP desired. Also the
boost assignment library allows use in initializers:

class CompoundShape
{
std::vector<Shape*> mShapes;
public:
CompoundShape()
: mShapes( List_of(new Triangle)(new Square) )
{}
};

Although both 'Shape* init[] = {new Triangle, new Square};' and the above
usage may have exception safety issues. What if either Triangle or Square
constructors throw. I'm not sure if even defining 'std::vector<
boost::shared_ptr<Shape> > mShapes;' is sufficient.

Jeff Flinn

Jul 23 '05 #6
On 11 Jun 2005 06:06:44 -0700, James Daughtry wrote:
How about if you combine the two?

Shape* init[] = {new Triangle, new Square};
That may leak if one new throws.
vector<Shape*> shape_vec(init, init + 2);

--
I'm not a racist. I hate everyone equally!
Jul 23 '05 #7
Jeff Flinn wrote:
class CompoundShape
{
std::vector<Shape*> mShapes;
public:
CompoundShape()
: mShapes( List_of(new Triangle)(new Square) )
{}
};

Although both 'Shape* init[] = {new Triangle, new Square};' and the above
usage may have exception safety issues. What if either Triangle or Square
constructors throw. I'm not sure if even defining 'std::vector<
boost::shared_ptr<Shape> > mShapes;' is sufficient.


A smart pointer (one suitable for use in standard containers) will help
you if one new succeeds and the other throws. If either constructor
can throw, it's up to them to free any memory they allocate, per FAQ
17.4.

Kristo

Jul 23 '05 #8


Kristo wrote:
Although both 'Shape* init[] = {new Triangle, new Square};' and the above
usage may have exception safety issues. What if either Triangle or Square
constructors throw. I'm not sure if even defining 'std::vector<
boost::shared_ptr<Shape> > mShapes;' is sufficient.


A smart pointer (one suitable for use in standard containers) will help
you if one new succeeds and the other throws. If either constructor
can throw, it's up to them to free any memory they allocate, per FAQ
17.4.


Just introducing smart pointers does not necessarily solve the leak
problem, though it's a step in the right direction:
http://www.gotw.ca/gotw/056.htm

Jul 23 '05 #9

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

Similar topics

4
by: Peter Kohout | last post by:
Hi everyone. I have a class CFoo { private: static int N = 50; int array; //lots more code
1
by: ajit goel | last post by:
Hi; I have a question which respect to initialising and creating objects in loops.Here are the 2 scenarios: 1. ###############################################################################...
6
by: Mattias Brändström | last post by:
Hello all! I am trying to write code that allows me to initialise one of my classes inline (with a vector like structure). Inline might not be the best term to use here but I can't think of any...
2
by: Dylan | last post by:
what is the best way of initializing a static std::vector data member with some values? (currently I just push_back some values in the constructor if the size == 0) thanks
1
by: Mattias Brändström | last post by:
Hello! Sometimes I get the urge to write something like the following template (see the code below) to help me initialise stl containers. With this template I could: vector<int> v =...
8
by: nrhayyal | last post by:
hi all, is it a good idea to assign object to pointers without initialising it to NULL? suppose if i have a class TEST class ELEMENT { private: int a; string s1;
107
by: DaveC | last post by:
I always used to initialise variables at declaration, then a couple of colleagues started telling me it was bad practice and that the compiler should be left to spot the use of uninitilised...
0
by: Juha Nieminen | last post by:
Angus wrote: Followup question: Will this be possible with the upcoming C++ standard? (AFAIK something similar will be possible with std::vector.)
32
by: Simon L | last post by:
BOOL bMyarray; memset(bMyArray , 0xFF, sizeof(BOOL) * 1000); clean & quick until someone else's code found I was returning -1 to mean true Because my BOOL is 4 bytes long, TRUE in memory...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
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...
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
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...

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.