STL common usage

1, Why choose C ++ algorithms brush

  • 1.C ++ speed (C Why not faster, java too slow)
  • There 2.C ++ STL (What is STL) - very easy to use library
  • 3. How to use STL algorithms for efficient brush
  • 4. Benefits: brush algorithm, learning cost is very low
  • 5. How from C to C ++ (basic grammar only to the extent brush algorithm)
俗话说:磨刀不误砍柴工
不会c++仍然可以做,但是效率低

2, input and output

C ++ retains the scanf and printf C, adding additional cin and cout

example

2.1.C program O
int a;
scanf("%d",&a);
printf("%d",a);
2.2.C ++ input and output
int a;
cin>>a;
cout<<a;
2.3 continuous input and output variables
int a,b,c;
cin>>a>>b>>c;
cout<<a<<b<<c;
2.4. Gracefully wrap
cout<<1;
cout<<endl;
cout<<2;
cout<<3<<endl<<endl;

benefit:

1. write a lot less

2. Continuous input and output variables

3. Wrap elegant

注意:cin、cout比scanf、printf慢,有时候刷算法超时,可能因为使用了cin、cout

输入输出的数量(>1000)特别多,刷算法用cin,cout容易超时

3, STL (Standard Template Library) header with algorithm

STL is a collection of "containers" of these "containers" have list, vector, set, map, etc., is a collection of STL algorithms and other components.

algorithm is a container inherit some arithmetic functions, auxiliary brush algorithm problem

The sort function

The concept: iterator - understood as a pointer

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int a[]={2,1,5,0,-1,5,9};
    sort(a,a+7);
    for(int i=0;i<7;i++)
        cout<<a[i]<<" ";
    cout<<endl;
    system("pause");
    return 0;
}

4、STL——string(*)

Concept: char * corresponding to the package, it is understood string

4.1. Simple to use
/**C中定义字符串以及打印*/
char *ch="asdkajbf";
for(int i=0;ch[i]!='\0';i++) cout<<*(ch+i);
/**C++中*/
string s="ssadaffw";
cout<<s<<endl;
4.2. Gets his string

I would like to get his string

hello world

In C:

scanf("%s",ch);//1.仅获取一个单词,空格结束 2.ch[100]得设置初始大小

In C ++:

string s;
getline(cin,s);//获取一行数据
cout<<s;
4.3. + = Operator

+ = For strings, characters are valid, it will be converted into a digital code asc

string s;
s+="hello";
s+=" world";
s+='5';
s+=10;//10对应的asc码是换行
int a=5;//想把a加入字符串
s+=(a+'0');
cout<<s;
4.4. Sorting (using algorithm)
string s="5418340";
sort(s.begin(),s.end());
cout<<s;
4.5.erase function
/**begin是头迭代器,end是尾迭代器*/
string s="5418340";
s.erase(s.begin());//删除第一个
s.erase(--s.end());//删除最后一个
cout<<s;
4.6.substr function
/**begin是头迭代器,end是尾迭代器*/
string s="5418340";
s=s.substr(1,3);//取418,取索引为1,往后截断3个
s=s.substr(1,-1);//索引为1,截断到最后
cout<<s;
4.7. Cycle (three kinds)

1.for circulation

string s="5418340";
for(int i=0;i<s.length();i++) cout<<s[i];

2. iterator

for(string::iterator it=s.begin();it!=s.end();it++) cout<<*it;

3. iterator simplification

for(auto it=s.begin();it!=s.end();it++) cout<<*it;

4. Using the new C ++ 11 for loop characteristics

for(auto x:s) cout<<x;

5、STL——vector(*)

The concept: vector corresponds to the array, storing the equivalent of template type of content

1.vector construction

vector<int> v;//定义一个空vector
vector<int> v2(4);//定义一个4个大小的vector,初始为0
vector<int> v3(4,6);//定义一个4个大小的vector,初始为6
vector<int> v{1,2,3,4,5};//定义一个vector,数字为1,2,3,4,5
for(auto x:v3) cout<<x;

Or 2. at [] Get element

vector<int> v{1,2,3,4,5};
cout<<v[1];//取索引为1的
cout<<v.at(2);//取索引为2的

3. Methods

  • push_back additional content
vector<int> v;
v.push_back(5);
v.push_back(5);
v.push_back(5);
v.push_back(5);
v.push_back(6);
for(auto x:v) cout<<x;
  • reset size resize
v.resize(10);//不赋值默认为0
  • erase remove elements, complexity is O (n)
v.erase(v.begin());//删除第一个元素
v.erase(--v.end());//删除最后一个元素
  • Get the first element to get the last element
/**获取第一个元素*/
cout<<v.front();
cout<<v[0];
cout<<*v.begin();
/**获取最后一个元素*/
cout<<v.back();
cout<<v[v.size()-1];//size是获取大小
cout<<*--v.end();

4. Sort

The third parameter is the comparator, the default is not write less <int> ()

vector<int> v{5,1,2,5,4,0,-1};
sort(v.begin(),v.end(),less<int>());//从小到大
sort(v.begin(),v.end(),greater<int>());//从大到小排序
for(auto x:v) cout<<x;

