c++ stl

vector

find

	vector<string> m;
    m.push_back("hello");
    m.push_back("hello2");
    m.push_back("hello3");
    if (find(m.begin(), m.end(), "hello") == m.end())
        cout << "no" << endl;
    else
        cout << "yes" << endl;

二维vector

定义:
vector<vector<int> > v(m,vector<int>(n));
你定义成这个样子就代表你可以像一个数组一样去使用它(必须配合v.resize()和v[0].resize()之类的,每次循环都得要resize一下,不然会段错误),但其实没有必要,你直接用vector<int> v[n]就可以了,就是你的里面那一层就不能像数组那样弄了,就只能用push_back()去添加了,感觉是这第二种方式更好一些。

string

find

从头开始查找

    string s("1a2b3c4d5e6f7jkg8h9i1a2b3c4d5e6f7g8ha9i");
    int position;
    //find 函数 返回jk 在s 中的下标位置
    position = s.find("jk");
    //如果没找到,返回一个特别的标志c++中用npos表示,我这里npos取值是4294967295
    if (position != s.npos)  
    {
        printf("position is : %d\n" ,position);
    }
    else
    {
        printf("Not found the flag\n");
    }

在这里插入图片描述
如果找得到的话,会返回下标

从固定位置开始查找

    string s("1a2b3c4d5e6f7jkg8h9i1a2b3c4d5e6f7g8ha9i");
    int position;
    position=s.find("b",4);
    cout<<"s.find(b,4) is : "<<position<<endl;

在这里插入图片描述
会从那个数字下标开始开始找,这边如果是3的话,那么结果就是3而不是23

substr

    string s("12345asdf");
	string a=s.substr(1,5);//获得字符串s中从第1位开始的长度为5的字符串
	cout<<a<<endl;

在这里插入图片描述

compare函数

a.compare(b)

如果a == b的话返回0
如果a < b的话返回-1
如果a > b的话返回1

	string a = "abc";
	
	string b = "abc";
	string c = "def";
	string d = "aaa";
	
	cout<<a.compare(b)<<endl;
	cout<<a.compare(c)<<endl;
	cout<<a.compare(d)<<endl;

结果是
在这里插入图片描述

print输出

printf("%s\n",a.c_str());

set

正向遍历

    set<int> s;  
    s.insert(1);
    s.insert(2);
	for(auto it=s.begin();it!=s.end();it++){
		cout<<*it;
	} 

在这里插入图片描述
反向遍历

for(auto it=s.rbegin();it!=s.rend();it++){
		cout<<*it;
	} 

删除7这个元素

s.erase(7);

find

	if(s.find(7)==s.end()){
		cout<<"not found";
	}
	else {
		cout<<*s.find(7);
	}

count
看元素在这个set中出现了几次,由于set中最多只有一次,所以0就是没出现,1就是出现

s.count(8)

排序
默认是从小到大排序的
要完成从大到小排的话,需要创建一个结构体,你在结构体里面自己写比较函数

struct mycomp  
{ //自定义比较函数,重载“()”操作符  
     bool operator() (const int &a, const int &b)  
     {  
         return a>b; 
     }  
};  
int main()  
{  
     set<int, mycomp> s; //采用比较函数mycomp  
     s.insert(5); //第一次插入5,可以插入  
     s.insert(1);  
     s.insert(6);  
     s.insert(3);  
     s.insert(5); //第二次插入5,重复元素,不会插入  
     set<int,mycomp>::iterator it;  
     for(it = s.begin(); it != s.end(); it++)  
         cout << *it << " ";  
     cout << endl;  
     return 0;  
} 

如果元素是结构体,那么需要在结构体里面重载"<"操作符

struct Info  
{  
     string name;  
     double score;  
     bool operator < (const Info &a) const // 重载“<”操作符,自定义排序规则  
     {  
         //按score由大到小排序。如果要由小到大排序,使用“>”即可。  
         return a.score < score;  
     }  
}; 

就你对于set这个东西,如果需要创建5个set,你可以这样:set<string> L[5];

multiset

支持重复元素
和set的主要差别就在于count()函数
还有erase()
在multiset中,删除迭代器的话只是删除迭代器,但是删除数字的话就是全部删除。

lower_bound(x)
返回大于等于x的迭代器,如果不存在返回end迭代器
upper_bound(x)
返回大于x的迭代器,如果不存在返回end迭代器

map

如果是unordered_map那时间复杂度会快,但就只是不支持lower_bound和upper_bound,迭代器的+±-

	map<string, int> m; // 定义一个空的map m,键是string类型的,值是int类型的
	
	m["hello"] = 2; // 将key为"hello", value为2的键值对(key-value)存入map中
	
	cout << m["hello"] << endl; // 访问map中key为"hello"的value, 如果key不存在,则返回0
	cout << m["world"] << endl;
	
	m["world"] = 3; // 将"world"键对应的值修改为3
	m[","] = 1; // 设立一组键值对,键为"," 值为1
	
	// 用迭代器遍历,输出map中所有的元素,键用it->first获取,值用it->second获取
	for (auto it = m.begin(); it != m.end(); it++) {
 		cout << it->first << " " << it->second << endl;
 	}
 	
 	// 访问map的第一个元素,输出它的键和值
 	cout << m.begin()->first << " " << m.begin()->second << endl;
 	
 	// 访问map的最后一个元素,输出它的键和值
 	cout << m.rbegin()->first << " " << m.rbegin()->second << endl;
 	
 	// 输出map的元素个数
  	cout << m.size() << endl;

m.count(a)就是看a这个键到底有没有
还有一个点m.erase(a)把a这个点的索引删除,这个用法就是和上面的count一起用的,就是把你这个索引删除和看你这个索引有没有在。

queue

queue<int> q; // 定义一个空队列q

for (int i = 0; i < 6; i++) {
	q.push(i); // 将i的值依次压?队列q中
}

cout << q.front() << " " << q.back() << endl; // 访问队列的队首元素和队尾元素

cout << q.size() << endl; // 输出队列的元素个数

q.pop(); // 移除队列的队首元素

priority_queue

默认是大根堆
优先队列它是一个堆啊,所以是top()和pop()不是front();
push()
top()
pop()

priority_queue<int> heap;

小根堆的实现
1.插入的时候插入负数,黑科技
2.定义的时候
priority_queue<int,vector<int>,greater<int>> heap

stack

关于

printf("%d ", a.top()); //取栈项数据
a.push(3); //将3这个元素入栈a
b.push(2); //将2这个元素入栈b
a.pop(); //将栈顶元素出栈
 
//栈的大小
printf("%d %d\n", a.size(), b.size());
 
if(a.empty())
return 1; // 判断栈是否为空

sort对结构体的排序

如果是数组,那么应该
yuebings[100]

	sort(yuebings,yuebings+100,cmp);

pair

pair<int,string>p;
p=make_pair(10,"qzq");
p={20,"qzq"};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值