variable size of an arrary | | |
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 | | | | re: variable size of an arrary
ottawajn wrote: Quote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask | | | | re: variable size of an arrary
Thanks.
My concern is how to use vector. Could you explain it ?
Is the following right?
//***********************
vector <floatmyarray;
//how to define myarray={1,2,3.1,4}?
myarray.size(8);
//for...
{
myarry[i]=...
}
//myarray={1,0,2,0,3.1,0,4,0};
//***************************
Cajn
Victor Bazarov wrote: Quote:
ottawajn wrote: Quote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
| | | | re: variable size of an arrary
ottawajn wrote: Quote:
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<Tv_;
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<doubleInsertZeros( const vector<double>& v )
{
vector<doublev2;
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<doublev = Initializer<double>( 4 )
.Add(1.0)
.Add(2.0)
.Add(3.0)
.Add(4.0);
// Step 2
vector<doublez = 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 | | | | re: variable size of an arrary
Please don't top-post. Thanks.
ottawajn wrote: Quote:
Thanks.
>
My concern is how to use vector. Could you explain it ?
>
Is the following right?
//***********************
vector <floatmyarray;
Assuming <vectoris included, and an appropriate using declaration or
definition is provided, then that will declare a vector of floats
called myarray. Quote:
//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. Have you tried compiling that? The member function size takes no
parameters and returnd the current size of the vector. Quote:
//for...
{
myarry[i]=...
}
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 | | | | re: variable size of an arrary
Thank you very much,
Cajn
mlimber wrote: Quote:
ottawajn wrote: Quote:
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<Tv_;
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<doubleInsertZeros( const vector<double>& v )
{
vector<doublev2;
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<doublev = Initializer<double>( 4 )
.Add(1.0)
.Add(2.0)
.Add(3.0)
.Add(4.0);
>
// Step 2
vector<doublez = 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
| | | | re: variable size of an arrary
mlimber wrote: Quote:
ottawajn wrote: Quote:
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<Tv_;
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<doubleInsertZeros( const vector<double>& v )
{
vector<doublev2;
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<doublev = Initializer<double>( 4 )
.Add(1.0)
.Add(2.0)
.Add(3.0)
.Add(4.0);
>
// Step 2
vector<doublez = 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
| | | | re: variable size of an arrary
ottawajn wrote: Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html> |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,414 network members.
|