473,320 Members | 1,814 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,320 software developers and data experts.

Initializing a vector

I can initialize an array like this when I create it:

const double a[3] = { 3.14, 2.72, 0.71 };

can I do the same with a vector<double> ?

or can I create a vector initialized with the contents of a constant
array ?
Jul 22 '05 #1
11 2756
Mats Weber wrote:
I can initialize an array like this when I create it:

const double a[3] = { 3.14, 2.72, 0.71 };

can I do the same with a vector<double> ?

or can I create a vector initialized with the contents of a constant
array ?


const double a[3] = { 3.14, 2.72, 0.71 };
vector<double> v(3);
std::copy(&a[0], &a[sizeof a/sizeof double - 1], v.begin();

- Pete
Jul 22 '05 #2
Pete C. wrote:
Mats Weber wrote:
I can initialize an array like this when I create it:

const double a[3] = { 3.14, 2.72, 0.71 };

can I do the same with a vector<double> ?

or can I create a vector initialized with the contents of a constant
array ?


const double a[3] = { 3.14, 2.72, 0.71 };
vector<double> v(3);
std::copy(&a[0], &a[sizeof a/sizeof double - 1], v.begin();

- Pete


Oops, forgot a closing paren:
std::copy(&a[0], &a[sizeof a/sizeof double - 1], v.begin());

- Pete
Jul 22 '05 #3
"Pete C." wrote:

Pete C. wrote:
Mats Weber wrote:
I can initialize an array like this when I create it:

const double a[3] = { 3.14, 2.72, 0.71 };

can I do the same with a vector<double> ?

or can I create a vector initialized with the contents of a constant
array ?


const double a[3] = { 3.14, 2.72, 0.71 };
vector<double> v(3);
std::copy(&a[0], &a[sizeof a/sizeof double - 1], v.begin();

- Pete


Oops, forgot a closing paren:
std::copy(&a[0], &a[sizeof a/sizeof double - 1], v.begin());


This drops the last element of the array. It also hard-codes the type of
the array. Here's an improvement:

std::copy(a, a + sizeof(a)/sizeof(*a), v.begin());

Even better, use vector's default constructor, so you can change the
number of elements in the initializer without having to change the
constructor call:

vector<double> v;
std::copy(a, a + sizeof(a)/sizeof(*a), inserter(v, v.end());

Better still, use the vector constructor that handles ranges:

vector v(a, a + sizeof(a)/sizeof(*a));

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #4
Mats Weber wrote:
I can initialize an array like this when I create it:

const double a[3] = { 3.14, 2.72, 0.71 };

can I do the same with a vector<double> ?

or can I create a vector initialized with the contents of a constant
array ?


Good answers from Pete :-)

Another treatment from Leor:

InitUtil:
An STL Container Initialization Library
by Leor Zolman

http://www.bdsoft.com/tools/initutil.html
Jul 22 '05 #5
Pete Becker <pe********@acm.org> wrote in message news:<40***************@acm.org>...
"Pete C." wrote: Better still, use the vector constructor that handles ranges:

vector v(a, a + sizeof(a)/sizeof(*a));


Isn't there a way to do this without using sizeof(), which is a
representation attribute and should not be required at this level of
abstraction ?
Jul 22 '05 #6
Mats Weber wrote:

Pete Becker <pe********@acm.org> wrote in message news:<40***************@acm.org>...
"Pete C." wrote:

Better still, use the vector constructor that handles ranges:

vector v(a, a + sizeof(a)/sizeof(*a));


Isn't there a way to do this without using sizeof(), which is a
representation attribute and should not be required at this level of
abstraction ?


I've seen attempts at doing it. They're more trouble than they're worth.
sizeof does the job.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #7
Mats Weber wrote:
vector v(a, a + sizeof(a)/sizeof(*a));


Isn't there a way to do this without using sizeof(), which is a
representation attribute and should not be required at this level of
abstraction ?


You can use this template:

template <class C, size_t N>
size_t dim_array (C (&) [N])
{ return N; }

vector v (a, a + dim_array (a) );

--
Salu2
Jul 22 '05 #8

"Mats Weber" <ma***@bluewin.ch> wrote in message news:9c**************************@posting.google.c om...
Pete Becker <pe********@acm.org> wrote in message news:<40***************@acm.org>...
"Pete C." wrote:

Better still, use the vector constructor that handles ranges:

vector v(a, a + sizeof(a)/sizeof(*a));


Isn't there a way to do this without using sizeof(), which is a
representation attribute and should not be required at this level of
abstraction ?


Why is sizeof not suited to solve your problem?
--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 22 '05 #9
In article <2j*************@uni-berlin.de>,
"Alex Vinokur" <al****@big.foot.com> wrote:
"Mats Weber" <ma***@bluewin.ch> wrote in message
news:9c**************************@posting.google. com...
Pete Becker <pe********@acm.org> wrote in message
news:<40***************@acm.org>...
> "Pete C." wrote:

> Better still, use the vector constructor that handles ranges:
>
> vector v(a, a + sizeof(a)/sizeof(*a));


Isn't there a way to do this without using sizeof(), which is a
representation attribute and should not be required at this level of
abstraction ?


Why is sizeof not suited to solve your problem?


Because sizeof deals with the machine representation of the array, which
I do not care about. All I need to know is the number of elements and
their values, not how they are represented in memory. Suppose we are
dealing with arrays of short, and some implementation decides to align
arrays of short on 32-bit boundaries (i.e. addresses that are multiples
of 4). In that case, given

short a[] = {1, 2, 4};

sizeof(short) = 2, sizeof(a) = 12 and the sizeof trick fails.
Jul 22 '05 #10
Mats Weber wrote:

In article <2j*************@uni-berlin.de>,
"Alex Vinokur" <al****@big.foot.com> wrote:
"Mats Weber" <ma***@bluewin.ch> wrote in message
news:9c**************************@posting.google. com...
Pete Becker <pe********@acm.org> wrote in message
news:<40***************@acm.org>...
> "Pete C." wrote:

> Better still, use the vector constructor that handles ranges:
>
> vector v(a, a + sizeof(a)/sizeof(*a));

Isn't there a way to do this without using sizeof(), which is a
representation attribute and should not be required at this level of
abstraction ?


Why is sizeof not suited to solve your problem?


Because sizeof deals with the machine representation of the array, which
I do not care about. All I need to know is the number of elements and
their values, not how they are represented in memory. Suppose we are
dealing with arrays of short, and some implementation decides to align
arrays of short on 32-bit boundaries (i.e. addresses that are multiples
of 4).


That's not possible.
Padding bytes are not allowed in an array. All array elements have to be
contiguous, withoug padding bytes.

But of course it is allowed for the compiler to add padding bytes to
the data type you make an array from.

struct MyType
{
short i;
};

The compiler is allowed to add padding bytes to that struct to bring
it to a size of 4 (with your numbers from above). In this case
sizeof will correctly return 4 and the 'sizeof' trick for getting
the number of elements in an array will continue to work.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #11

"Mats Weber" <ma***@bluewin.ch> wrote in message
news:ma*************************@sicinfo.epfl.ch.. .
In article <2j*************@uni-berlin.de>,
"Alex Vinokur" <al****@big.foot.com> wrote:
"Mats Weber" <ma***@bluewin.ch> wrote in message
news:9c**************************@posting.google. com...
Pete Becker <pe********@acm.org> wrote in message
news:<40***************@acm.org>...
> "Pete C." wrote:

> Better still, use the vector constructor that handles ranges:
>
> vector v(a, a + sizeof(a)/sizeof(*a));

Isn't there a way to do this without using sizeof(), which is a
representation attribute and should not be required at this level of
abstraction ?


Why is sizeof not suited to solve your problem?


Because sizeof deals with the machine representation of the array, which
I do not care about. All I need to know is the number of elements and
their values, not how they are represented in memory. Suppose we are
dealing with arrays of short, and some implementation decides to align
arrays of short on 32-bit boundaries (i.e. addresses that are multiples
of 4). In that case, given

short a[] = {1, 2, 4};

sizeof(short) = 2, sizeof(a) = 12 and the sizeof trick fails.


Such an implementation would be non-conforming. sizeof must include any
bytes needed for alignment.

The sizeof trick always works, the C++ standard requires it to.

But if you don't like it use Julian's template.

john
Jul 22 '05 #12

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

Similar topics

4
by: hrmadhu | last post by:
Hi, I wish to declare a vector of deque of int, which I do as follows. #include<vector> #include<deque> #include<iostream> using namespace std; int main(int argc, char* argv) {
16
by: Emanuel Ziegler | last post by:
Hi, I am using the vector class from the STL. Normally, I initialize values element by element, which is very uncomfortable and sometimes impossible (e.g. when passing a constant vector to an...
6
by: Matthias | last post by:
Hi, say I have a vector v1: std::vector<SomeType> v1; and I need a vector v2 of pointers to v1's elements: std::vector<SomeType*> v2;
5
by: pmatos | last post by:
Hi all, I have a vector of vector of ints, I could use C approach by using int but I think C++ vector<vector<int> > would be easier to manage. So I have a function which creates and initializes...
9
by: Dennis Jones | last post by:
Hi, I have some old code that I am refactoring to use smart pointers and have run into a small problem. My original code looks something like this: class WorkerThread { std::map<int,...
3
by: eriwik | last post by:
I use the following structure to store filenames for one or more "sets" grouped together by a number: map<int, map<string> > fileSets; As arguments to the constructor I send a...
4
by: jayharris | last post by:
I'm having a ton of trouble initializing a multi-dimensional array inside a constructor, largely because I don't know the size of the array until runtime. I have a class that looks like this: ...
13
by: John | last post by:
Is this a valid C++ program that will not crash on any machine? #include <iostream> using namespace std; int main( void ) { int i; cin >i; double X; X = 1123;
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<...
4
by: Peskov Dmitry | last post by:
class simple_class { int data; public: simple_class() {data=10;}; simple_class(int val) : data(val){} }; int main() {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.