STL 容器

vector

动态数组, 可以动态定义长度.

#include <bits\stdc++.h>

using namespace std;

int main() {
	int n = 3;
	vector<int> vec;
	vec.push_back(1);//[1]
	vec.push_back(2);//[1,2]
	vec.push_back(3);//[1,2,3]
	vec[1] = 3;//[1,3,3]
	vec[2] = 2;//[1,3,2]
	vec.pop_back();//[1,2]
	vec.pop_back();//[1]
	// 清空 vector 
	vector<int>().swap(vec);


	// 构造函数
    n = 10;
    vector<int> vec2(n, 1);  
    for (int i = 0; i < vec2.size(); i++)//获取长度
	{
	//	cout << vec2[i] << ' ';//访问
	}
    
    // 二维 vector
    vector<vector<int> > vec3;
    // 初始化 
    for (int i = 0; i < n; i++) {
		vector<int> x(i + 1, 1);
		vec3.push_back(x);
	}
	
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < vec3[i].size(); j++) {
			cout << vec3[i][j] << ' ';
		}
		cout << endl;
	}
	// 二维vector 构造函数
	vector<vector<int> > vec(10, vector<int>()); 

	
	return 0;
	
} 

stack

栈, 先进后出

#include <stack>
#include <iostream>
using namespace std;
int main()
{
    stack<string> s;
    s.push("123456");
    s.push("aaaaa");
    s.push("bbbbbb");
    while (!s.empty()) 
    {
        cout << s.top() << endl;
        s.pop();
    }
    cout << s.size() << endl;
    return 0;
}

queue

队列: 先进先出

#include <queue>
#include <iostream>
using namespace std;
int main() 
{
    queue<int> q;
    q.push(1);
    q.push(2);
    q.push(3);
    cout << q.front() << endl;
    q.pop();
    while (!q.empty()) {
        // 如果队列不空,一直出队,用这样的方法清空一个队列,因为队列没有 clear 方法。
        cout << q.front() << endl;
        q.pop();
    }
    return 0;
}

优先队列

他和queue不同的就在于我们可以自定义其中数据的优先级, 让优先级高的排在队列前面,优先出队

和队列基本操作相同:

  • top 访问队头元素
  • empty 队列是否为空
  • size 返回队列内元素个数
  • push 插入元素到队尾 (并排序)
  • emplace 原地构造一个元素并插入队列
  • pop 弹出队头元素
  • swap 交换内容
//升序队列
	priority_queue <int, vector<int>, greater<int> > q;
	//降序队列
	priority_queue <int, vector<int>, less<int> >q2;
	
	priority_queue<int> p; // 默认升序队列

自定义优先级

#include<iostream>
#include<queue>
#include<cstdlib>
using namespace std;
struct Node{
	int x,y;
	Node(int a, int b) {
		this->x = a;
		this->y = b;
	} 
};
 
struct cmp{
	bool operator()(Node a, Node b){
		if(a.x == b.x)	return a.y > b.y;
		return a.x < b.x; // 大的优先
	}
};
 
int main(){
	priority_queue<Node, vector<Node>, cmp>p;
	
	for(int i=0; i<10; ++i)
		p.push(Node(rand(), rand()));
		
	while(!p.empty()){
		cout<<p.top().x<<' '<<p.top().y<<endl;
		p.pop();
	}//while
	//getchar();
	return 0;
}

map

#include <bits\stdc++.h>

using namespace std;

