Involves some simple operations
The main contents are:
sort
vector
stack
queue
map
set
# include <iostream>
# include <algorithm>
# include <vector>
# include <stack>
# include <queue>
# include <deque>
# include <map>
# include <unordered_map>
# include <set>
# include <unordered_set>
using namespace std;
bool cmp (pair<int,int>a, pair<int,int>b){
// 自定义比较器
return a.first>b.first;
}
int main(){
//数组排序函数 sort
cout<<"**sort**\n\n";
int a[] = {
1,2,4,5,63,11,29,14};
sort(a,a+8); // sort 使用了algorithm
//sort第一个参数 代表排序的其实位置 第二个参数代表最后一个位置的下一个位置 第三个参数可以是一个比较器
for (int i=0;i<8;i++){
cout<<a[i]<<" ";
}
cout<<endl;
//string
cout<<"\n**string**\n\n" ;
/*c中字符串只能用char类型数组,c++ stl可以使用string*/
string str = "sadasf";
cout<<str<<endl;
/*string获取一行字符串 --- getline (c中的scanf遇到空格或换行结束)*/
// string str1;
// getline(cin,str1);
// cout<<str1<<end;
/*string 重载了运算符*/
string str2;
str2 += "hello";
str2 += " world";
cout<<str2<<endl;
/*对string中的数字排序*/
cout<<"对sring排序\n";
string str3 = "38291294"; // 对字符也可
sort(str3.begin(),str3.end()); // .end()指向的是最后一个元素位置的下一个位置
cout<<str3<<endl; // sort对str3本身进行了修改排序
/*erase函数 删除元素*/
cout<<"erase() 删除\n";
str3.erase(--str3.end()); // 删除最后一个元素
cout<<str3<<endl;
/*substr() 切片*/
cout<<"切片\n";
string s = str3.substr(1,5); // 从索引为 1 开始,取5个数
cout<<s<<endl;
s = str3.substr(1,-1); // 从索引为1开始,取到最后
cout<<s<<endl;
/*循环 for 或者 迭代器 iterator*/
cout<<"循环\n";
// 法1
for (string::iterator it=str3.begin();it!=str3.end();it++)
cout<<*it;
cout<<endl;
// 法2 auto 自动获取类型
for (auto x:str3) // x 取str3中每个值
cout<<x;
cout<<endl;
// 法3
for (auto it=str3.begin();it!=str3.end();it++){
cout<<*it;
}
cout<<endl;
// vector 可以看成数组
cout<<"\n**vector**\n\n";
/*定义*/
cout<<"定义\n";
vector<int> v; // 此时为空 什么也不输出
for (auto x:v) cout<<x; cout<<endl;
vector<int> v2(4); // 定义一个大小为4的vector,初始为0
for (auto x:v2) cout<<x; cout<<endl;
vector<int> v3(4,1); // 定义一个大小为4的vector,初始为1
for (auto x:v3) cout<<x; cout<<endl;
vector<int> v4={
1,3,4,2,3,6,5,6}; // 定义一个大小为4的vector,初始为1
for (auto x:v4) cout<<x; cout<<endl;
/*push_back 从后追加数据*/
cout<<"push_back:\n";
v.push_back(3);
v.push_back(3);
for (auto x:v) cout<<x; cout<<endl;
/*erase删除元素 复杂度为O(n)*/
cout<<"erase()删除元素\n"; //删除后要对后面的元素前移
v.erase(v.begin()); // 删最后一个元素为--v.end()
for (auto x:v) cout<<x; cout<<endl;
/*size获取大小 v.size()*/
cout<<"size获取大小\n";
cout<<v.size()<<endl;
/*排序*/
cout<<"排序\n";
vector<int> v1 = {
12,3,1,3,42,3,25,4};
sort(v1.begin(),v1.end()); // 从小到大 默认为 less<>()
for(auto x:v1) cout<<x<<" "; cout<<endl;
sort(v1.begin(),v1.end(),greater<int>()); // 从大到小
for(auto x:v1) cout<<x<<" "; cout<<endl;
/*输入*/
// cout<<"vector输入\n";
// vector<int> v5;
// int aa;
// for (int i=0;i<3;i++){
// cin>>aa;
// v5.push_back(aa);
// }
// for (auto x:v5) cout<<x<<" "; cout<<endl;
// stack 栈
cout<<"\n**stack**\n\n";
stack<int> st;
/*push*/
st.push(2);
st.push(3);
/*top()*/
cout<<"取数top\n";
cout<<st.top()<<endl;
/*pop() 出栈 没有返回值*/
st.pop();
/*size查看数量*/
cout<<"查看数量\n";
cout<<st.size()<<endl;
// queue push front pop size 从后往前插入队列 front返回前面的值
// deque push_back push_front pop_back pop_front
// map 映射 底层: 有序的树状结构
cout<<"\n**map**\n\n";
map<int,int> m;
m[6] = 3;
m[5] = 8;
m[4] = 9;
for(auto it=m.begin();it!=m.end();it++) // begin()返回是指针,所以输出用 ->
cout<<it->first<<" "<<it->second<<endl; // first 第一个值 seco第二个值
/*自定义排序 map 不可排序 所以需要转化成vector*/
cout<<"map自定义排序\n"<<endl;
vector<pair<int,int>> vm(m.begin(),m.end()); // 把map转化成vector
sort(vm.begin(),vm.end(),cmp);
for (auto x:vm) cout<<x.first<<" "<<x.second<<endl;
cout<<endl;
// unordered_map 底层:无序的hash结构 快
cout<<"unordered_map\n";
unordered_map<int,int> um;
um[6] = 3;
um[7] = 9;
um[5] = 8;
um[4] = 9;
for(auto it=um.begin();it!=um.end();it++) // begin()返回是指针,所以输出用 ->
cout<<it->first<<" "<<it->second<<endl; // first 第一个值 seco第二个值
// set 集合 会去掉重复元素 并 排序 底层:树状有序结构
cout<<"\n**set**\n\n";
set<int> se;
/*插入*/
se.insert(5);
se.insert(3);
se.insert(3);
se.insert(4);
cout<<"大小为: "<<se.size()<<endl;
for (auto x:se) cout<<x<<" ";
cout<<endl;
// unordered_set 无序
operation result
**sort**
1 2 4 5 11 14 29 63
**string**
sadasf
hello world
对sring排序
12234899
erase() 删除
1223489
切片
22348
223489
循环
1223489
1223489
1223489
**vector**
定义
0000
1111
13423656
push_back:
33
erase()删除元素
3
size获取大小
1
排序
1 3 3 3 4 12 25 42
42 25 12 4 3 3 3 1
**stack**
取数top
3
查看数量
1
**map**
4 9
5 8
6 3
map自定义排序
6 3
5 8
4 9
unordered_map
4 9
5 8
7 9
6 3
**set**
大小为: 3
3 4 5
When there is a problem writing auto
With Dev-C++, the new features of C++11 cannot be used. Versions after gcc4.8 support the new features of C++11, but the -std parameter needs to be set when compiling.
We need to add a std parameter to the compiler:
- Use gcc 1.cpp -std=c++11 so that the compiler can use the new features of c++11.
It is the same to use new features in dev-c++. When he starts the compiler to compile the code, we let him add this parameter. - The setting method is: find Compiler Options in Tools, open it, and then check the Add the following commands when calling compiler: tick, add -std=c++11 in it.