<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>CppAD::vector Template Class: Example and Test</title>
<meta name="description" id="description" content="CppAD::vector Template Class: Example and Test"/>
<meta name="keywords" id="keywords" content=" vector Cppad Cppad::vector example test "/>
<style type='text/css'>
body { color : black }
body { background-color : white }
A:link { color : blue }
A:visited { color : purple }
A:active { color : purple }
</style>
<script type='text/javascript' language='JavaScript' src='_cppad_vector.cpp_xml.js'>
</script>
</head>
<body>
<table><tr>
<td>
<a href="http://www.coin-or.org/CppAD/" target="_top"><img border="0" src="_image.gif"/></a>
</td>
<td><a href="cppad_vector.xml" target="_top">Prev</a>
</td><td><a href="vector_bool.cpp.xml" target="_top">Next</a>
</td><td>
<select onchange='choose_across0(this)'>
<option>Index-&gt;</option>
<option>contents</option>
<option>reference</option>
<option>index</option>
<option>search</option>
<option>external</option>
</select>
</td>
<td>
<select onchange='choose_up0(this)'>
<option>Up-&gt;</option>
<option>CppAD</option>
<option>library</option>
<option>CppAD_vector</option>
<option>cppad_vector.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>CppAD-&gt;</option>
<option>Install</option>
<option>Introduction</option>
<option>AD</option>
<option>ADFun</option>
<option>preprocessor</option>
<option>multi_thread</option>
<option>library</option>
<option>ipopt_solve</option>
<option>Example</option>
<option>speed</option>
<option>Appendix</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>library-&gt;</option>
<option>ErrorHandler</option>
<option>NearEqual</option>
<option>speed_test</option>
<option>SpeedTest</option>
<option>time_test</option>
<option>NumericType</option>
<option>CheckNumericType</option>
<option>SimpleVector</option>
<option>CheckSimpleVector</option>
<option>nan</option>
<option>pow_int</option>
<option>Poly</option>
<option>LuDetAndSolve</option>
<option>RombergOne</option>
<option>RombergMul</option>
<option>Runge45</option>
<option>Rosen34</option>
<option>OdeErrControl</option>
<option>OdeGear</option>
<option>OdeGearControl</option>
<option>CppAD_vector</option>
<option>thread_alloc</option>
<option>index_sort</option>
<option>BenderQuad</option>
<option>opt_val_hes</option>
<option>LuRatio</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>CppAD_vector-&gt;</option>
<option>cppad_vector.cpp</option>
<option>vector_bool.cpp</option>
</select>
</td>
<td>cppad_vector.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>CppAD::vector Template Class: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 

# include &lt;cppad/vector.hpp&gt;
# include &lt;cppad/check_simple_vector.hpp&gt;
# include &lt;sstream&gt; // sstream and string are used to test output operation
# include &lt;string&gt;

bool CppAD_vector(void)
{	bool ok = true;
	using CppAD::vector;     // so can use vector instead of CppAD::vector 
	typedef double Type;     // change double to test other types

	// check Simple Vector specifications
	CppAD::CheckSimpleVector&lt; Type, vector&lt;Type&gt; &gt;();

	vector&lt;Type&gt; x;          // default constructor 
	ok &amp;= (x.size() == 0);

	x.resize(2);             // resize and set element assignment
	ok &amp;= (x.size() == 2);
	x[0] = Type(0);
	x[1] = Type(1);

	vector&lt;Type&gt; y(2);       // sizing constructor
	ok &amp;= (y.size() == 2);

	const vector&lt;Type&gt; z(x); // copy constructor and const element access
	ok &amp;= (z.size() == 2);
	ok &amp;= ( (z[0] == Type(0)) &amp;&amp; (z[1] == Type(1)) );

	x[0] = Type(2);          // modify, assignment changes x
	ok &amp;= (x[0] == Type(2));

	x = y = z;               // vector assignment
	ok &amp;= ( (x[0] == Type(0)) &amp;&amp; (x[1] == Type(1)) );
	ok &amp;= ( (y[0] == Type(0)) &amp;&amp; (y[1] == Type(1)) );
	ok &amp;= ( (z[0] == Type(0)) &amp;&amp; (z[1] == Type(1)) );

	// test of output
	std::string        correct= &quot;{ 0, 1 }&quot;;
	std::string        str;
	std::ostringstream buf;
	buf &lt;&lt; z;
	str = buf.str();
	ok &amp;= (str == correct);

	// test resize(0), capacity, and clear
	size_t i = x.capacity();
	ok      &amp;= i &gt; 0;
	x.resize(0);
	ok      &amp;= i == x.capacity();
	x.clear();
	ok      &amp;= 0 == x.capacity();

	// test of push_back scalar and capacity
	size_t N = 100;
	for(i = 0; i &lt; N; i++)
	{	size_t old_capacity = x.capacity();	
		x.push_back( Type(i) );
		ok &amp;= (i+1) == x.size();
		ok &amp;= i &lt; x.capacity();
		ok &amp;= (i == old_capacity) || old_capacity == x.capacity(); 
	}
	for(i = 0; i &lt; N; i++)
		ok &amp;= ( x[i] == Type(i) );

	// test of data
	Type* data = x.data();
	for(i = 0; i &lt; N; i++)
	{	ok &amp;= data[i] == Type(i);
		data[i] = Type(N - i);
		ok &amp;= x[i] == Type(N - i);
	}

	// test of push_vector 
	x.push_vector(x);
	ok &amp;= (x.size() == 2 * N);
	for(i = 0; i &lt; N; i++)
	{	ok &amp;= x[i] == Type(N - i);
		ok &amp;= x[i+N] == Type(N - i);
	}


	return ok;
}

</pre>

</font></code>


<hr/>Input File: example/cppad_vector.cpp

</body>
</html>
