blob: 0d9d1993618818721ca9db31a0f01c9e2852103c [file] [log] [blame]
<HTML>
<HEAD>
<TITLE>Declaring a valarray</TITLE>
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD>
<BODY BGCOLOR=#FFFFFF>
<A HREF="22-1.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="22-3.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Apache C++ Standard Library User's Guide</B></DIV>
<H2>22.2 Declaring a valarray</H2>
<A NAME="idx460"><!></A>
<P>A <B><I><A HREF="../stdlibref/valarray.html">valarray</A></I></B> can be constructed by any of the following means:</P>
<UL>
<LI><P CLASS="LIST"><B>Default constructor</B>. The default constructor is provided to allow arrays of <B><I><A HREF="../stdlibref/valarray.html">valarray</A></I></B>s. Use the resize function to adjust the <B><I>valarray</I></B> after construction.</P></LI>
<LI><P CLASS="LIST"><B>Construction and initialization.</B> A <B><I><A HREF="../stdlibref/valarray.html">valarray</A></I></B> provides two ways to start with a given size and values: </P></LI>
<UL>
<LI><P CLASS="LIST"><SAMP>valarray(const T&amp; v,size_t n)</SAMP> constructs a <B><I><A HREF="../stdlibref/valarray.html">valarray</A></I></B> of size <SAMP>n</SAMP> with each element initialized to the value <SAMP>v</SAMP>. </P></LI>
<LI><P CLASS="LIST"><SAMP>valarray(const T* p,size_t n)</SAMP> constructs a <B><I><A HREF="../stdlibref/valarray.html">valarray</A></I></B> of size <SAMP>n</SAMP> with each element initialized to the corresponding element of the array pointed to by <SAMP>p</SAMP>. This constructor allows a program to transfer data with maximum efficiency from an ordinary `C' array (resulting from a file operation, for example) into a <B><I>valarray</I></B>.</P></LI>
</UL>
<LI><P CLASS="LIST"><B>Copy constructor</B>. The copy constructor has value semantics.</P></LI>
<LI><P CLASS="LIST"><B>Conversion constructors</B>. Class <B><I><A HREF="../stdlibref/valarray.html">valarray</A></I></B> provides four conversion constructors for converting from auxiliary classes generated by subscript operations. We'll look at these classes in detail and describe the use of the conversion constructors in <A HREF="22-4.html#2242">Section&nbsp;22.4.2</A>.</P></LI>
</UL>
<P>The following example shows the use of the first three categories of constructors:</P>
<UL><PRE>
#include &lt;valarray&gt;
std::valarray&lt;int&gt; v1; // construct an empty valarray
std::valarray&lt;int&gt; v2(1,3); // construct a valarray of three
// elements, all initialized to 1
v1.resize(3,2); // resize the first valarray to three
// elements, all initialized to 2
std::valarray&lt;int&gt; v3(v1); // v3 gets a copy of v1's elements.
</PRE></UL>
<BR>
<HR>
<A HREF="22-1.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="22-3.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A>
<!-- Google Analytics tracking code -->
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-1775151-1";
urchinTracker();
</script>
<!-- end of Google Analytics tracking code -->
</BODY>
</HTML>