STL部分用法总结(sort、partial_sort、find、迭代器、advance、distance)

1. vector

初始化

vector<int> vec1;
vector<int> vec1(7);
vector<int> vec1(7,3);
vector<int> vec1 = {1,2,3.0,4,5,6,7};
vector<int> vec1 = vec2;
//或
vector<int> vec1(vec2);
vector<int> vec1(vec2.begin(),vec2.end() - 1);

 排序

vector<int>a;
int b[100];
//sort默认从小到大排序
sort(a,begin(), a.end());
sort(a.begin(), a.end(), cmp);
sort(b, b + 100);
sort(b, b + 100, cmp);
//仅排序[50,100)中的元素
partial_sort(a.begin(), a.begin() + 50, a.begin() + 100,cmp);
partial_sort(b, b + 50, b + 100,cmp);

自定义排序cmp

//从大到小排序
bool cmp(int a, int b)
{
    return b<a;
}

//按照stu的num字段从小到大排序
bool cmp(const stu& a, const stu &b)
{
    return a.num > b.num;
}

重写<操作符

struct stu
{
    int num;
    string id;
};
//按照stu的num字段从小到大排序
bool operator< (const stu& a, const stu &b)
{
    return a.num < b.num;
}

查找

find

vector<int>::iterator it = find(vec.begin(), vec.end(), 6);
if (it != vec.end())
	cout << "Yes, index:" << distance(vec.begin(), it) << endl;
else
	cout << "No" << endl;

重写==操作符(对于结构体类型)

struct stu
{
    int num;
    string id;
};

bool operator== (const stu& a, const stu &b)
{
    return a.num == b.num;
}

int main()
{
    vector<stu>vec;
    vec.push_back((stu){451, "111"});
    vec.push_back((stu){121, "333"});
    vec.push_back((stu){850, "222"});
    vector<stu>::iterator it = find(vec.begin(), vec.end(), (stu){121, ""});
    //输出结果:Yes, index:1
    if (it != vec.end())
        cout << "Yes, index:" << distance(vec.begin(), it) << endl;
    else
        cout << "No" << endl;
    return 0;
}

lower_bound、upper_bound

//在有序的[begin,end)中二分查找第一个>=num的值并返回其迭代器
lower_bound(begin, end, num)
//在有序的[begin,end)中二分查找第一个>num的值并返回其迭代器
upper_bound(begin, end, num)

迭代器

vector<int>data(10);
//下标->迭代器:初始化迭代器指向data[5]
vector<int>::iterator i = data.begin() + 5;
//将i后移2个元素
advance(i, 2);
//迭代器->下标:i与data.begin()的距离,int类型,等于i所指向元素的下标
distance(data.begin(), i); 

2.set

初始化

set<int>a;
a.insert(1);
a.insert(2);
a.insert(3);
a.insert(4);
a.insert(5);
int n;

排序

默认从小到大排序,对于自定义类型可以重写“<”操作符

查找

if (a.find(2) == a.end())
{
    cout << "a中不包含元素2" << endl;
}

迭代器

允许的:

n = *begin();

//使用迭代器
set<int>::iterator iter = a.begin();
n = *iter;
n = *(++iter);//++iter、iter++都被允许
advance(iter, 2);
//求当前迭代器所指元素下标
int d = distance(a.begin(), iter);

不被允许的:

n = *(begin() + 1);//+2、3、4、、、等都不允许

//使用迭代器
set<int>::iterator iter = a.begin();
n = *(iter + 1);//+2、3、4、、、等都不允许

3. String

查找find

原型函数:以find函数为例,find_first_of、find_first_not_of、find_last_of、find_last_not_of类似

(find_first_of():查找在字符串中第一次与str相匹配的位置并返回;

find_first_not_of:查找在字符串中第一次与str不相匹配的位置并返回;

find_last_of、find_last_not_of类似)

//index为开始查找的下标
size_type find( const basic_string &str );
size_type find( const basic_string &str, size_type index );
size_type find( const char *str);
size_type find( const char *str, size_type index);
size_type find( char ch);
size_type find( char ch, size_type index );

示例:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    string str = "Hello World!";
    string key1 = "llo";
    char key2[] = "llo";
    char key3 = 'o';
    string::size_type index1 = str.find(key1);
    string::size_type index2 = str.find(key1, 0);
    string::size_type index3 = str.find(key2);
    string::size_type index4 = str.find(key2, 0);
    string::size_type index5 = str.find(key3);
    string::size_type index6 = str.find(key3, 0);
    if (index1 != string::npos)
        cout << "Yes, index:" << index1 << endl;
    else
        cout << "No" << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MallocLu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值