vector initialization 
Author Message
 vector initialization

I'm trying to preload a vector<int> with (18) 6digit decimal numbers.
The program works the way I have it but I'm first initializing an array
with the numbers, then iterating them into the vector as shown below:

 // Just for initializing the vector
 long Acc[ArrSize] = { 5658845,  4520125,  7895122,  8777541,  8451277,
1302850,
        8080152,  4562555,  5552012,  5050552,  7825877,  1250255,
        1005231,  6545231,  3852085,  7576651,  4581002 };

 vector<long> Accounts;
 long Number;
 for (int ndx = 0;ndx < ArrSize;ndx++) // vector initialization
  Accounts.push_back(Acc[ndx]);

Again this works, but isn't there a better way to preload a series of
numbers or values into a vector?

Thanks,
Del



Sun, 12 Oct 2003 10:01:05 GMT  
 vector initialization

: I'm trying to preload a vector<int> with (18) 6digit decimal numbers.
: The program works the way I have it but I'm first initializing an
: array with the numbers, then iterating them into the vector as shown
: below:
:
:  // Just for initializing the vector
:  long Acc[ArrSize] = { 5658845, 4520125, 7895122, 8777541, 8451277,
: 1302850,
:         8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
:         1005231, 6545231, 3852085, 7576651, 4581002 };
:
:  vector<long> Accounts;
:  long Number;
:  for (int ndx = 0;ndx < ArrSize;ndx++) // vector initialization
:   Accounts.push_back(Acc[ndx]);
:
:
: Again this works, but isn't there a better way to preload a series of
: numbers or values into a vector?
:
: Thanks,
: Del

I don't know of a way without the array, but if you aren't reading
these numbers in from some type of input where would you keep
them?

I can make it a bit more attractive:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    int arr[] = { 1,2,3,4,5,6,7,8,9 };

    vector<int> a(arr, arr + (sizeof(arr) / sizeof(arr[0])));
    vector<int>::iterator it = a.begin();

    for(; it != a.end(); ++it) cout << *it << ' ';

    cout << endl;
    return 0;

Quote:
}

R.


Sun, 12 Oct 2003 10:44:30 GMT  
 vector initialization

Quote:

> I'm trying to preload a vector<int> with (18) 6digit
> decimal numbers. The program works the way I have
> it but I'm first initializing an array with the numbers,
> then iterating them into the vector as shown below:

>  // Just for initializing the vector
>  long Acc[ArrSize] = { 5658845,  4520125,  7895122,
> 8777541,  8451277,  1302850,  8080152,  4562555,
> 5552012,  5050552,  7825877,  1250255,  1005231,
> 6545231,  3852085,  7576651,  4581002 };

>  vector<long> Accounts;
>  long Number;
>  for (int ndx = 0;ndx < ArrSize;ndx++) // vector initialization
>   Accounts.push_back(Acc[ndx]);

> Again this works, but isn't there a better way to preload a
> series of numbers or values into a vector?

There is a vector constructor that takes two iterators, and builds a
vector based on the sequence of data between them. Since pointers into
arrays are iterators, too, you could use that.

NB: Untested code - beware typos.

int initialValues[] = {1, 1, 2, 3, 5, 8, 13};
const int numInitialValues = sizeof(initialValues) /
sizeof(*initialValues);
std::vector<int> vec(initialValues, initialValues + numInitialValues);

Before doing this, though, consider that it's possible you don't really
want a vector at all here. If your data is constant, you might well be
able to use it directly from the array, without the overhead of a
vector. This is one of those occasions where, sometimes, a container
just isn't worth it.

You might like to take a peek at my article at
  http://www.btinternet.com/~chrisnewton/programming/containers/main.htm
for more thoughts on using containers vs. using arrays (and a few useful
tricks, including using the constructor as shown above).

Hope that helps,
Chris



Mon, 13 Oct 2003 07:12:56 GMT  
 vector initialization
Thanks to both Chris and Scott,
The numbers were a starting point, with a vector later to be expanded.  Your
solution wasn't everything I was looking for but still far better than I had
done with the "for" loop.  I'll be using your method in the future
Thanks,
Del
Quote:


> > I'm trying to preload a vector<int> with (18) 6digit
> > decimal numbers. The program works the way I have
> > it but I'm first initializing an array with the numbers,
> > then iterating them into the vector as shown below:

> >  // Just for initializing the vector
> >  long Acc[ArrSize] = { 5658845,  4520125,  7895122,
> > 8777541,  8451277,  1302850,  8080152,  4562555,
> > 5552012,  5050552,  7825877,  1250255,  1005231,
> > 6545231,  3852085,  7576651,  4581002 };

> >  vector<long> Accounts;
> >  long Number;
> >  for (int ndx = 0;ndx < ArrSize;ndx++) // vector initialization
> >   Accounts.push_back(Acc[ndx]);

> > Again this works, but isn't there a better way to preload a
> > series of numbers or values into a vector?

> There is a vector constructor that takes two iterators, and builds a
> vector based on the sequence of data between them. Since pointers into
> arrays are iterators, too, you could use that.

> NB: Untested code - beware typos.

> int initialValues[] = {1, 1, 2, 3, 5, 8, 13};
> const int numInitialValues = sizeof(initialValues) /
> sizeof(*initialValues);
> std::vector<int> vec(initialValues, initialValues + numInitialValues);

> Before doing this, though, consider that it's possible you don't really
> want a vector at all here. If your data is constant, you might well be
> able to use it directly from the array, without the overhead of a
> vector. This is one of those occasions where, sometimes, a container
> just isn't worth it.

> You might like to take a peek at my article at
>   http://www.btinternet.com/~chrisnewton/programming/containers/main.htm
> for more thoughts on using containers vs. using arrays (and a few useful
> tricks, including using the constructor as shown above).

> Hope that helps,
> Chris



Wed, 15 Oct 2003 11:47:20 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Help on array/vector initialization please

2. multidimensional vector initialization

3. VECTOR within a VECTOR

4. vector question when replacing array[10][20] with vectors

5. proper way of refering to pointer of vector to vector

6. Vector of vectors - compiler doesn't understand?

7. How to iterate through a vector of vectors?

8. Vectors Within Vectors

9. vector::pop_back and vector::erase leaking memory?

10. Problem initializing a vector of vectors

11. NEWBIE: Initialization of member variables

12. Aggregate Initialization of Managed Structure

 

 
Powered by phpBB® Forum Software