码迷,mamicode.com
首页 > 其他好文 > 详细

STL初学

时间:2018-01-24 00:50:21      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:大小   body   ali   数据结构   stack   amp   ==   order   哈希表   

标准模板库STL初学

线性数据结构

vector

一维向量,相当于数组

list

链表

map

映射,提供(Key,Value)式操作,相当于哈希表

string

char字符串

queue

队列,先入先出的线性表

stack

栈,先入后出的线性表

set

集合

deque

双向链表

 

一、vector:对数组的封装

push_back

在尾部添加一个元素

pop_back

从尾部删除一个元素(capacity不变)

clear

清空所有元素(capacity不变)

at

按索引访问某个位置的元素(操作符[]也被重载,可以用)

front / back

返回头/尾元素的引用

size

返回元素个数

capacity

返回当前容量

resize

改变容量大小

insert

在中间插入元素(与普通数组一样,涉及移动,效率低)

erase

删除中间的元素(与普通数组一样,涉及移动,效率低)

在能确定最大容量时,应直接指定,避免反复申请缓冲区和复制大段数据。即创建时vector<int> arr(128)

迭代器iterator是vector的内部类,用于对vector内部的元素遍历。

vector<int>::iterator iter;
常用于for(iter = arr.begin(); iter != arr.end(); iter++)

其中,*和->操作符被重载,可以直接使用。

 

二、list:对链表的封装

push_back

在尾部添加一个元素

pop_back

从尾部删除一个元素

push_front

在首部添加一个元素

pop_front

从首部删除一个元素

clear

清空所有元素

front / back

返回头/尾元素的引用

size

返回元素个数

insert

在中间插入元素(需要用迭代器遍历找到插入点)

erase

删除中间的元素(需要用迭代器遍历找到删除点)

只进行push_back和pop_front,就相当于队列;只进行push_back和pop_back,就相当于栈。

 

三、string:对字符串的封装

内部仍然是维护一个char型数组,并且也是以0结尾。重载了许多操作符。

append

附加字符串

clear

清空

capacity

容量

size / length

两者相同,都返回实际长度

at

按索引访问字符(与重载操作符[]等效)

find

查找一个字符或一个子串

rfind

从后往前查找一个字符或一个子串

find_first_of

查找匹配若干字符串中的一个字符

find_first_not_of

查找不匹配若干字符串中的一个字符

find_last_of

从后往前查找,匹配若干字符串中的一个字符

find_last_not_of

从后往前查找,不匹配若干字符串中的一个字符

substr

取得一个子串

insert

插入字符或子串

replace

替换字符或子串

构造对象:

string str("abc");
string str = "abc";
string str("abcde", 5);
string str;
string str = "";

可以用c_str函数来获取string内部的字符串指针:const char *str = str1.c_str();

传参时,使用const string& 或 string&(传引用),一般不使用string(传值)

 

附加字符串:(append函数和操作符)

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     string str;
 6     str.append("abc"); //附加字符串
 7     str.append("abcde",3); //附加字符串前3个字符
 8     str.append("abcde",1,3); //附加1-3个字符
 9     str.append(2,h); //附加2个h
10     cout << str << endl; //abcabcbcdhh
11     return 0;
12 }

重载操作符 +=,+:如str += "abc";

和vector一样,当附加字符串时有可能会自动扩充缓冲区,并且需要原有数据。

 

字符串比较(重载了关系操作符):

string类重载了所有关系操作符(==, !=, >, >=, <, <=)。其概念与strcmp返回值相同。

 

字符串查找:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     string str = "LiMing is doing homework";
 6     int pos1 = str.find(i); //返回1
 7     int pos2 = str.find(i,2); //从位置2开始找,返回3
 8     int pos3 = str.find("ing"); //返回3
 9     int pos4 = str.find("ing",pos3+3); //以po3+3=6开始往后找
10     int pos5 = str.rfind("ing"); //从右向左 返回12
11     int pos6 = str.rfind("ing",pos5-3); //以pos5-3=9从右向左开始找,返回3
12     int pos7 = str.find_first_of("aeiouAEIOU"); //返回第一个元音字母的位置 1
13     int pos8 = str.find_first_not_of("aeiouAEIOU"); //返回第一个不是元音字母的位置 0
14     int pos9 = str.find_last_of("aeiouAEIOU"); //返回从右向左找第一个是元音字母的位置 21
15     int pos0 = str.find_last_not_of("aeiouAEIOU"); //返回从右向左找第一个不是元音字母的位置 23
16     return 0;
17 }

 

复制子串(substr):

string str = "abcdefg";
string str1 = str.substr(4) //efg
string str2 = str.substr(4,2); //ef

 

插入和替换(低效):

// insert 的使用
string& insert(int pos, const char *str); //在pos处插入str
string& insert(int pos, const char *str, int count); //在pos处插入str[0,count-1]
string& insert(int pos, const char *str, int offset, int count); //在pos处插入str[offset,offset+count-1]
string& insert(int pos, int count, char ch); //插入若干字符

// replace的使用,原串和用于替换的串的长度无关
string& replace(int pos, int num, const char *dest); //替换原字符串[pos,pos+num-1]
string& replace(int pos, int num, const char *dest, int count); //以dest[0,count-1]替换原字符串[pos,pos+num-1]
string& replace(int pos, int num, const char *dest, int offset, int count); //以dest[offset,offset+count-1]替换原字符串[pos,pos+num-1]
string& replace(int pos, int num, int count, char ch); //以count个ch替换原字符串[pos,pos+num-1]

 

STL初学

标签:大小   body   ali   数据结构   stack   amp   ==   order   哈希表   

原文地址:https://www.cnblogs.com/sandychn/p/8338301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!