📄 c++ stl tutorial.mht
字号:
cout << *rii << endl;
}
cout << endl << "Sample Output:" << endl;
cout << SS.size() << endl;
cout << SS[2] << endl;
swap(SS[0], SS[2]);
cout << SS[2] << endl;
}
</PRE></TD></TR></TBODY></TABLE></DD></DL>Compile: <TT>g++=20
exampleVector.cpp</TT>=20
<P>Run: <TT>./a.out</TT>=20
<P>Output:=20
<DL>
<DD><PRE>Loop by index:
The number is 10
The number is 20
The number is 30
Constant Iterator:
The number is 10
The number is 20
The number is 30
Reverse Iterator:
The number is 30
The number is 20
The number is 10
Sample Output:
3
The number is 30
The number is 10
</PRE></DD></DL>
<P>
<HR>
<P><B>Two / Three / Multi Dimensioned arrays using vector:</B>=20
<P>A two dimensional array is a vector of vectors. The vector =
contructor=20
can initialize the length of the array and set the initial value.=20
<P>
<DL>
<DD>Example of a vector of vectors to represent a two =
dimensional array:=20
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE>#include <iostream>
#include <vector>
using namespace std;
main()
{
// Declare size of two dimensional array and initialize.
vector< vector<int> > vI2Matrix(3, =
vector<int>(2,0)); =20
vI2Matrix[0][0] =3D 0;
vI2Matrix[0][1] =3D 1;
vI2Matrix[1][0] =3D 10;
vI2Matrix[1][1] =3D 11;
vI2Matrix[2][0] =3D 20;
vI2Matrix[2][1] =3D 21;
cout << "<B>Loop by index</B>:" << endl;
int ii, jj;
for(ii=3D0; ii < 3; ii++)
{
for(jj=3D0; jj < 2; jj++)
{
cout << vI2Matrix[ii][jj] << endl;
}
}
}
</PRE></TD></TR></TBODY></TABLE></DD></DL>Compile: <TT>g++=20
exampleVector2.cpp</TT>=20
<P>Run: <TT>./a.out</TT>=20
<DL>
<DD><PRE>Loop by index:
0
1
10
11
20
21
</PRE></DD></DL>
<P>
<HR>
<P>A three dimensional vector would be declared as:=20
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE>#include <iostream>
#include <vector>
using namespace std;
main()
{
// Vector length of 3 initialized to 0
vector<int> vI1Matrix(3,0);
// Vector length of 4 initialized to hold =
another=20
// vector vI1Matrix which has been =
initialized to 0
vector< vector<int> > vI2Matrix(4, vI1Matrix);
// Vector of length 5 containing two =
dimensional vectors
vector< vector< vector<int> > > vI3Matrix(5, =
vI2Matrix);
...
</PRE></TD></TR></TBODY></TABLE>
<P><B>or declare all in one statement:</B>=20
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE>#include <iostream>
#include <vector>
using namespace std;
main()
{
vector< vector< vector<int> > > vI3Matrix(2, =
vector< vector<int> > (3, vector<int>(4,0)) );
for(int kk=3D0; kk<4; kk++)
{
for(int jj=3D0; jj<3; jj++)
{
for(int ii=3D0; ii<2; ii++)
{
cout << vI3Matrix[ii][jj][kk] << endl;
}
}
}
}
</PRE></TD></TR></TBODY></TABLE></P></DD></DL>
<P>
<HR>
<P>Using an iterator:=20
<DL>
<DD>Example of iterators used with a two dimensional vector.=20
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE>#include <iostream>
#include <vector>
using namespace std;
main()
{
vector< vector<int> > vI2Matrix; // Declare two =
dimensional array
vector<int> A, B;
vector< vector<int> >::iterator iter_ii;
vector<int>::iterator iter_jj;
A.push_back(10);
A.push_back(20);
A.push_back(30);
B.push_back(100);
B.push_back(200);
B.push_back(300);
vI2Matrix.push_back(A);
vI2Matrix.push_back(B);
cout << endl << "<B>Using Iterator</B>:" << endl;
for(iter_ii=3DvI2Matrix.begin(); iter_ii!=3DvI2Matrix.end(); =
iter_ii++)
{
for(iter_jj=3D(*iter_ii).begin(); iter_jj!=3D(*iter_ii).end(); =
iter_jj++)
{
cout << *iter_jj << endl;
}
}
}
</PRE></TD></TR></TBODY></TABLE></DD></DL>Compile: <TT>g++=20
exampleVector2.cpp</TT>=20
<P>Run: <TT>./a.out</TT>=20
<DL>
<DD><PRE>Using Iterator:
10
20
30
100
200
300
</PRE></DD></DL>
<HR>
<P>Constructor/Declaration:=20
<DL>
<DD>
<TABLE cellPadding=3D3 width=3D"100%" border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TH>Method/operator</TH>
<TH>Description</TH></TR>
<TR>
<TD vAlign=3Dtop>vector<T> v;</TD>
<TD>Vector declaration of data type "T".</TD></TR>
<TR>
<TD vAlign=3Dtop>vector<T> v(size_type n);</TD>
<TD>Declaration of vector containing type "T" and of size =
"n"=20
(quantity).</TD></TR>
<TR>
<TD vAlign=3Dtop>vector<T> v(size_type n,const T& =
t);</TD>
<TD>Declaration of vector containing type "T", of size "n"=20
(quantity) containing value "t".<BR>Declaration:=20
<TT>vector(size_type n, const T& t)</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>vector<T> =
v(begin_iterator,end_iterator);</TD>
<TD>Copy of Vector of data type "T" and range begin_iterator =
to=20
end_iterator.<BR>Declaration: <TT>template <CLASS=20
InputIterator>vector(InputIterator,=20
InputIterator)</TT></TD></TR></TBODY></TABLE></DD></DL>
<P>Size methods/operators:=20
<DL>
<DD>
<TABLE cellPadding=3D3 width=3D"100%" border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TH>Method/operator</TH>
<TH>Description</TH></TR>
<TR>
<TD vAlign=3Dtop>empty()</TD>
<TD>Returns bool (true/false). True if =
empty.<BR>Declaration:=20
<TT>bool empty() const</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>size()</TD>
<TD vAlign=3Dtop>Number of elements of =
vector.<BR>Declaration:=20
<TT>size_type size() const</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>resize(n, t=3DT())</TD>
<TD vAlign=3Dtop>Adjust by adding or deleting elements of =
vector so=20
that its size is "n".<BR>Declaration: <TT>void resize(n, t =
=3D=20
T())</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>capacity()</TD>
<TD vAlign=3Dtop>Max number of elements of vector before=20
reallocation.<BR>Declaration: <TT>size_type capacity()=20
const</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>reserve(size_t n)</TD>
<TD vAlign=3Dtop>Max number of elements of vector set to "n" =
before=20
reallocation.<BR>Declaration: <TT>void =
reserve(size_t)</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>max_size()</TD>
<TD vAlign=3Dtop>Max number of elements of vector=20
possible.<BR>Declaration: <TT>size_type max_size()=20
const</TT></TD></TR></TBODY></TABLE>Note: <TT>size_type</TT> =
is an=20
unsigned integer. </DD></DL>
<P>Other methods/operators:=20
<DL>
<DD>
<TABLE cellPadding=3D3 width=3D"100%" border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TH>Method/operator</TH>
<TH>Description</TH></TR>
<TR>
<TD vAlign=3Dtop>erase()<BR>clear()</TD>
<TD vAlign=3Dtop>Erase all elements of =
vector.<BR>Declaration:=20
<TT>void clear()</TT></TD></TR>
<TR>
<TD=20
=
vAlign=3Dtop>erase(iterator)<BR>erase(begin_iterator,end_iterator)</TD>
<TD vAlign=3Dtop>Erase element of vector. Returns iterator =
to next=20
element.<BR>Erase element range of vector. Returns =
iterator to=20
next element.<BR>Declarations:=20
<UL>
<LI><TT>iterator erase(iterator pos)</TT>
<LI><TT>iterator erase(iterator first, iterator=20
last)</TT></LI></UL></TD></TR>
<TR>
<TD vAlign=3Dtop>=3D<BR>Example: X=3DY()</TD>
<TD vAlign=3Dtop>Assign/copy entire contents of one vector =
into=20
another.<BR>Declaration: <TT>vector& operator=3D(const =
vector&)</TT></TD></TR>
<TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -