C ++ STL basics

1. Reasons for choosing C ++ brush algorithm

  • 1. C ++ is fast (C is not faster, java is too slow)
  • 2. C ++ has STL (what is STL)-very easy to use class library
  • 3. How to use STL for efficient brushing algorithm
  • 4. Benefits: Brushing algorithm, learning cost is extremely low
  • 5. How to go from C to C ++ (only basic grammar to brush algorithm level)
俗话说:磨刀不误砍柴工
不会c++仍然可以做,但是效率低

2. Input and output

C ++ retains C's scanf and printf, and adds additional cin and cout

example

2.1. Input and output in C program
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. Wrap gracefully
cout<<1;
cout<<endl;
cout<<2;
cout<<3<<endl<<endl;

benefit:

1. Write a lot less

2. Continuous input and output variables

3. Elegant line breaks

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

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

3. STL (Standard Template Library) and algorithm header files

STL is a collection of "containers". These "containers" are list, vector, set, map, etc. STL is also a collection of algorithms and other components.

algorithm is some algorithm functions inherited from the container

sort function

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: equivalent to the encapsulation of char *, understood as a 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. Get a line of strings

I want to get a line of string

hello world

C:

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

C ++:

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

+ = For strings, characters are valid, numbers will be converted to asc codes

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. Loop (3 types)

1.for loop

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. Use C ++ 11 new features for loop

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

5、STL——vector(*)

Concept: vector is equivalent to an array, and the template type is equivalent to the stored 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;

2. Use at or [] to get the element

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

3. Method

  • 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;
  • resize
v.resize(10);//不赋值默认为0
  • erase delete element, complexity is O (n)
v.erase(v.begin());//删除第一个元素
v.erase(--v.end());//删除最后一个元素
  • Get the first element, 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, not written, the default is less ()

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 an element onto the stack
  • pop pops an element, pop has no return value
  • top Take the top element of the stack
  • size View the number of elements
s.push(2);
s.push(3);
cout<<s.top()<<endl;
s.pop();
cout<<s.top()<<endl;
cout<<s.size()<<endl;
  • Hex conversion (decimal to 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 words (expand sstream, stoi, itoa)

Enter a string of characters and print the strings in reverse order

输入: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 number

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;
  • Number 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 is a tree table, unorderedmap is a 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;
}
  • Pair usage (map to vector for sorting)
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)

Concept: collection

  • Application counting, 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: double-ended queue

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;
  • Sort
sort(d.begin(),d.end(),greater<int>());

11、STL——list

Concept: 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. Documentation

English:

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

Chinese:

http://c.biancheng.net/stl/map/
Published 13 original articles · praised 5 · visits 459

Guess you like

Origin blog.csdn.net/why18767183086/article/details/104174540