STL(vector, set, map, string)的用法总结1

因为STL在C++中极为重要,所以在这里做出小小的总结

如果想要偷懒少些头文件的话,可以用
#include <bits/stdc++.h> 头文件来导入所有

四种常用的容器是vector,set,map,string,头文件同名,定义方法例子为:
vector < int > example 访问方式与数组一样,或者利用迭代器访问
set < int > example 利用迭代器访问
map < int, int > example 可以当成一个超级数组来访
string example 相当于一种数据类型
类型根据具体的问题而定,可以是任意的类型,注意,当类型也是某一种容器的时候,>>不要连起来写,中间需要隔一个空格

注:利用迭代器的访问方法放在了文章的最后(需要指出的是在常见的STL当中,只有vector和string支持用迭代器加上整数的做法)

vector容器(数组或者迭代器访问)

注:定义二维数组:vector< int > array[100]

常用函数

example.begin():返回指向第一个元素的迭代器
example.end():返回指向最后一个元素的迭代器

example.push_back(a):向数组的尾部添加元素
example.pop_back():将尾部的元素删除
example.insert(example.begin()+i, a):使插入的元素a变成第i+1个元素(数组下标对应的是i)
example.erase(example.begin()+i):删除第i+1个元素(数组下标对应的是i)
example.erase(example.begin()+i,example.end()+j):删除区间[i,j)
example.size():取大小
example.clear():清除

相关应用

常常与< algorithm >中的sort函数和reverse函数联立使用
eg:
reverse(example.begin(),example.end())
sort(example.begin(),example.end())//默认是升序排列
sort(example.begin(),example.end(),Compare)//按照自己定义的比较函数进行排列
注:比较函数定义举例:
bool Compare(const int& a,const int& b)
{
return a>b;
}
当然,比较常用的排序肯定是从小到大或者从大到小啦,这两个比较函数都有前人帮我们实现了
前者可以使用less< int > ()后者可以使用greater< int > ()

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	vector<int> ans;
	ans.push_back(1);
	ans.push_back(2);
	ans.push_back(3);
	sort(ans.begin(),ans.end(),greater<int>());
	for(vector<int>::iterator it=ans.begin();it!=ans.end();it++)
	{
		cout<<*it<<" ";
	}
}
set容器(迭代器访问)

不支持*(迭代器+整数)的访问方式,只能循环枚举出所有的值
set的特性:(1)不会拥有相同的元素。(2)所有的元素会根据元素的键值进行排列(默认升序)

常用函数

example.insert(a):向集合中插入元素
example.count(a):返回a元素的个数,一般返回值0/1。
example.find(a):返回指向a元素的迭代器。找不到就返回end()
example.empty():如果集合为空,就返回true
example.erase(a):直接删除掉a(参数还可以是一个迭代器或者是一个迭代器表示的区间)----三种消除方式
example.size():返回大小
example.clear():清除
example.swap(example1):注意交换的是集合变量

注意,元素的iterator会随着insert而发生改变,在使用的时候注意更新
map容器
特性:插入方法的多样性

两种方法:
1.(常用)当成超级数组
example[0]=“a”
2. 利用insert函数
example.insert(pair<int,string> (0,“a”))
区别:对于一样的键值对,数组法是可以覆盖的,但是insert方法是无法插入数据的,为了知道使用insert有没有插入成功可以通过用以下的代码进行判断

// 构造定义,返回一个pair对象
pair<iterator,bool> insert (const value_type& val);
 
pair<map<int, string>::iterator, bool> Insert_Pair;
 
Insert_Pair = mapStudent.insert(map<int, string>::value_type (001, "student_one"));
 
if(!Insert_Pair.second)
    cout << ""Error insert new element" << endl;

利用pair的第二个布尔值,如果是false,那么就是失败了

查找

直接上代码

#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

int main()
{
	map<int,string> ans;
	map<int,string>::iterator iter;
	ans[0]="abc";
	ans[1]="bcd";
	ans[2]="cde";
	iter=ans.find(1);//这里如果用value会报错 
	cout<<iter->second<<endl;
	cout<<iter->first<<endl; 
} 
删除

直接上代码

#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

int main()
{
	map<int,string> ans;
	map<int,string>::iterator iter;
	ans[0]="abc";
	ans[1]="bcd";
	ans[2]="cde";
	ans.erase(0);
	ans.erase("bcd");//这里使用了value来删除,所以报错了 
	cout<<ans[0]<<endl;
	cout<<ans[1]<<endl;
	cout<<ans[2]<<endl;
} 
常用函数(与上面两个差不多)

example.size():大小
example.count(a):返回指定元素出现次数(参数为key)
example.begin()
example.end()
example.empty()
example.clear()
example.swap(example1): 两个map的交换

sting 容器

特性:拥有一些被重载的运算符
(1) =,相当于assign() //赋以新值
(2) +=,append(),push_back() //在尾部添加字符
(3) + //串联字符串
(4) ==,!=,<,<=,>,>=,compare() //比较字符串
(5) >>,getline() //从stream读取某值
(6) << //将谋值写入stream
(7) [ ], at() //存取单一字符

常用函数

