一、String相关
1、带空格的字符串输入
string a;
getline(cin,a);
2、字串的截取
string s = "sfsa";
string a = s.substr(0, 3);
string b = s.substr();
string c = s.substr(2, 3);
cout << a << endl;
cout << b << endl;
cout << c << endl;
输出
sfs
sfsa
sa
3、字符的查找
string s = "sfsa";
string a = s.substr(0, 3);
string b = s.substr();
string c = s.substr(2, 3);
int t1, t2, t3;
t1 = s.find('s');
t2 = s.find('f');
t3 = s.find('a');
int t4 = s.find('b');
cout << t1 << endl;
cout << t2 << endl;
cout << t3 << endl;
cout << t4 << endl;
输出
0
1
3
-1
find函数也适用于vector等很多数据结构
二、结构体相关
1、结构体中符号重载
struct Point{
int x,y;
Point (int x=0,int y=0):x(x),y(y){}
};
Point operator +(const Point &A,const Point &B){
return Point(A.x+B.x,A.y+B.y);
}
ostream& operator <<(ostream& out, const Point& p)
{
out << "(" << p.x << ',' << p.y << ')';
return out;
}
int main()
{
Point a,b(1,2);
a.x=2;
cout<<a+b<<endl;
return 0;
}
struct stru
{
string x;
int y;
bool operator <(const stru& te) const { return te.y > this->y; }
bool operator >=(const stru& te) const { return te.y <= this->y; }
bool operator <=(const stru& te) const { return te.y >= this->y; }
bool operator !=(const stru& te) const { return te.y != this->y; }
bool operator ==(const stru& te) const { return te.y == this->y; }
};
三、不定长数组:vector
1、声明
vector<int>v1;
vector<int>v2[N];
2、排序
sort(v1.begin(),v.end());
3、插入与删除
v1.push_back(1);//往尾部添加
v1.pop_back();//删除尾部
4、设定大小
v1.resize(5);
5、查重
vector<int>v;
v.push_back(1);
v.push_back(1);
v.push_back(2);
v.push_back(2);
v.push_back(5);
v.push_back(5);
v.push_back(4);
sort(v.begin(), v.end());
int m=unique(v.begin(), v.end())-v.begin();
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
cout << *it << ' ';
cout << endl;
cout << m << endl;
输出
1 2 4 5 4 5 5
4
6、其它
v1.clear();//清空
v1.empty();//判空
int num=count(V.begin(),V.end(),ask); //查找ask的数量
四、集合:set
1、作用
自动判重,重复输入只存一个,自动从小到大排序
2、声明与插入
set<int>s;
s.insert(2);
s.insert(1);
s.insert(4);
s.insert(3);
s.insert(5);
3、迭代器输出
for(set<int>::iterator it=s.begin();it!=s.end();it++)
{
cout<<*it<<endl;
}
输出
1
2
3
4
5
五、映射:map
1、定义
map<key,value>代表了从键(key)到值(value)的对应关系
2、声明、插入,修改
map<string, int>m;
m["张三"] = 1;
m["李四"] = 2;
m["王五"] = 4;
m["张三"] = 6;
for (map<string, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << it->first << ' ' << it->second << endl;
}
cout << endl;
m.insert(pair<string, int>("张三", 1));
for (map<string, int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << it->first << ' ' << it->second << endl;
}
输出
李四 2
王五 4
张三 6
李四 2
王五 4
张三 6
3、查找
int t = m.count("张三");
int t2 = m.count("小明");
cout << t << ' ' << t2 << endl;
输出
1 0
六、栈(stack):后进先出
1、声明
stack<int>s;
stack<int>se[N];
2、插入与删除
stack<int>s;
s.push(1);
s.push(3);
s.push(4);
s.push(3);
s.push(6);
s.push(2);
此时s={1,3,4,3,6,2}
s.pop();
s.pop();
此时s={1,3,4,3}
int t=s.top();
t=3;
七、队列(queue):先进先出
1、声明
queue<int>q;
queue<int>qu[N];
2、输入,删除
queue<int>q;
q.push(1);
q.push(3);
q.push(4);
q.push(3);
q.push(6);
q.push(2);
q={1,3,4,3,6,2};
q.pop();
q.pop();
q={4,3,6,2};
int t=q.front();
t=4;
八、优先队列(priority_queue):谁大谁先出
1、声明
priority_queue<int>s;//谁大谁先出
priority_queue<int,vector<int>,greater<int> >q;//谁小谁先出
2、插入与删除
queue<int>q;
q.push(1);
q.push(3);
q.push(4);
q.push(3);
q.push(6);
q.push(2);
q={1,2,3,3,4,6};
q.push(5);
q={1,2,3,3,4,5,6};
q.pop();
q.pop();
q={3,3,4,5,6};
int t=q.front();
t=3;
作者:Avalon Demerzel,喜欢我的博客就点个赞吧,更多紫书知识点请见作者专栏《紫书学习笔记》