Vector、set、string、map的区别

xiaoxiao2021-02-28  110

Vector、set、string、map的区别

1.插入的区别(主要看参数)

Vectore:

添加、删除用push_back(x);pop_back();

插入用index(it,x);

Set::

插入用index(x);

String:

插入 insert(pos,str);在pos下标处插入str;

Insert(it,it1,it2);在it出插入it1~it2内元素;

map:

mp.insert(make_pair(“haha”,5) );

mp.insert(pair<string,int>(“haha”,5));

若已有此键值时不会覆盖原元素;

mp[str]=int;

若已经有此key键,则会覆盖原元素

2、删除的区别

Erase(it)和erase(it1,it2)通用

除此之外

Set和map因为唯一性还可以用erase(value)和erase(key)删除元素

String还可以erase(pos,lenth);从pos位开始删除长度为lenth的字符个数

3、查找的区别

Vector无查找函数

Set和map查找为find(value)和find(key),返回都为迭代器

String:

s.find(s2);查找子串,返回的为子串的第一位的下标

s.find(s2,pos);从pos位查找,返回值为下标

4、排序的区别

Vector和string可用sort排序;

Set和map自动从小到大排序,自定义排序方式方法完全一样

5、s.at(i);和s[i]的区别

Vector和string中可以使用下标访问

name[index] 若是访问到不存在的元素

vector中会乱出结果,string中为‘/0’;

name.at(i);若是访问到不存在元素会报错。

转载请注明原文地址: https://www.6miu.com/read-85752.html

最新回复(0)