C++STL简单应用

string

string 表示可变长度的字符序列
字符串是对象
string 类支持字符串对象的各种操作
各种初始化方式
字符串之间的复制、比较、连接
查询字符串长度和判断字符串是否为空
访问字符串中的单个字符
使用string 类要包含头文件<string>

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string line;
    getline(cin,line);
    //两个参数:输入流对象和存放读入字符串的string对象
    //从指定输入流中读取内容,遇到换行符为止;将所读内容存入指定的string对象中,流中的换行符被读取并丢弃
    cout<<line<<endl;
    if(!line.empty())		//line不为空,输出
    cout << "is not empty!`" << endl;
    string line1;
    //每次读取一行文本,输出长度超过5个字符的行
    while(getline(cin, line1))
    if(line1.size() > 5)
    cout << line1 << endl;

    //字典序比较
    //string s1,s2,s3;
    //string s1 = "hello";
    //string s2 = "hello world";	// s2 > s1
    //string s3 = "Hello";		// s3 < s1, s3 < s2
    //赋值和连接
    string s4="hello world!",s5;
    s5=s4;
    s4="nihaoshijie!";

    //cin遇到空白字符(包括空格 tab键和回车)作为终止字符
    string line2;
    cin>>line2;
    cout<<line2<<endl;
    return 0;
}
/*
a b  c d
a b  c d
is not empty!`
nihaoshijie!
hello world!
a b c d
a
Process returned 0 (0x0)   execution time : 34.879 s
Press any key to continue.
*/

随机访问string中的字符,用下标运算符可以访问string对象中指定位置的字符string
对象s的下标范围从0到s.size()-1

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s;
    int n;
    getline(cin,s);
    cin>>n;
    if(n<(int)s.size())
    cout<<s[n];
    return 0;
}
/*
a b c d e
4
c
Process returned 0 (0x0)   execution time : 16.407 s
Press any key to continue.
*/

在这里插入图片描述

栈(stack)

stack是一种先进后出(First In Last Out, FILO)的数据结构,它只有一个出口,只能操作最顶端元素。
头文件<stack>
定义stack<type>name
常用操作:
name.empty() 返回bool类型,判断是否为空
name.size()返回栈内元素个数
name.top()返回栈顶元素值
name.pop()移除栈顶元素
name.push()从栈顶添加元素
后入先出

 #include <iostream>
#include <stack>
using namespace std;
int main()
{
    stack<int>s;
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
    cout<<"size:"<<s.size()<<endl;
    cout<<"top:"<<s.top()<<endl;
    s.pop();
    cout<<"size:"<<s.size()<<endl;
    if(s.empty())
        cout<<"is not empty!";
    else
    cout << "is empty!" << endl;
    return 0;
}
/*
size:4
top:4
size:3
is empty!
Process returned 0 (0x0)   execution time : 0.033 s
Press any key to continue.
*/

队列(queue)

stack是一种先进后出(First In Last Out, FILO)的数据结构,它只有一个出口,只能操作最顶端元素。
头文件<queue>
定义queue<type>name
常用操作:
name.empty()返回bool类型,判断是否为空
name.size()返回queue内元素个数
name.push()从queue顶添加元素
name.pop()从queue元素
name.front()返回queue内的下一个元素
name.back()返回queue内的最后一个元素
先入先出

#include <iostream>
#include <queue>
using namespace std;
int main()
{
    queue<int>s;
    s.push(1);
    s.push(2);
    s.push(3);
    s.push(4);
    cout<<"size:"<<s.size()<<endl;
    cout<<"back:"<<s.back()<<endl;
    cout<<"front:"<<s.front()<<endl;
    s.pop();
    cout<<"size:"<<s.size()<<endl;
    cout<<"pop.back:"<<s.back()<<endl;
    cout<<"pop.front:"<<s.front()<<endl;
    if(!s.empty())
    cout<<"is not empty!";
    else cout<<"is empty!";
    return 0;
}
/*
size:4
back:4
front:1
size:3
pop.back:4
pop.front:2
is not empty!
Process returned 0 (0x0)   execution time : 0.032 s
Press any key to continue.
*/

vector动态数组

头文件<vector>
定义vector<type>name
常用操作:
name.empty() 返回bool型,表示vector是否为空
name.size() 返回vector内元素个数
name.push_back将元素a插入最尾端
name.pop_back() 将最尾端元素删除
v[i] 类似数组取第i个位置的元素

#include <iostream>
#include <vector>
using namespace std;
int main()
{
    vector<int>v;
    int i;
    for(i=0;i<5;i++){
        v.push_back(5-i);}
    for(i=0;i<5;i++) cout<<v[i]<<" ";
    cout<<endl;
    cout<<"size:"<<v.size()<<endl;
    v.pop_back();
    for(i=0;i<v.size();i++)
    cout<<v[i]<<" ";
    cout<<endl;
    return 0;
}
/*
5 4 3 2 1
size:5
5 4 3 2

Process returned 0 (0x0)   execution time : 0.031 s
Press any key to continue.
*/

优先队列

头文件: #include <queue>
定义:priority_queue <type> name;//默认是大顶堆
常用操作:
q.push(elem) 将元素elem置入优先队列
q.top() 返回优先队列的下一个元素
q.pop() 移除一个元素
q.size() 返回队列中元素的个数
q.empty() 返回优先队列是否为空
接下来通过实例来理解:
关键在于搞清top()和pop()所取元素的位置

#include <iostream>
#include <queue>
using namespace std;
int main()
{
    priority_queue<int>p;//默认是大顶堆
    p.push(1);
    p.push(2);
    p.push(3);
    p.push(4);
    p.push(5);
    cout<<"top1:"<<p.top()<<endl<<"size1:"<<p.size()<<endl;
    p.pop();//从输入端移除一个元素
    cout<<"top2:"<<p.top()<<endl<<"size2:"<<p.size()<<endl;
    if(p.empty()) cout<<"is empty";
    else cout<<"is not empty";
    return 0;
}

在这里插入图片描述
优先队列的特点:
其中在解释原理中使用了重载运算符<,展示了排列的原理

#include<iostream>
#include<queue>
using namespace std;
struct node{
      int x;
}s;
bool operator <(node a,node b){
      return a.x<b.x;//从大到小排序,与bool cmp()sort相反
      //return a.x>b.x;//从小到大排序
}
priority_queue<node>q;
int main(){
    for(int i=1;i<=10;i++){
           cin>>s.x;//输入
           q.push(s);//入队
      }
      while(!q.empty()){
                 printf("%d ",q.top());
                 q.pop();//输出头元素,且删除队列的第一个元素
      }
           return 0;
}

在这里插入图片描述与sort相反,sort默认从小到大排列;
优先队列默认从大到小排列

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值