【C++常用STL容器及方法汇总】

vector

#include <vector>  
#include<algorithm>

//初始化
vector<int> data;
vector<string> data(10,"hello");
vector<int> data(5,(1,2,3,4,5));
vector<vector<int>> data;
//常用
data.size();
if(data.empty())
reverse(data.begin(),data.end());
------------------------------------------------------------------------------------------------------------
sort(data.begin(),data.end());  //升序
sort(data.begin(), data.begin()+3);  //部分升序
bool GreaterSort (Point2 a,Point2 b) { return (a.x>b.x); }
bool LessSort (Point2 a,Point2 b) { return (a.x<b.x); }
sort(aaa.begin(),aaa.end(),LessSort);//升序排列
sort(aaa.begin(),aaa.end(),GreaterSort);//降序排列
------------------------------------------------------------------------------------------------------------
int maxValue = *max_element(v.begin(),v.end()); //最大值
int minValue = *min_element(v.begin(),v.end()); //最小值
int maxPosition = max_element(v.begin(),v.end()) - v.begin(); //最大值下标
int minPosition = min_element(v.begin(),v.end()) - v.begin(); //最小值下标
------------------------------------------------------------------------------------------------------------
max_element(data.begin(),data.end());
min_element(data.begin(),data.end());
//插入
data.push_back(2);   //在尾部插入
data.insert(data.begin(),2);   //在指定位置插入
//删除
data.clear();
data.erase(data.begin().data.begin()+3);
data.pop_back();
vector<int> vec{ 1, 2, 3, 4, 5 };
vector<int>::iterator it = vec.begin() + 2;
vec.erase(it);    //删除迭代器指定元素;
//截取
vector<int> v4(v1.begin(),v1.begin()+5); 

queue

#include<queue>
 
queue<int> q;
q.size();
q.front();   //队列头元素
int num=q.top(); 
q.back();    //队列尾元素
q.push(2);
q.pop();
if(q.empty());
queue<int>q2=q; //复制队列
queue<int>q3(q); //复制队列
q=queue<int>(); //设置为空队列
priority_queue
#include <queue>

//定义
priority_queue<int> pq;                          //默认大顶堆
priority_queue<int,vector<int>,less<int>> pq1;    //大顶堆
priority_queue<int,vector<int>,greater<int>> pq2;  //小顶堆

//常用方法
top()  :返回元素中第一个元素的引用
push():插入一个元素,并重新维护堆,无返回值.
pop() :删除优先级最高的元素,并重新维护堆无返回值
size() :返回容器中有效元素的数量,返回队列的大小
empty() :检测容器是否为空.返回“true”或者“false”.

stack

#include<stack>
 
stack<string> s;
s.size();
if(s.empty());栈为空返回true
s.push("hello!");
s.pop();
string str=s.top();

stack<string> s2=s; //复制栈
stack<string>s3(s); //复制栈
s=stack<string>(); //设置为空栈

map

#include<map>
using namespace std;
 
map<int,string> m; 
//赋值
m[0]="a";
m[1]="b";
m[2]="c";
//插入
pair<int,string> value(3,"d");
m.insert(value);
//遍历
map<int,string>::iterator it=m.begin();
for(;it!=m.end();it++)
    cout<<it->first<<" "<<it->second<<endl;
it->second="abc";            //可改变value值,不可改key值
//查询
if(m.count())                //返回0或1
it=m.find(5);                //返回的是迭代器,查找key值
if(it=m.end())
    cout<<"NO Match";
//删除
m.erase(it);
m.erase(m.begin(),m.end());
m.erase("d");

set

#include<set>
using namespace std;
 
//集合,自动排序
unordered_set<int> s;
set<int> s;
//插入
s.insert(2);
s.insert(5);
s.insert(3);
s.insert(6);
s.insert(1);
//遍历
set<int>::iterator it;
for (it = s.begin(); it != s.end(); ++it)
	cout << *it << " ";
cout << endl;
//删除
s.erase(2);
for (it = s.begin(); it != s.end(); ++it)
	cout << *it << " ";
cout << endl;
//查找
if (s.find(6) == s.end() || s.count(5)) {
	cout << "No Match"<<endl;
}
cout << *s.rbegin()<< endl;   //输出最后一个值

输入输出

//输出
int x = 7;
printf("%03d", x); //007
printf("%02d", x); //07
printf("%3d", x); //空格空格7
printf("%d", x); //7
//转string后输出
int i = 10;   
cout<<to_string(i)<<endl; 
//向上取整
	float a1 = 10.1;
	cout << ceil(a1) << endl
//向下取整
	float a2 = 10.1;
	cout << (int)a2 << end
//四舍五入
	float a3 = 10.4;                   //4舍
	cout << (int)(a3+0.5) << endl;
	
	float a4 = 10.5;                   //5进
	cout << (int)(a4+0.5) << endl;
//保留6位小数
	double a6 = 123.123456789;      //建议使用 double
	printf("%.4f\n", a6);   
	printf("%.5f\n", a6);   
	printf("%.6f\n", a6);          
	printf("%.7f\n", a6);   
 
	float a5 = 123.123456789;       //不要使用 float,保留小数位数有限,并且跟整数部分长度有关
	printf("%.4f\n", a5);   
	printf("%.5f\n", a5);   
	printf("%.6f\n", a5);   
	printf("%.7f\n", a5);    
//fixed+setprecision(n)
	double pi = 3.1415926;
	double pi2 = 3.1;
	cout << setprecision(4) << pi << endl;  //实现保留4位有效数字;    输出:3.142
	cout << pi << endl;                   //setprecision(n)持续作用;输出:3.142

	cout << setprecision(4) << pi2 << endl;  // 3.1
	cout << pi2 << endl;                     //3.1

	cout << fixed << setprecision(4) << pi << endl;   //3.1416
	cout << pi << endl;                               //3.1416

	cout << fixed << setprecision(4) << pi2 << endl;  //3.1000
	cout << pi2 << endl;     

string、char、int等相互转换

//int类型转换为char类型
	int num = 6;
	char c = num + '0';
	cout<<c<<endl;
	
//char类型转换为int类型
	char c1 = '4';
	int num1 = c1 - '0';
	cout<<num1<<endl;

//int与string的转换
        int a = 7;
	float b = 3.14;
	double c = 1.415649363;
	//利用string库函数中函数to_string 
	string a_str = to_string(a);
	string b_str = to_string(b);
	string c_str = to_string(c);
	cout << "a" << endl;
	cout << "b" << endl;
	cout << "c" << endl;

//string转int
	string num_str = "2142";
	//利用string库中的stoi函数 
	int num_int = stoi(num_str);
	cout<<num_int<<endl;
	
	//利用标准库中的atoi函数
	string s = "51426";
	int value = atoi(s.c_str());
	cout << value << endl;

参考链接:
容器及方法
输入输出参考1
输入输出参考2
STL参考1
STL参考2
priority_queue_1
priority_queue_2
string with int_1
string with int_2

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值