Standard Template Library STL containers usage assembly
NO.1 stack 栈
Note: closed https://www.cnblogs.com/aiguona/p/7200837.html
Storehouse
#include<stack>
Defined way
stack <_template> s; // parameters are data types, which define how the stack is
Common Operations
- s.empty () // If the stack is empty returns true, false otherwise
- s.size () // returns the number of elements in the stack
- s.pop () // delete the top element but does not return its value
- s.top () // return to the top of the stack elements, but does not remove the element
s.push (X) // new elements in the stack pressed, the parameter X is an element to be pushed
For exanple:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
int main()
{
stack<char> s;
s.push(a);
cout << s.top() <<endl;
s.push(b);
cout << s.top();
s,pop();
cout << s.top();
return 0;
}
NO.2 queue queue
Note: closed https://www.cnblogs.com/aiguona/p/7200837.html
Storehouse
#include <queue> // queue
Defined way
queue <_template> q; // parameters are data types, which are defined manner queues
Common Operations
- q.empty () // if the queue is empty returns true, false otherwise
- q.size () // returns the number of elements in the queue
- q.pop () // Delete the first element of the queue but does not return its value
- Value q.front () // returns the first element of the team, but does not remove the element
- q.push (X) // in the tail pushed a new element, X is an element to be pushed
q.back () // returns the value of the end of the queue elements, but does not delete the element
For example:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
int main()
{
queue<int> q;
stack<char> s;
q.push(1);
cout << q.enpty() << endl;
q.push(2);
cout << q.front() << endl;
q.pop();
cout << q.front() << endl;
q.pop();
cout << q.empty() <<endl;
return 0;
}
Note: STL is convenient, but using a constant huge, should the use of more than two data structures, the proposed handwriting.
NO.3 priority_queue heap priority queue or
Storehouse
#include<queue>
Definition method
priority_queue <_template, vector <_template>, cmp> q; // vector what is behind speaks
cmp is our Own a heap compare the priority of the structure function of the shape , as follows:
struct cmp{
bool operator () (int a,int b)
{
return a>b;
}
};
NOTE: This is a small heap root , is greater than the root number is small heap .
struct cmp{
bool operator () (int a,int b)
{
return a<b;
}
};
NOTE: This is a big heap root , is less than the number of stacks is large root .
DETAILED define how cmp Referring https://www.cnblogs.com/xzxl/p/7266404.html
operator overloaded operators Referring https://www.cnblogs.com/xiangxiaodong/archive/2012/02/12/ 2348144.html
Common Operations
Note: closed https://www.cnblogs.com/xzxl/p/7266404.html
- empty () // if the queue is empty, returns true
- pop () // delete the top element, delete the first element
- push () // add an element to
- size () // returns the number of elements in the priority queue has
top () // Returns the top element of the priority queue, returns the priority queue has the highest priority element
For example:
#include<iostream>
#include<queue>
#include<vector>
#include<cmath>
#include<stdlib.h>//rand()库
using namespace std;
struct cmp{
bool operator () (int a,int b)
{
return a>b;//小根堆
}
};
priority_queue<int,vector<int>,cmp> q;
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
int x=rand()%1000;//生成随机数,不同电脑数不一样,电脑相同,数可能一样,非常慢。
cout<<x<<" ";
q.push(x);
}
cout<<endl;
cout<<q.size()<<endl;
while(q.empty()==0)
{
cout<<q.top()<<" ";
q.pop();
}
cout<<endl;
cout<<q.size()<<endl;
}
No.4 deque deque
Storehouse
#include<deque>;
Defined way
\ And <_template> and the areas;
Common Operations
- push_back (int x) // tail added
- size () // length
- resize (int a) // Reserved 0 to a-1 elements, the remaining deleted
- empty () // ibid.
- begin () // returns the first element of the iterator (iterator to the back will be introduced)
- end () // Returns the last element iterator
- push_front (int x) // add an element first look
- pop_front () // Delete the first element
- pop_back () // Delete the last element
- front () // returns the first element
back () // Returns the last element
Note: If you want to visit, you can use standard array. For example, I want to visit this seventh element, the dep [7];
More are all common operations, also wanted to know if other operations, please visit the website https://www.cnblogs.com/TianMeng-hyl/p/12229706.html
NO.5 iterator
- Iterator can be simply understood as a guideline, how to declare:
容器名<_template>::iterator it;
- How to traverse: A Case Study of deque
for (it = deq.begin(); it != deq.end(); it++)
cout << *it << endl;
* It this way for each element
No.6 set collection
Tag URL <blog.csdn.net/byn12345/article/details/79523516>
Note: Because of the special nature of the collection, in which each element can and will only appear once , that there will be no repetition of elements , but also sort
Storehouse
#include<set>
Defined way
\set<_template> S;
Common Operations
- insert () // adding elements
- size () // length
- clear () // Clear
......
There are many operations ...
For example:
#include <iostream>
#include <set>
using namespace std;
int main(){
set<int> s;
s.insert(1);
s.insert(2);
s.insert(3);
s.insert(1);
cout<<"set 的 size 值为 :"<<s.size()<<endl;
cout<<"set 的 maxsize的值为 :"<<s.max_size()<<endl;
cout<<"set 中的第一个元素是 :"<<*s.begin()<<endl;
cout<<"set 中的最后一个元素是:"<<*s.end()<<endl;
s.clear();
if(s.empty())
{
cout<<"set 为空 !!!"<<endl;
}
cout<<"set 的 size 值为 :"<<s.size()<<endl;
cout<<"set 的 maxsize的值为 :"<<s.max_size()<<endl;
return 0;
}
No.7 vector Vector
Popular point that is variable length arrays
Storehouse
#include<vector>
Defined way
\vector<_template>
Common Operations
- v.push_back (); // add an element tail
- v.pop_back (); // delete the tail element
- v.size (); // number of elements
v.resize (int x); // Reserved 1 to x of element
For example
#include<iostream>
#include<vector>
using namespace std;
vector<int> v;
int main()
{
v.push_back(1);
v.pop_back();
cout<<v.size()<<endl;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.resize(2); //数组元素:1,2
cout<<v.size()<<endl;
}
No.8 map map
Storehouse
#inlcude<map>
Defined way
map<_template,_template> M;
Common Operations
- For map, I often use his array form, rather than members of his functions, such as:
map<string,int> M;
M["Monday"]=1;
In simple terms, it is the definition of a next array can be anything.
All containers are traversed by the method of the iterator. Need to begin () and end (), but slightly different, each one it is a pair (pair is below what would say), pair of first, is the array index, second is the value, for example:
#include<iostream>
#include<map>
using namespace std;
map<string,int> M;
int main()
{
string day;
int i;
while(cin>>day)
{
cin>>i;
M[day]=i;
if(i==7) break;
}
map<string,int>::iterator it;
for(it=M.begin();it!=M.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
}
/*
Monday 1
Tuesday 2
Wednesday 3
Thursday 4
Friday 5
Saturday 6
Sunday 7
*/
map in accordance with a sequential storage will be, so when using the structure, need to reload <No.
For example:
#include<iostream>
#include<map>
using namespace std;
map<string,int> M;
int main()
{
string day;
int i;
while(cin>>day)
{
cin>>i;
M[day]=i;
if(i==7) break;
}
map<string,int>::iterator it;
for(it=M.begin();it!=M.end();it++)
{
cout<<(*it).first<<" "<<(*it).second<<endl;
}
}
We see two ways to traverse, can.
No.9 pair
You can pair is understood as a number of
Storehouse
#include<pair>
Defined way
Common Operations
pair<T1, T2> p1; //创建一个空的pair对象(使用默认构造),它的两个元素分别是T1和T2类型,采用值初始化。
pair<T1, T2> p1(v1, v2); //创建一个pair对象,它的两个元素分别是T1和T2类型,其中first成员初始化为v1,second成员初始化为v2。
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的公有数据成员
For example:
std::pair<std::string, int> getPreson() {
return std::make_pair("Sven", 25);
}
int main(int argc, char **argv) {
std::string name;
int ages;
std::tie(name, ages) = getPreson();
std::cout << "name: " << name << ", ages: " << ages << std::endl;
return 0;
}
In addition to iterator, which is another way of receiving, see https://blog.csdn.net/sevenjoin/article/details/81937695 .