?? c++ stl tutorial.mht
字號(hào):
From: <Saved by Microsoft Internet Explorer 5>
Subject: C++ STL Tutorial
Date: Wed, 4 Apr 2007 11:01:27 +0800
MIME-Version: 1.0
Content-Type: multipart/related;
type="text/html";
boundary="----=_NextPart_000_0122_01C776A8.97A450E0"
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028
This is a multi-part message in MIME format.
------=_NextPart_000_0122_01C776A8.97A450E0
Content-Type: text/html;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html
=EF=BB=BF<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>C++ STL Tutorial</TITLE><LINK=20
href=3D"http://www.yolinux.com/TUTORIALS/yolinux.css" type=3Dtext/css=20
rel=3DSTYLESHEET>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dutf-8">
<META=20
content=3D"STL,Standard Template =
Library,list,class,object,C++,Tutorial,examples,info,linux"=20
name=3DKEYWORDS>
<META=20
content=3D"Standard Template Library (STL) and C++ examples. Example of =
a doubly linked list using STL. YoLinux: Linux Information Portal =
includes informative tutorials and links to many Linux sites."=20
name=3DDESCRIPTION>
<META content=3D"Greg Ippolito" name=3Dauthor>
<META content=3D"MSHTML 6.00.2900.3059" name=3DGENERATOR></HEAD>
<BODY text=3D#000000 bgColor=3D#cccccc>
<HR SIZE=3D5>
<TABLE cellPadding=3D4 width=3D"100%">
<TBODY>
<TR>
<TD vAlign=3Dtop><A href=3D"http://www.yolinux.com/"><IMG=20
alt=3D"Yolinux.com Tutorial"=20
=
src=3D"http://www.yolinux.com/TUTORIALS/images/YoLinux_Tutorial_logo.png"=
=20
border=3Dnone></A> </TD>
<TD vAlign=3Dtop>
<H1>C++ STL (Standard Template Library) Examples and=20
Tutorial</H1></TD></TR></TBODY></TABLE>
<TABLE>
<TBODY>
<TR>
<TD width=3D"70%">The Standard Template Libraries (STL's) are a set =
of C++=20
template classes to provide common programming data structures and =
functions such as doubly linked lists (list), paired arrays (map), =
expandable arrays (vector), large string storage and manipulation =
(rope),=20
etc. The STL library is available from the <A=20
href=3D"http://www.sgi.com/tech/stl/">STL home page</A>. This is =
also your=20
best detailed reference for all of the STL class functions =
available.=20
<P>STL can be categorized into the following groupings:=20
<UL>
<LI>Container classes:=20
<UL>
<LI>Sequences:=20
<UL>
<LI><A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html#VECTOR"=
><B>vector</B></A>:=20
(this tutorial) Dynamic array of variables, struct or =
objects.=20
Insert data at the end.=20
<LI><B>deque</B>: Array which supports insertion/removal of =
elements=20
at beginning or end of array=20
<LI><A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html#LIST"><=
B>list</B></A>:=20
(this tutorial) Linked list of variables, struct or objects. =
Insert/remove anywhere. </LI></UL>
<LI>Associative Containers:=20
<UL>
<LI><B>set</B> (duplicate data not allowed in set), multiset =
(duplication allowed): Collection of ordered data in a =
balanced=20
binary tree structure. Fast search.=20
<LI><B>map</B> (unique keys), multimap (duplicate keys =
allowed):=20
Associative key-value pair held in balanced binary tree =
structure.=20
</LI></UL>
<LI>Container adapters:=20
<UL>
<LI><B>stack</B> LIFO=20
<LI><B>queue</B> FIFO=20
<LI><B>priority_queue</B> returns element with highest =
priority.=20
</LI></UL>
<LI>String:=20
<UL>
<LI><A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html=
"><B>string</B></A>:=20
Character strings and manipulation=20
<LI><B>rope</B>: String storage and manipulation </LI></UL>
<LI><B>bitset</B>: Contains a more intuitive method of storing =
and=20
manipulating bits. </LI></UL>
<LI>Operations/Utilities:=20
<UL>
<LI><B>iterator</B>: (examples in this tutorial) STL class to=20
represent position in an STL container. An iterator is =
declared to be=20
associated with a single container class type.=20
<LI><B>algorithm</B>: Routines to find, count, sort, search, =
...=20
elements in container classes=20
<LI><B>auto_ptr</B>: Class to manage memory pointers and avoid =
memory=20
leaks. </LI></UL></LI></UL></TD></TR>
<TR>
<TD vAlign=3Dbottom align=3Dright><!-- BEGIN RICH-MEDIA BURST! CODE =
-->
<SCRIPT type=3Dtext/javascript><!--=0A=
google_ad_client =3D "pub-8567479315106986";=0A=
google_ad_width =3D 728;=0A=
google_ad_height =3D 90;=0A=
google_ad_format =3D "728x90_as";=0A=
google_ad_type =3D "text_image";=0A=
google_ad_channel =3D"";=0A=
google_color_border =3D "CCCCCC";=0A=
google_color_bg =3D "FFFFFF";=0A=
google_color_link =3D "000000";=0A=
google_color_url =3D "666666";=0A=
google_color_text =3D "333333";=0A=
//--></SCRIPT>
<SCRIPT =
src=3D"http://pagead2.googlesyndication.com/pagead/show_ads.js"=20
type=3Dtext/javascript>=0A=
</SCRIPT>
<!-- END BURST CODE -->
<P></P></TD></TR></TBODY></TABLE><A name=3DVECTOR></A>
<P>
<HR SIZE=3D5>
<TABLE cellSpacing=3D1 cellPadding=3D4 width=3D"100%" bgColor=3D#000000 =
border=3D1>
<TBODY>
<TR bgColor=3D#cccccc>
<TD vAlign=3Dtop width=3D160 bgColor=3D#c0c0c0><B>Contents:</B>=20
<UL>
<LI># <A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html#VECTOR"=
>STL=20
vector</A>=20
<LI># <A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++STL.html#LIST">S=
TL=20
list</A> </LI></UL>
<P>
<HR>
<P><FONT size=3D-1><B>Related YoLinux Tutorials:</B>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++StringClass.html=
">C++=20
STL string class</A>=20
<P>=C2=B0<A =
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++.html">C++=20
Info, links</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/C++MemoryCorruptionAndMemoryLeak=
s.html">C++=20
Memory corruption, leaks</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialSoftwareDevelopment=
.html">Software=20
development tools</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialC++CodingStyle.html=
">C++=20
Coding Style</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/LinuxTutorialXemacs.html">Emacs =
and=20
C/C++</A>=20
<P>=C2=B0<A=20
=
href=3D"http://www.yolinux.com/TUTORIALS/MicrosoftVisualC++Tips.html">MS/=
Visual=20
C++ tips and best practices</A>=20
<P>=C2=B0<A =
href=3D"http://www.yolinux.com/TUTORIALS/index.html">YoLinux=20
Tutorials Index</A> </FONT>
<P>
<HR>
<P><!-- BEGIN RICH-MEDIA BURST! CODE -->
<SCRIPT language=3DJavaScript>=0A=
rnum=3DMath.round(Math.random() * 100000);=0A=
document.write('<scr'+'ipt =
src=3D"http://www.burstnet.com/cgi-bin/ads/sk9427a.cgi/v=3D2.0S/sz=3D120x=
600A|160x600A/'+rnum+'/RETURN-CODE/JS/"></scr'+'ipt>');=0A=
</SCRIPT>
<NOSCRIPT><A=20
=
href=3D"http://www.burstnet.com/ads/sk9427a-map.cgi/ns/v=3D2.0S/sz=3D120x=
600A|160x600A/"=20
target=3D_top><IMG alt=3D"Click Here"=20
=
src=3D"http://www.burstnet.com/cgi-bin/ads/sk9427a.cgi/ns/v=3D2.0S/sz=3D1=
20x600A|160x600A/"=20
border=3D0></A> </NOSCRIPT><!-- END BURST CODE -->
<P>
<HR>
<P>
<SCRIPT type=3Dtext/javascript><!--=0A=
google_ad_client =3D "pub-8567479315106986";=0A=
google_ad_width =3D 160;=0A=
google_ad_height =3D 600;=0A=
google_ad_format =3D "160x600_as";=0A=
google_ad_channel =3D"";=0A=
google_color_border =3D ["6699CC","003366","2D5893","333333"];=0A=
google_color_bg =3D ["003366","000000","99AACC","000000"];=0A=
google_color_link =3D ["FFFFFF","FFFFFF","000000","FFFFFF"];=0A=
google_color_url =3D ["AECCEB","FF6600","000099","999999"];=0A=
google_color_text =3D ["AECCEB","FF6600","003366","CCCCCC"];=0A=
//--></SCRIPT>
<SCRIPT =
src=3D"http://pagead2.googlesyndication.com/pagead/show_ads.js"=20
type=3Dtext/javascript>=0A=
</SCRIPT>
<P>
<HR>
<P><A href=3D"http://yolinux.tradepub.com/">Free Information =
Technology=20
Magazine Subscriptions and Document Downloads</A>=20
<P>
<HR>
<P><A =
href=3D"http://yolinux.tradepub.com/?pt=3Dcat&page=3DInfosoft">Free=20
Information Technology <B>Software and Development</B> Magazine=20
Subscriptions and Document Downloads</A>=20
<P>
<HR>
<P></P></TD>
<TD vAlign=3Dtop>
<TABLE cellSpacing=3D0 cellPadding=3D2 width=3D"100%" border=3D0>
<TBODY>
<TR bgColor=3D#ffcc33>
<TD><B><BIG>STL vector:</BIG></B></TD></TR></TBODY></TABLE>
<P><B>vector</B>: Dynamic array of variables, struct or objects. =
Insert=20
data at the end.=20
<P>Simple example of storing STL strings in a vector. This example =
shows=20
three methods of accessing the data within the vector:=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>
#include <string>
using namespace std;
main()
{
vector<string> SS;
SS.push_back("The number is 10");
SS.push_back("The number is 20");
SS.push_back("The number is 30");
cout << "<B>Loop by index</B>:" << endl;
int ii;
for(ii=3D0; ii < SS.size(); ii++)
{
cout << SS[ii] << endl;
}
cout << endl << "<B>Constant Iterator</B>:" << =
endl;
vector<string>::const_iterator cii;
for(cii=3DSS.begin(); cii!=3DSS.end(); cii++)
{
cout << *cii << endl;
}
cout << endl << "<B>Reverse Iterator</B>:" << endl;
vector<string>::reverse_iterator rii;
for(rii=3DSS.rbegin(); rii!=3DSS.rend(); ++rii)
{
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -