inline assignment of vectors

F

foice

why can't I make the same inline assignment for vectors?
float fp_values[] = { 0.1, 0.2 , 0.3, 0.4};

as for instance this
vector<float> v=(1.2, 3.3, 4.);

any idea how to do that in one line and for arbitrary lenght of the
initialization?

thanks roberto
 
S

Stuart Golodetz

foice said:
foice said:
why can't I make the same inline assignment for vectors?
float fp_values[] = { 0.1, 0.2 , 0.3, 0.4};
as for instance this
vector<float> v=(1.2, 3.3, 4.);
any idea how to do that in one line and for arbitrary lenght of the
initialization?
See here :

http://live.boost.org/doc/libs/1_36_0/libs/assign/doc/index.html

i see ... it's very interesting. though not standard :(

It's portable, though, which is usually good enough :)

Stu
 
Á

Ángel José Riesgo

why can't I make the same inline assignment for vectors?
float fp_values[] = { 0.1, 0.2 , 0.3, 0.4};

as for instance this
vector<float> v=(1.2, 3.3, 4.);

As others have mentioned, you can do that sort of thing in the
forthcoming C++ standard (C++0x). The correct syntax will be:

std::vector<float> v = {1.2f, 3.3f, 4.f};

I think the latest version of the GCC compiler already supports this
if you enable the experimental C++0x features.
any idea how to do that in one line and for arbitrary lenght of the
initialization?

In the current C++ standard (C++03), you can't do this in one line.
But if you can live with doing it in three lines, this is the way I do
it:

const float kVArray[] = {1.2f, 3.3f, 4.f};

const size_t kVArraySize = sizeof(kVArray) / sizeof(*kVArray);

std::vector<float> v(kVArray, kVArray + kVArraySize);

Hope this helps.

Ángel José Riesgo
 
J

James Kanze

why can't I make the same inline assignment for vectors?
float fp_values[] = { 0.1, 0.2 , 0.3, 0.4};
as for instance this
vector<float> v=(1.2, 3.3, 4.);
As others have mentioned, you can do that sort of thing in the
forthcoming C++ standard (C++0x). The correct syntax will be:
std::vector<float> v = {1.2f, 3.3f, 4.f};
I think the latest version of the GCC compiler already supports this
if you enable the experimental C++0x features.
In the current C++ standard (C++03), you can't do this in one line.
But if you can live with doing it in three lines, this is the way I do
it:
const float kVArray[] = {1.2f, 3.3f, 4.f};
const size_t kVArraySize = sizeof(kVArray) / sizeof(*kVArray);
std::vector<float> v(kVArray, kVArray + kVArraySize);

It's usual to define the template functions begin and end, so
you can write:

float const kvArray[] = { 1.2f, 3.3f, 4.f };
std::vector<float> v( begin( kvArray ), end( kvArray ) );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top