begin() end(): 返回的是迭代器
rbegin() rend():返回的是逆向迭代器(附录2)
clear():清除
size():返回字符数量
empty():判断是否为空
swap():交换两个字符串的内容
max_size():返回字符的可能最大个数
capacity():返回重新分配之前的字符容量
string::npos:其本身的值是-1,但是由于是unsigned int类型,所以也相当于是最大值。用途是作为find函数返回失败时的返回值

find():在父字符串中查找子字符串主要有两种使用方法
找到了的话,返回的是数组下标

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string str="Thank you for your smile.";
	string str2="you";
	string str3="me";
	if(str.find(str2)!=string::npos)
	{
		cout<<str.find(str2)<<endl;
	}
	if(str.find(str2,7)!=string::npos)
	{
		cout<<str.find(str2,7)<<endl;
	}
	if(str.find(str3)!=string::npos)
	{
		cout<<str.find(str3)<<endl;
	}
	else
	{
		cout<<"I don't find the word"<<endl;
	}
}
//运行结果 
//6
//14
//I don't find the word

insert():插入字符(方法多样化)

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string A="aaaaaa",str="hello";
	A.insert(0,2,'b');//bbaaaaaa,在下标为零的位置插入两个b 
	A="aaaaaa";
	A.insert(1,"bb");//abbaaaaa,在下标为一的位置插入字符串 
	A="aaaaaa";
	A.insert(1,"bbb",2);//abbaaaaa,在下标为一的位置插入字符串的前两个字符 
	A="aaaaaa";
	A.insert(1,str);//ahelloaaaaa,在下标为一的位置插入string类型 
	A="aaaaaa";
	A.insert(6,str,0,3);//aaaaaahel,在下标为一的位置插入string类型变量的从0开始的三个字符 
	A="aaaaaa";
	A.insert(6,str,1,string::npos);//aaaaaaello, 在下标为一的位置插入string类型变量1后面的全部字符 
	A="aaaaaa";
	A.insert(A.begin(),'b');//baaaaaa,在迭代器指定的位置插入字符 
	A="aaaaaa";
	A.insert(A.begin(),2,'b');//bbaaaaaa,在迭代器指定的位置插入两个同样的字符 
}

erase():删除字符(三种方式,迭代器,迭代器区间,坐标索引)

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string A="abcdabcdabcd";
	A.erase(A.begin());//删除a,即迭代器处的字符 
	A.erase(A.begin(),A.begin()+1);//删除b,即迭代器区间的字符,前闭后开 
	A.erase(0,1);//删除c,即从坐标零的元素开始的一个字符,就是A[0] 
	A.erase(0,5);//删除dabcd,即从坐标零的元素开始的五个字符 
	cout<<A<<endl;
}

replace():替换字符(常用的为三种)

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string A="aaaaaa";
	A.replace(2,1,"b");//aabaaa,将从下标为2开始的含有一个字符的字符串替换成指定字符串 
	A="aaaaaa";
	A.replace(2,1,3,'c');//aacccaaa,将从下标为2开始的一个字符替换成 3个指定字符 
	A="aaaaaa";
	A.replace(A.begin(),A.end(),"bbb");//将迭代器指定的区间替换成指定的字符串(前开后闭) 
	cout<<A; 
}

substr():返回某个子字符串

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string A="aaaaaa";
	A=A.substr(1);//aaaaa,从1开始到结束 
	A="aaaaaa";
	A=A.substr(1,2);//aa,从1开始的两个字符 
	cout<<A<<endl;
}
关于函数部分,先大致到这里为止,今后如果用上了其余的再补充

附录1:利用一个删除数组重复项的函数来举例

int removeDuplicates(vector<int>& nums) {

        set<int> ans;
        for(int i=0;i<nums.size();i++)
        {
            ans.insert(nums[i]);
        }
        set<int>::iterator it;
        nums.clear();
        for(it=ans.begin();it!=ans.end();it++)//从begin()开始,不到end()不结束
        {
            nums.push_back(*it);
        }
        return nums.size();

附录2:逆向迭代器
反向迭代器(Reverse Iterator)是普通迭代器的适配器,通过重新定义自增和自减操作,以达到按反序遍历元素的目的。如果在标准算法库中用反向迭代器来代替普通的迭代器,那么运行结果与正常情况下相反。除此之外,其用法与普通迭代器完全一样,我们不作详细讨论。
反向迭代器reverse_iterator是一种反向遍历容器的迭代器,也就是从最后一个元素到第一个元素遍历容器。反向迭代器的自增(或自减)的含义反过来了:对于反向迭代器,++运算符将访问前一个元素,–运算符将访问下一个元素。

注意!!

逆向迭代器为了保持逻辑意义与不同迭代器对应,对其物理意义进行了调整。
我们都知道begin()指向的是第一个元素,end()指向的是最后一个元素的下一个位置,但是,对于逆向迭代器而言,rbegin(),它物理位置是容器最后一个元素的下一个位置逻辑位置即容器最后一个元素的位置(对反向迭代器来说就是第一个元素元素的位置),rend(),物理位置为容器第一个元素位置逻辑位置即第一个位置的前一个位置。 相当于物理相当于逻辑往后整体移动一位。

比如,假设我有1,2,3,4,5,6,7,8,9九个数,使得一个普通迭代器指向5,如果用这个迭代器去初始化一个逆向迭代器,那么它拥有和普通迭代器相同的物理位置,所以它的逻辑位置应该是指向4的,我们取得的值也为4。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值