设为首页 加入收藏

TOP

C++STL笔记(二)
2017-10-13 10:21:46 】 浏览:6968
Tags:STL 笔记
()

1.删除单个元素

str.erase(it) //it为所要删除元素位置的迭代器

2.删除一个区间内的所有元素

(1)str.erase(first,last); [first,last)左开右闭

string str=”abcdefg”;

str.erase(str.begin()+2,str.end()-1);

str:abg

(2)str.erase(pos,length);

pos为起始位置,删除后面长度为length的字符串。

string str=”abcdefg”;

str.erase(3,2);

str:abcfg

6)clear()

str.clear(); 清空字符串

7)substr()

substr(pos,len); 返回从pos号位开始、长度为len的子串。

string str=”Thank you for your smile”;

str.substr(0,5);

Thank

str.substr(14,4);

your

str.substr(19,5);

smile

8)string::npos

 string::npos是一个常数,其本身值为-1,unsigned_int类型,作为find函数失配时的返回值。

9)find()

str1.find(str2); 当str2是str1的子串时,返回str2在str1中第一次出现的位置;如果str2不是str1的子串,返回string::npos。

str1.find(str2,pos); 从str1的pos号位开始匹配str2,返回值与上文相同。

string str1=”Thank you for your smile”;

string str2=”you”;

string str3=”me”;

cout<<str1.find(str2);

6

cout<<str1.find(str2,7);

14

10)replace()

1.str1.replace(pos,len,str2);  把str1从pos号位开始、长度为len的子串替换为str2.

2.str1.replace(it1,it2,str2);  把str1的迭代器[it1,it2)范围的子串替换成str2.

 

string str1=”Maybe you will turn around”;

string str2=”will not”;

striing str3=”surely”;

 

cout<<str1.replace(10,4,str2);

Maybe you will not turn around

cout<<str1.replace(str1.begin(),str1.begin()+5,str3);

surely you will not turn around

 

4.map

映射,map可以将任何基本类型(包括STL容器)映射到任何基本类型(包括STL容器)。

例如:

字符串-->页码。

头文件

#include<map>

 

4.1 map的定义

map<typename1,typename2> mp;

map<key,value> mp;

map<int,int> mp 相当于普通的int型数组。

 

如果是字符串到整型的映射,必须用string,不能用char数组。

map<string,int>  mp;

map的键和值也可以是STL容器。

例如:

map<set<int>,string> mp;

 

4.2 map容器内元素的访问

1.通过下标访问

与访问普通数组一样,例如

map<char,int> mp;

mp[‘c’]来访问它对应的整数。

mp[‘c’]=20;

注:map的键值是唯一的。

2.通过迭代器访问

map<typename1,typename2>::iterator it;

it->first访问键

it->second访问值

map<char,int>mp;

mp[‘m’]=20;

mp[‘r’]=30;

mp[‘a’]=40;

 

for(auto it=mp.begin();it!=mp.end();it++)

printf(“%c %d\n”,it->first,it->second);

 

map会按键值从小到大自动排序。

 

4.3 map常用函数

1)find()

find(key)返回键值为key的映射的迭代器。

auto it = mp.find(‘c’);

2)erase()

1.删除单个元素。

mp.erase(it);  it为迭代器。

mp.erase(key); key为欲删除的映射的键。

2.删除一个区间内的所有元素。

map.erase(first,last)  [first,last) 删除迭代器区间,左闭右开。

3)size()

返回map中映射的对数。

4)clear()

清空map中所有的元素。

 

5.queue

队列,一个先进先出的容器。

头文件

#include<queue>

5.1 queue的定义

queue<typename> name;

 

5.2 queue 容器内元素的访问

queue是一种先进先出的限制访问的数据结构。

只能front()访问队首元素,back()访问队尾元素。

queue<int> q;

q.front();

q.back();

 

5.3 queue 常用函数

1)push()

q.push(x)将x进行入队

2)front() / back()

访问队首和队尾元素。

3)pop()

q.pop()令队首元素出队。

 

4)empty()

检测queue是否为空,返回true为空,返回false则非空。

5)size()

返回queue内元素的个数。

 

 

5.4 queue常见用途

例如广度优先搜索。

 

6.priority_queue

优先队列,底层用堆来实现。在优先队列中,队首元素是当前队列中优先级最高的一个。

 

头文件

#include<queue>

 

6.1 priority_queue的定义

priority_queue<typename> name;

 

6.2 priority_queue 容器内元素的访问

只能用q.top()函数访问。

 

6.3 priority_queue 常用函数

1)push()

q.push(x)将x入队。

2)top()

q.top()可以获得队首元素。

3)pop()

令队首元素(堆顶元素)出队。

q.pop();

4)empty()

检测优先队列是否为空,为空则返回true,返回false为空。

5)size()

返回优先队列内元素的个数。

&n

首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Dom4J生成xml和包含CDATA问题 下一篇读书笔记 effective c++ Item 14 ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目