int main() {
	
	
	map<string, int> dict;              // dict 是一个 string 到 int 的映射,存放每个名字对应的班级号,初始时为空
    dict.insert(make_pair("Tom", 1));   // {"Tom"->1}
    dict.insert(make_pair("Jone", 2));  // {"Tom"->1, "Jone"->2}
    dict.insert(make_pair("Mary", 1));  // {"Tom"->1, "Jone"->2, "Mary"->1}
    dict.insert(make_pair("Tom", 2));   // {"Tom"->1, "Jone"->2, "Mary"->1}
    
    dict["Tom"] = 1;   // {"Tom"->1}
    dict["Jone"] = 2;  // {"Tom"->1, "Jone"->2}
    dict["Mary"] = 1;  // {"Tom"->1, "Jone"->2, "Mary"->1}
    cout << "Mary is in class " << dict["Mary"] << endl;
    cout << "Tom is in class " << dict["Tom"] << endl;
    
    if (dict.count("Mary")) {
        cout << "Mary is in class " << dict["Mary"] << endl;
    }
    
    for (map<string, int>::iterator it = dict.begin(); it != dict.end(); it++) {
        cout << it->first << " -> " << it->second << endl;  // first 是关键字, second 是对应的值
    }

// clear() 

/*
	pair<T1, T2> p1;            //创建一个空的pair对象(使用默认构造),它的两个元素分别是T1和T2类型,采用值初始化。
	pair<T1, T2> p1(v1, v2);    //创建一个pair对象,它的两个元素分别是T1和T2类型,其中first成员初始化为v1,second成员初始化为v2。
	pair<T1, T2> p2 = make_pair(v1, v2);          // 以v1和v2的值创建一个新的pair对象,其元素类型分别是v1和v2的类型。
	p1 < p2;                    // 两个pair对象间的小于运算,其定义遵循字典次序:如 p1.first < p2.first 或者 !(p2.first < p1.first) && (p1.second < p2.second) 则返回true。
	p1 == p2;                  // 如果两个对象的first和second依次相等,则这两个对象相等;该运算使用元素的==操作符。
	p1.first;                   // 返回对象p1中名为first的公有数据成员
	p1.second;                 // 返回对象p1中名为second的公有数据成员

*/

// 初始化
	pair<string, string> author("James","Joy");    // 创建一个author对象,两个元素类型分别为string类型,并默认初始值为James和Joy。
	pair<string, int> name_age("Tom", 18);
	pair<string, int> name_age2(name_age);    // 拷贝构造初始化 
	
	return 0;
	
} 

unordered_map

std::unordered_map<std::string, std::int> umap; //定义

umap.insert(Map::value_type("test", 1));//增加

//根据key删除,如果没找到n=0
auto n = umap.erase("test")   //删除

auto it = umap.find(key) //改
if(it != umap.end()) 
    it->second = new_value; 

//map中查找x是否存在
umap.find(x) != map.end()//查
//或者
umap.count(x) != 0

注意:使用auto循环时候,修改的值作用域仅仅循环之内,出去循环还会变成未修改的数值。

for(auto x : unomap) { //遍历整个map,输出key及其对应的value值
	x.second = 0;	
	cout << x.second << endl; //全是  000;;	
}
cout << x.second << endl;//回复原来的数值的。
彻底改变:使用find彻底找到这个数值,然后在进行改,可以保证作用域是整个程序。
for(auto x : unomap)//遍历整个map,输出key及其对应的value值
{
	auto it = umap.find(key) //改
	if(it != umap.end()) 
	    it->second = new_value; 
}		

遍历

unordered_map<key,T>::iterator it;
    (*it).first;   //the key value
    (*it).second   //the mapped value
    for(unordered_map<key,T>::iterator iter = mp.begin(); iter != mp.end(); iter++)
          cout << "key value is" << iter->first << " the mapped value is " << iter->second;

    //也可以这样
    for(auto& v : mp)
        print v.first and v.second

嵌套 map 及其遍历

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 1000005;
map<int, map<int, int> >mp;
/*
用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,
它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器
*/
int main() {
	mp[2][3] = 1; 
	map<int,map<int,int> >::iterator it;  // 以下是如何遍历嵌套 mp
	map<int, int>:: iterator it2;
	for(it = mp.begin(); it!=mp.end(); it++) {
		for(it2 = it->second.begin(); it2 != it->second.end(); it2++)
			cout << it->first << " " << it2->first << " " << it2->second << " " << endl;
	}
	return 0;
}

set

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

struct Node {
    int x, y;
    
    bool operator<(const Node &rhs) const {
        if (x == rhs.x) {
            return y < rhs.y;
        } else {
            return x < rhs.x;
        }
    }
    
};

int main()
{
	
	set<string> country;//{}
	country.insert("China");//{"China"}
	country.insert("America");//{"China", "America"}
	country.insert("France");//{"China", "America", "France"}
//	country.erase("America");//{"China", "France"}
//	country.erase("England");//{"China", "France"}
	
	if (country.count("China")) {
		cout << "China belong to country" << endl;
	}
	
	for (set<string>::iterator it = country.begin(); it != country.end(); it++) {
		cout << *it << endl;
	}
	// set 会自动帮我们排序
	 // 所以 自定义结构体需要手写 运算符重载  见上面 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值