variable size of an arrary

O

ottawajn

Dear there,

I want to do the follows.

(1)initial an array, say myarray[]={1 2 3.1 4};

(2)insert zeros in myarray, like {1,0,2,0,3.1,0,4,0};

(3)let myarray[]={1,0,2,0,3.1,0,4,0}; (I have not idea how to figure it
out)

Therefore, the size of array myarray is variable.

Could anyone please help me to figure out the 3rd step?

Thank you,

Cajn
 
V

Victor Bazarov

ottawajn said:
I want to do the follows.

(1)initial an array, say myarray[]={1 2 3.1 4};

(2)insert zeros in myarray, like {1,0,2,0,3.1,0,4,0};

(3)let myarray[]={1,0,2,0,3.1,0,4,0}; (I have not idea how to figure
it out)

Therefore, the size of array myarray is variable.

Could anyone please help me to figure out the 3rd step?

Arrays cannot chage their size. If you initialise the array with
like you showed here (you need commas between the values, not spaces),
you will not be able to insert anything there. You need to "reserve"
the space for zeros. It means you need to define the size manually:

double myarray[9] = { ...

And then, to insert a single value (zero) you could write a loop and
move all elements from the insertion position till the last one one
position towards the end. To insert several values you can call your
function in another loop.

However, it is better to use 'std::vector' since it can do all those
things for you (well, almost).

V
 
O

ottawajn

Thanks.

My concern is how to use vector. Could you explain it ?

Is the following right?
//***********************
vector <float> myarray;

//how to define myarray={1,2,3.1,4}?

myarray.size(8);

//for...
{
myarry=...
}

//myarray={1,0,2,0,3.1,0,4,0};
//***************************

Cajn


Victor said:
ottawajn said:
I want to do the follows.

(1)initial an array, say myarray[]={1 2 3.1 4};

(2)insert zeros in myarray, like {1,0,2,0,3.1,0,4,0};

(3)let myarray[]={1,0,2,0,3.1,0,4,0}; (I have not idea how to figure
it out)

Therefore, the size of array myarray is variable.

Could anyone please help me to figure out the 3rd step?

Arrays cannot chage their size. If you initialise the array with
like you showed here (you need commas between the values, not spaces),
you will not be able to insert anything there. You need to "reserve"
the space for zeros. It means you need to define the size manually:

double myarray[9] = { ...

And then, to insert a single value (zero) you could write a loop and
move all elements from the insertion position till the last one one
position towards the end. To insert several values you can call your
function in another loop.

However, it is better to use 'std::vector' since it can do all those
things for you (well, almost).

V
 
M

mlimber

ottawajn said:
I want to do the follows.

(1)initial an array, say myarray[]={1 2 3.1 4};

(2)insert zeros in myarray, like {1,0,2,0,3.1,0,4,0};

(3)let myarray[]={1,0,2,0,3.1,0,4,0}; (I have not idea how to figure it
out)

Therefore, the size of array myarray is variable.

Could anyone please help me to figure out the 3rd step?

As Victor says, you can't do this with arrays. In fact, you probably
shouldn't be using arrays anyway (see
<http://parashift.com/c++-faq-lite/containers.html#faq-34.1>).

Now, to answer your question, you could do something like this (not
tested):

#include <vector>
using namespace std;

template<typename T>
class Initializer
{
vector<T> v_;
public:
Initializer( const typename vector<T>::size_type capacity=0 )
{
v_.reserve( capacity );
}

Initializer& Add( const T& t )
{
v_.push_back(t);
return *this;
}

operator vector<T>() const
{
return v_;
}
};

vector<double> InsertZeros( const vector<double>& v )
{
vector<double> v2;
v2.reserve( v.size() * 2 );
for( unsigned i=0; i < v.size(); ++i )
{
v2.push_back( v[ i ] );
v2.push_back( 0 );
}
return v2;
}

int main()
{
// Step 1
vector<double> v = Initializer<double>( 4 )
.Add(1.0)
.Add(2.0)
.Add(3.0)
.Add(4.0);

// Step 2
vector<double> z = InsertZeros( v );

// Step 3
v.swap( z );

return 0;
}

Of course there are various other ways to do the same thing and some
optimizations that you might apply, but that's the gist.

Cheers! --M
 
O

ottawajn

mlimber said:
ottawajn said:
I want to do the follows.

(1)initial an array, say myarray[]={1 2 3.1 4};

(2)insert zeros in myarray, like {1,0,2,0,3.1,0,4,0};

(3)let myarray[]={1,0,2,0,3.1,0,4,0}; (I have not idea how to figure it
out)

Therefore, the size of array myarray is variable.

Could anyone please help me to figure out the 3rd step?

As Victor says, you can't do this with arrays. In fact, you probably
shouldn't be using arrays anyway (see
<http://parashift.com/c++-faq-lite/containers.html#faq-34.1>).

Now, to answer your question, you could do something like this (not
tested):

#include <vector>
using namespace std;

template<typename T>
class Initializer
{
vector<T> v_;
public:
Initializer( const typename vector<T>::size_type capacity=0 )
{
v_.reserve( capacity );
}

Initializer& Add( const T& t )
{
v_.push_back(t);
return *this;
}

operator vector<T>() const
{
return v_;
}
};

vector<double> InsertZeros( const vector<double>& v )
{
vector<double> v2;
v2.reserve( v.size() * 2 );
for( unsigned i=0; i < v.size(); ++i )
{
v2.push_back( v[ i ] );
v2.push_back( 0 );
}
return v2;
}

int main()
{
// Step 1
vector<double> v = Initializer<double>( 4 )
.Add(1.0)
.Add(2.0)
.Add(3.0)
.Add(4.0);

// Step 2
vector<double> z = InsertZeros( v );

// Step 3
v.swap( z );

return 0;
}

Of course there are various other ways to do the same thing and some
optimizations that you might apply, but that's the gist.

Cheers! --M
 
O

ottawajn

Thank you very much,

Cajn
ottawajn said:
I want to do the follows.

(1)initial an array, say myarray[]={1 2 3.1 4};

(2)insert zeros in myarray, like {1,0,2,0,3.1,0,4,0};

(3)let myarray[]={1,0,2,0,3.1,0,4,0}; (I have not idea how to figure it
out)

Therefore, the size of array myarray is variable.

Could anyone please help me to figure out the 3rd step?

As Victor says, you can't do this with arrays. In fact, you probably
shouldn't be using arrays anyway (see
<http://parashift.com/c++-faq-lite/containers.html#faq-34.1>).

Now, to answer your question, you could do something like this (not
tested):

#include <vector>
using namespace std;

template<typename T>
class Initializer
{
vector<T> v_;
public:
Initializer( const typename vector<T>::size_type capacity=0 )
{
v_.reserve( capacity );
}

Initializer& Add( const T& t )
{
v_.push_back(t);
return *this;
}

operator vector<T>() const
{
return v_;
}
};

vector<double> InsertZeros( const vector<double>& v )
{
vector<double> v2;
v2.reserve( v.size() * 2 );
for( unsigned i=0; i < v.size(); ++i )
{
v2.push_back( v[ i ] );
v2.push_back( 0 );
}
return v2;
}

int main()
{
// Step 1
vector<double> v = Initializer<double>( 4 )
.Add(1.0)
.Add(2.0)
.Add(3.0)
.Add(4.0);

// Step 2
vector<double> z = InsertZeros( v );

// Step 3
v.swap( z );

return 0;
}

Of course there are various other ways to do the same thing and some
optimizations that you might apply, but that's the gist.

Cheers! --M
 
G

Gavin Deane

Please don't top-post. Thanks.
ottawajn said:
Thanks.

My concern is how to use vector. Could you explain it ?

Is the following right?
//***********************
vector <float> myarray;

Assuming <vector> is included, and an appropriate using declaration or
definition is provided, then that will declare a vector of floats
called myarray.
//how to define myarray={1,2,3.1,4}?

There are lots of ways to get values into a vector. push_back is
probably the easiest to start with.

myarray.push_back(1);
myarray.push_back(2);
etc.
myarray.size(8);

Have you tried compiling that? The member function size takes no
parameters and returnd the current size of the vector.
//for...
{
myarry=...
}


You can loop over the individual elements or you can use iterators to,
well, iterate through the vector. You've clearly found some information
about vectors to have presented this code. Has that come from a
textbook? Which one - if it's any good it should show you how to get up
and running with vectors and other standard library elements
(algorithms and iterators) that work with them. For further reference,
http://www.josuttis.com/libbook/ is invaluable.

Gavin Deane
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top