5. cycle

vector<int> v{5,1,2,5,4,0,-1};
for(int i=0;i<v.size();i++) cout<<v[i];//for循环
cout<<endl;
for(vector<int>::iterator it=v.begin();it!=v.end();it++) cout<<*it;//迭代器循环
cout<<endl;
for(auto it=v.begin();it!=v.end();it++) cout<<*it;//迭代器简化循环
cout<<endl;
for(auto x:v) cout<<x;//c++11新特性

6、STL——stack(*)

Concept: Stack

  • structure
stack<int> s;
  • push、pop、size、empty
  • push a push element
  • a pop the stack elements, pop no return value
  • top to take the top element
  • View size number of elements
s.push(2);
s.push(3);
cout<<s.top()<<endl;
s.pop();
cout<<s.top()<<endl;
cout<<s.size()<<endl;
  • Base conversion (decimal turn Binary)
int itob(int decimal){
    stack<int> s;int res=0;
    while(decimal!=0){
        s.push(decimal%2);
        decimal/=2;
    }
    while(!s.empty()){
        res=res*10+s.top();
        s.pop();
    }
    return res;
}
  • Reverse word (expand sstream, stoi, itoa)

Enter his string, the string in reverse print

输入:hello world my name is steve yu

输出:yu steve is name my world hello

#include <iostream>
#include <stack>
#include <sstream>

using namespace std;

int main(){
    string str;
    stack<string> s;
    getline(cin,str);
    stringstream ss;
    ss<<str;
    while(ss>>str)
        s.push(str);
    while(!s.empty()){
        cout<<s.top();
        s.pop();
        if(s.size()!=0) cout<<" ";
    }
    return 0;
}
  • String-to-digital

method 1:

 string s="1234";
 int i;
 stringstream ss;
 ss<<s;
 ss>>i;
 cout<<i;

Method 2:

string s="1234";
int i=stoi(s);
cout<<i;
  • Digital to String

method 1:

int a=1234;
string out;
stringstream ss;
ss<<a;
ss>>out;
cout<<out<<endl;

Method 2: (c ++ 11)

int a=1234;
cout<<to_string(a)<<endl;

7、STL——queue

Concept: Queue

  • structure
queue<int> q;
  • push、back
q.push(5);
q.push(6);
cout<<q.front()<<endl;
q.pop();
cout<<q.front()<<endl;
cout<<q.size()<<endl;

8、STL——map(unordered_map pair)

Concept: Mapping (map as a tree table, unorderedmap hash table)

  • map
map<int,int> m;//有序的,树状结构(底层)
m[6]=3;
m[5]=8;
m[4]=9;
for(auto it=m.begin();it!=m.end();it++)
    cout<<it->first<<" "<<it->second<<endl;
for(auto tmp:m){
    cout<<tmp.first<<" "<<tmp.second<<endl;
}
  • unordered_map
unordered_map<int,int> m;//无序的,哈希结构(底层)
m[6]=3;
m[5]=8;
m[4]=9;
for(auto it=m.begin();it!=m.end();it++)
    cout<<it->first<<" "<<it->second<<endl;
for(auto tmp:m){
    cout<<tmp.first<<" "<<tmp.second<<endl;
}
  • Usage pair (map vector transfected into a sort)
bool cmp(pair<int,int> a,pair<int,int> b){
    return a.first>b.first;
}
int main(){
    unordered_map<int,int> m;//无序的,哈希结构(底层)
    m[6]=3;
    m[5]=8;
    m[4]=9;
    vector<pair<int,int>> v(m.begin(),m.end());
    sort(v.begin(),v.end(),cmp);
    for(auto tmp:v){
        cout<<tmp.first<<tmp.second<<endl;
    }
    return 0;
}

9、set(unordered_set)

The concept: a collection of

  • Application count deduplication
set<int> s;//树状结构,有序
unordered_set<int> s2;//哈希结构,无序,快
s.insert(3);
s.insert(4);
s.insert(4);
s.insert(4);
cout<<s.size()<<endl;
for(auto tmp:s)
    cout<<tmp<<" ";
cout<<endl;
for(auto it=s.begin();it!=s.end();it++)
    cout<<*it<<" ";
cout<<endl;

10, STL it -

Concept: deque

deque<int> d;
// 4 9 1 2
d.push_back(1);
d.push_back(2);
d.push_front(9);
d.push_front(4);
d.pop_back();
d.pop_front();
for(auto tmp:d) cout<<tmp<<endl;
for(auto it=d.begin();it!=d.end();it++) cout<<*it<<endl;
  • Sequence
sort(d.begin(),d.end(),greater<int>());

11、STL——list

The concept: a doubly linked list

list<int> li;
li.push_back(6);
li.push_front(5);
li.emplace_front(9);
li.emplace_back(10);
li.insert(++li.begin(),2);
for(auto tmp:li) cout<<tmp<<endl;
for(auto it=li.begin();it!=li.end();it++) cout<<*it<<endl;

12. Documents

English:

http://www.cplusplus.com/reference/stl/

Chinese:

http://c.biancheng.net/stl/map/

Guess you like

Origin www.cnblogs.com/littlepage/p/12113748.html