📄 c++ stl tutorial.mht
字号:
<TD vAlign=3Dtop><</TD>
<TD vAlign=3Dtop>Comparison of one vector to=20
another.<BR>Declaration: <TT>bool operator<(const =
vector&,=20
const vector&)</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>=3D=3D</TD>
<TD>Returns bool. True if every element is =
equal.<BR>Declaration:=20
<TT>bool operator=3D=3D(const vector&, const=20
vector&)</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>at(index)<BR>v[index]</TD>
<TD vAlign=3Dtop>Element of vector. Left and Right value =
assignment:=20
v.at(i)=3De; and e=3Dv.at(i);<BR>Declaration: =
<TT>reference=20
operator[](size_type n)</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>front()<BR>v[0]</TD>
<TD vAlign=3Dtop>First element of vector. (Left and Right =
value=20
assignment.)<BR>Declaration: <TT>reference =
front()</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>back()</TD>
<TD vAlign=3Dtop>Last element of vector. (Left and Right =
value=20
assignment.)<BR>Declaration: <TT>reference =
back()</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>push_back(const T& value)</TD>
<TD vAlign=3Dtop>Add element to end of =
vector.<BR>Declaration:=20
<TT>void push_back(const T&)</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>pop_back()</TD>
<TD vAlign=3Dtop>Remove element from end of =
vector.<BR>Declaration:=20
<TT>void pop_back()</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>assign(size_type n,const T& t)</TD>
<TD vAlign=3Dtop>Assign first n elements a value =
"t".</TD></TR>
<TR>
<TD vAlign=3Dtop>assign(begin_iterator,end_iterator)</TD>
<TD vAlign=3Dtop>Replace data in range defined by=20
iterators.<BR>Declaration: <TT></TT></TD></TR>
<TR>
<TD vAlign=3Dtop>insert(iterator, const T& t)</TD>
<TD vAlign=3Dtop>Insert at element "iterator", element of =
value=20
"t".<BR>Declaration: <TT>iterator insert(iterator pos, =
const=20
T& x)</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>insert(iterator pos, size_type n, const =
T&=20
x)</TD>
<TD vAlign=3Dtop>Starting before element "pos", insert first =
n=20
elements of value "x".<BR>Declaration: <TT>void =
insert(iterator=20
pos, size_type n, const T& x)</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>insert(iterator pos,=20
begin_iterator,end_iterator)</TD>
<TD vAlign=3Dtop>Starting before element "pos", insert range =
begin_iterator to end_iterator.<BR>Declaration: <TT>void=20
insert(iterator pos, InputIterator f, InputIterator =
l)</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>swap(vector& v2)</TD>
<TD vAlign=3Dtop>Swap contents of two =
vectors.<BR>Declaration:=20
<TT>void=20
swap(vector&)</TT></TD></TR></TBODY></TABLE></DD></DL>Iterator =
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>begin()</TD>
<TD vAlign=3Dtop>Return iterator to first element of=20
vector.<BR>Declaration: <TT>const_iterator begin()=20
const</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>end()</TD>
<TD vAlign=3Dtop>Return iterator to last element of=20
vector.<BR>Declaration: <TT>const_iterator end() =
const</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>rbegin()</TD>
<TD vAlign=3Dtop>Return iterator to first element of vector =
(reverse=20
order).<BR>Declaration: <TT>const_reverse_iterator =
rbegin()=20
const</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>rend()</TD>
<TD vAlign=3Dtop>Return iterator to last element of vector =
(reverse=20
order).<BR>Declaration: <TT>const_reverse_iterator rend()=20
const</TT></TD></TR>
<TR>
<TD vAlign=3Dtop>++</TD>
<TD vAlign=3Dtop>Increment iterator.</TD></TR>
<TR>
<TD vAlign=3Dtop>--</TD>
<TD vAlign=3Dtop>Decrement =
iterator.</TD></TR></TBODY></TABLE></DD></DL>
<P>
<HR>
<B>Vector Links:</B>=20
<UL>
<LI><A href=3D"http://www.sgi.com/tech/stl/Vector.html">SGI: =
vector</A> -=20
Detail of all vector member functions and operators available. =
</LI></UL>
<P><A name=3DLIST></A>
<HR>
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>STL list:</BIG></B></TD></TR></TBODY></TABLE>
<P><B>list</B>: Linked list of variables, struct or objects. =
Insert/remove=20
anywhere.=20
<P>Two examples are given:=20
<OL>
<LI>The first STL example is for data type <B>int</B>=20
<LI>The second for a list of <B>class</B> instances. =
</LI></OL>They are=20
used to show a simple example and a more complex real world =
application.=20
<P>1. Lets start with a simple example of a program using STL for =
a linked=20
list:=20
<P>
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE>
// Standard Template Library example
#include <iostream>
#include <list>
using namespace std;
// Simple example uses type int
main()
{
list<int> L;
L.push_back(0); // Insert a new element at the end
L.push_front(0); // Insert a new element at the beginning
L.insert(++L.begin(),2); // Insert "2" before position of first =
argument
// (Place before second argument)
L.push_back(5);
L.push_back(6);
list<int>::iterator i;
for(i=3DL.begin(); i !=3D L.end(); ++i) cout << *i << " =
";
cout << endl;
return 0;
}
</PRE></TD></TR></TBODY></TABLE></DD></DL>Compile: <TT>g++=20
example1.cpp</TT>=20
<P>Run: <TT>./a.out</TT>=20
<P>Output: <TT>0 2 0 5 6</TT>=20
<P><FONT color=3D#ff0000>[Potential Pitfall]</FONT>: In Red Hat =
Linux=20
versions 7.x one could omit the "<TT>using namespace std;</TT>" =
statement.=20
Use of this statement is good programming practice and is required =
in Red=20
Hat 8.0.=20
<P><FONT color=3D#ff0000>[Potential Pitfall]</FONT>: Red Hat 8.0 =
requires=20
the reference to "<TT>#include <iostream></TT>". Red Hat =
versions=20
7.x used "<TT>#include <iostream.h></TT>".=20
<P>
<HR>
<P>2. The STL tutorials and texts seem to give simple examples =
which do=20
not apply to the real world. The following example is for a doubly =
linked=20
list. Since we are using a class and we are not using defined =
built-in C++=20
types we have included the following:=20
<UL>
<LI>To make this example more complete, a copy constructor has =
been=20
included although the compiler will generate a member-wise one=20
automatically if needed. This has the same functionality as the=20
assignment operator (=3D).=20
<LI>The assignment (=3D) operator must be specified so that sort =
routines=20
can assign a new order to the members of the list.=20
<LI>The "less than" (<) operator must be specified so that =
sort=20
routines can determine if one class instance is "less than" =
another.=20
<LI>The "equals to" (=3D=3D) operator must be specified so that =
sort=20
routines can determine if one class instance is "equals to" =
another.=20
</LI></UL>
<P>
<DL>
<DD>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" =
bgColor=3D#000000=20
border=3D1>
<TBODY>
<TR bgColor=3D#c0c0c0>
<TD><PRE>
// Standard Template Library example using a class.
#include <iostream>
#include <list>
using namespace std;
// The List STL template requires overloading operators =3D, =3D=3D and =
<.
class AAA
{
friend ostream &operator<<(ostream &, const AAA &);
public:
int x;
int y;
float z;
AAA();
AAA(const AAA &);
~AAA(){};
AAA &operator=3D(const AAA &rhs);
int operator=3D=3D(const AAA &rhs) const;
int operator<(const AAA &rhs) const;
};
AAA::AAA() // Constructor
{
x =3D 0;
y =3D 0;
z =3D 0;
}
AAA::AAA(const AAA &copyin) // Copy constructor to handle pass by =
value.
{ =20
x =3D copyin.x;
y =3D copyin.y;
z =3D copyin.z;
}
ostream &operator<<(ostream &output, const AAA &aaa)
{
output << aaa.x << ' ' << aaa.y << ' ' =
<< aaa.z << endl;
return output;
}
AAA& AAA::operator=3D(const AAA &rhs)
{
this->x =3D rhs.x;
this->y =3D rhs.y;
this->z =3D rhs.z;
return *this;
}
int AAA::operator=3D=3D(const AAA &rhs) const
{
if( this->x !=3D rhs.x) return 0;
if( this->y !=3D rhs.y) return 0;
if( this->z !=3D rhs.z) return 0;
return 1;
}
// This function is required for built-in STL list functions like sort
int AAA::operator<(const AAA &rhs) const
{
if( this->x =3D=3D rhs.x && this->y =3D=3D rhs.y =
&& this->z < rhs.z) return 1;
if( this->x =3D=3D rhs.x && this->y < rhs.y) return =
1;
if( this->x < rhs.x ) return 1;
return 0;
}
main()
{
list<AAA> L;
AAA Ablob ;
Ablob.x=3D7;
Ablob.y=3D2;
Ablob.z=3D4.2355;
L.push_back(Ablob); // Insert a new element at the end
Ablob.x=3D5;
L.push_back(Ablob); // Object passed by value. Uses default =
member-wise
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -