1027STL学习2Word格式.docx
- 文档编号:16380510
- 上传时间:2022-11-23
- 格式:DOCX
- 页数:42
- 大小:243.99KB
1027STL学习2Word格式.docx
《1027STL学习2Word格式.docx》由会员分享,可在线阅读,更多相关《1027STL学习2Word格式.docx(42页珍藏版)》请在冰豆网上搜索。
I'
cpch['
7'
]=='
\0'
把string拷贝到char*指向的内存空间的操作
chare[8]={0};
//申请空间
intiCount=strA.copy(e,7,0);
//iCount==7
string的赋值
stringstrC;
strC=strA;
strC.assign("
UIP"
strC.assign(strA);
strC.assign(5,'
strC.assign(strA,2,3);
string的连接
stringstrD("
Hello"
strD+=strA;
//此时strA内容为"
HelloUIPower"
strD+="
strD.append("
3);
strD.append(strA);
strD.append(strA,0,3);
strD.append(5,'
string的子串
stringstrE=strA.substr(2,3);
//此时strE内容为"
Pow"
string的查找
intiPos=strA.find('
P'
0);
//此时iPos为2,函数的第二个参数可不写
iPos=strA.find("
owe"
//此时iPos为3,函数的第二个参数可不写
stringstrF="
IP"
iPos=strA.find(strF,0);
//此时iPos为1,函数的第二个参数可不写
iPos=strA.rfind('
//此时iPos为2,函数的第二个参数可不写
iPos=strA.rfind("
//此时iPos为3,函数的第二个参数可不写
iPos=strA.rfind(strE,0);
//此时iPos为1,函数的第二个参数可不写
string的替换
stringstrF("
abcdefg"
strF.replace(1,3,"
xyzxyz"
//此时strF的内容为"
axyzxyzefg"
编码统一化,编写单一源代码
tchar*pt=_T("
tstringtstr(pt);
tstringtstrSub=tstr.substr(2,3);
讲解要点
一、函数模板与类模板的用法;
二、容器的分类,各个容器的数据结构;
三、容器vector的具体用法(包括迭代器的具体用法)。
函数模板的简介
intmax(inta,intb)
returna>
b?
a:
b;
charmax(chara,charb)
returna>
floatmax(floata,floatb)
template<
typenameT>
//或者template<
classT>
Tmax(Ta,Tb)
{
returna>
b?
a:
b;
voidmain()
intiMax=max(3,5);
//调用intmax(inta,intb);
charchMax=max('
w'
'
d'
//调用charmax(chara,charb);
floatfMax=max(2.7f,1.3f);
//调用floatmax(floata,floatb);
类模板的简介
classCMax
public:
CMax(inta,intb)
{
m_a=a;
m_b=b;
}
intGetMax()
returnm_a>
m_b?
m_a:
m_b;
private:
intm_a;
intm_b;
CMax(Ta,Tb)
TGetMax()
Tm_a;
Tm_b;
};
CMax<
int>
maxInt(3,5);
//需要指定类型
intiMax=maxInt.GetMax();
char>
maxChar('
charchMax=maxChar.GetMax();
float>
maxFloat(2.7f,1.3f);
floatfMax=maxFloat.GetMax();
****************************************
可以定义多种类型的形参。
typenameT1,typenameT2>
classCTest
{...};
对象实例化时:
CTesttestA<
int,float>
CTesttestB<
double,string>
容器的简介
各数据结构在查找,插入,删除时的效率。
容器的分类
序列式容器:
vector,deque,list
关联式容器:
set,multiset,map,multimap
vector对象的默认构造
vector<
T>
vecT;
vecInt;
//一个存放int的vector容器。
vecFloat;
//一个存放float的vector容器。
string>
vecString;
//一个存放string的vector容器。
...//尖括号内还可以设置指针类型或自定义类型。
ClassCA{};
CA*>
vecpCA;
//用于存放CA对象的指针的vector容器。
CA>
vecCA;
//用于存放CA对象的vector容器。
由于容器元素的存放是按值复制的方式进行的,所以此时CA必须提供CA的拷贝构造函数,以保证CA对象间拷贝正常。
vector末尾的添加移除操作
vecInt.push_back
(1);
vecInt.push_back(3);
vecInt.push_back(5);
vecInt.push_back(7);
vecInt.push_back(9);
vecInt.pop_back();
//{5,7,9}
vector的数据存取
//假设包含1,3,5,7,9
vecInt.at
(2)==vecInt[2];
//5
vecInt.at
(2)=8;
或vecInt[2]=8;
vecInt就包含1,3,8,7,9值
intiF=vector.front();
//iF==1
intiB=vector.back();
//iB==9
vector.front()=11;
//vecInt包含{11,3,8,7,9}
vector.back()=19;
//vecInt包含{11,3,8,7,19}
双向迭代器与随机访问迭代器
双向迭代器支持的操作:
it++,++it,it--,--it,*it,itA=itB,
itA==itB,itA!
=itB
其中list,set,multiset,map,multimap支持双向迭代器。
随机访问迭代器支持的操作:
在双向迭代器的操作基础上添加
it+=i,it-=i,it+i(或it=it+i),it[i],
itA<
itB,itA<
=itB,itA>
itB,itA>
=itB的功能。
其中vector,deque支持随机访问迭代器。
vector与迭代器的配合使用
//假设包含1,3,5,7,9元素
:
iteratorit;
//声明容器vector<
的迭代器。
it=vecInt.begin();
//*it==1
++it;
//或者it++;
*it==3,前++的效率比后++的效率高,前++返回引用,后++返回值。
it+=2;
//*it==7
it=it+1;
//*it==9
//it==vecInt.end();
此时不能再执行*it,会出错!
正向遍历:
for(vector<
iteratorit=vecInt.begin();
it!
=vecInt.end();
++it)
intiItem=*it;
cout<
<
iItem;
//或直接使用cout<
*it;
这样子便打印出13579
逆向遍历:
reverse_iteratorrit=vecInt.rbegin();
rit!
=vecInt.rend();
++rit)//注意,小括号内仍是++rit
intiItem=*rit;
//或直接使用cout<
*rit;
此时将打印出9,7,5,3,1
注意,这里迭代器的声明采用vector<
reverse_iterator,而非vector<
iterator。
迭代器还有其它两种声明方法:
const_iterator与vector<
const_reverse_iterator
以上两种分别是vector<
iterator与vector<
reverse_iterator的只读形式,使用这两种迭代器时,不会修改到容器中的值。
备注:
不过容器中的insert和erase方法仅接受这四种类型中的iterator,其它三种不支持。
《EffectiveSTL》建议我们尽量使用iterator取代const_iterator、reverse_iterator和const_reverse_iterator。
vector对象的带参数构造
intiArray[]={0,1,2,3,4};
vecIntA(iArray,iArray+5);
vecIntB(vecIntA.begin(),vecIntA.end());
//用构造函数初始化容器vecIntB
vecIntC(3,9);
//此代码运行后,容器vecIntB就存放3个元素,每个元素的值是9。
vecIntD(vecIntA);
vector的赋值
vecIntA,vecIntB,vecIntC,vecIntD;
vecIntA.assign(iArray,iArray+5);
vecIntB.assign(vecIntA.begin(),vecIntA.end());
//用其它容器的迭代器作参数。
vecIntC.assign(3,9);
vecIntD;
vecIntD=vecIntA;
vecIntA.swap(vecIntD);
vector的大小
例如vecInt是vector<
声明的容器,现已包含1,2,3元素。
intiSize=vecInt.size();
//iSize==3;
boolbEmpty=vecInt.empty();
//bEmpty==false;
执行vecInt.resize(5);
//此时里面包含1,2,3,0,0元素。
再执行vecInt.resize(8,3);
//此时里面包含1,2,3,0,0,3,3,3元素。
再执行vecInt.resize
(2);
//此时里面包含1,2元素。
vector的插入
vecA;
vector<
vecB;
vecA.push_back
(1);
vecA.push_back(3);
vecA.push_back(5);
vecA.push_back(7);
vecA.push_back(9);
vecB.push_back
(2);
vecB.push_back(4);
vecB.push_back(6);
vecB.push_back(8);
vecA.insert(vecA.begin(),11);
//{11,1,3,5,7,9}
vecA.insert(vecA.begin()+1,2,33);
//{11,33,33,1,3,5,7,9}
vecA.insert(vecA.begin(),vecB.begin(),vecB.end());
//{2,4,6,8,11,33,33,1,3,5,7,9}
vector的删除
删除区间内的元素
vecInt是用vector<
声明的容器,现已包含按顺序的1,3,5,6,9元素。
iteratoritBegin=vecInt.begin()+1;
iteratoritEnd=vecInt.begin()+3;
vecInt.erase(itBegin,itEnd);
//此时容器vecInt包含按顺序的1,6,9三个元素。
假设vecInt包含1,3,2,3,3,3,4,3,5,3,删除容器中等于3的元素
iteratorit=vecInt.being();
)//小括号里不需写++it
if(*it==3)
{
it=vecInt.erase(it);
//以迭代器为参数,删除元素3,并把数据删除后的下一个元素位置返回给迭代器。
//此时,不执行++it;
}
else
++it;
//删除vecInt的所有元素
vecInt.clear();
//容器为空
一、容器deque的使用方法;
二、容器queue,stack的使用方法;
三、容器list的使用方法。
deque对象的默认构造
deque<
deqInt;
//一个存放int的deque容器。
deqFloat;
//一个存放float的deque容器。
deqString;
//一个存放string的deque容器。
...
//尖括号内还可以设置指针类型或自定义类型。
deque末尾的添加移除操作
deque<
deqInt.push_back
(1);
deqInt.push_back(3);
deqInt.push_back(5);
deqInt.push_back(7);
deqInt.push_back(9);
deqInt.pop_front();
deqInt.push_front(11);
deqInt.push_front(13);
deqInt.pop_back();
//deqInt{13,11,5}
deque的数据存取
deque<
deqInt.push_back
(1);
deqInt.push_back(3);
deqInt.push_back(5);
deqInt.push_back(7);
deqInt.push_back(9);
intiA=deqInt.at(0);
//1
intiB=deqInt[1];
//3
deqInt.at(0)=99;
//99
deqInt[1]=88;
//88
intiFront=deqInt.front();
//99
intiBack=deqInt.back();
//9
deqInt.front()=77;
//77
deqInt.back()=66;
//66
deque与迭代器
for(deque<
iteratorit=deqInt.begin();
=deqInt.end();
cout<
"
"
//13579
reverse_iteratorrit=deqInt.rbegin();
=deqInt.rend();
++rit)
//97531
deque对象的带参数构造
deqIntA;
deqIntA.push_back
(1);
deqIntA.push_back(3);
deqIntA.push_back(5);
deqIntA.push_back(7);
deqIntA.push_back(9);
deqIntB(deqIntA.begin(),deqIntA.end());
//13579
deqIntC(5,8);
//88888
deqIntD(deqIntA);
//13579
deque的赋值
deqIntA,deqIntB,deqIntC,deqIntD;
deqIntB.assign(deqIntA.begin(),deqIntA.end());
deqIntC.assign(5,8);
//88888
deqIntD=deqIntA;
//13579
deqIntC.swap(deqIntD);
//互换
deque的大小
intiSize=deqIntA.size();
//3
if(!
deqIntA.empty())
deqIntA.resize(5);
//13500
deqIntA.resize(7,1);
//1350011
deqIntA.resize
(2);
//13
deque的插入
deqA;
deque<
deqB;
deqA.push_back
(1);
deqA.push_back(3);
deqA.push_back(5);
deqA.push_back(7);
deqA.push_back(9);
deqB.push_back
(2);
deqB.push_back(4);
deqB.push_back(6);
deqB.push_back(8);
deqA.insert(deqA.begin(),11);
deqA.insert(deqA.begin()+1,2,33);
deqA.insert(deqA.begin(),deqB.begin(),deqB.end());
//{
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 1027 STL 学习