C++ STL的简单运用——学习记录

     本周学了一下STL,学的内容有string,stack,queue,vector,priority queue,map,set与其简单应用, 速度比较快,,还好学之前我预习了一些,,,在学的过程中有这么两个地方我觉得应该特别注意一下——重载运算符和sort。下面我将对这两个内容着重介绍一下,然后对本周其他学习的内容进行简单的记录。
    重载运算符
    个人理解:是一个特殊的函数,当匹配到对应的数据类型和对应的运算符时用新的运算规则来进行运算
代码举例

/*结构体内重载*/
#include <bits/stdc++.h>

using namespace std;
struct coord
{
 int x,y;
 const bool operator </* 这里的 "<"在类内,注意是在类内,被重新定义运算*/ (const coord &b/*传入类型*/)const/*当匹配到“<”和对应类型时此段特殊的函数将自动运行*/
 {
  /*此处为新的运算规则*/
    if(x!=b.x)
    {
       return x>b.x;
    }
    else
   {
      return y<b.y;
   }
 }
};
int main()
{
  priority_queue<coord> c ;/*优先队列总是会让“最大”数值排在队首,可以用重载运算符来改变其规则,试着把“最小”数值排在队首*/
  coord a;
  for(int i=0;i<3;i++)
  {
   cin>>a.x>>a.y;
   c.push(a);/*压入元素*/
  }
  for(int i=0;i<3;i++)
  {
  cout<< c.size()<<endl;
  cout<< c.top().x<<" "<<c.top().y<<endl;
  c.pop();
  }
  return 0;
 }
/*运行结果
4 5
2 0
1 2
3 成员数量
1 2  成员内容
2
2 0
1
4 5
*/
/*
有一点说明一下,因为优先队列的排序也并不特殊,你给出一个结构体类型里面包含多个数据没法排
所以这里的重载应该是必要的。

*/

sort
    年少不知sort好,错把冒泡当成宝。
    关于sort这个排列函数,普通简单用法有直接用sort(begin,end),实现升序排序。其他的还有通过函数实现自定义排序比如按照各位大小排,按照十位大小拍,如此很容易想到和结构体关联,实现对结构体数组的排序。

/*普通运用就不说了,,直接看一下与函数关联的*/
#include <bits/stdc++.h>
using namespace std;
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
    int a[100];
    int n;
    cin>>n;
   srand(time(0));
   for(int i=0;i<n;i++)
   {
    a[i]=rand()%10;
   }
   sort(a,a+n,cmp);
   for(int i=0;i<n;i++)
   {
   cout<<a[i]<<" ";
   }
   cout<<endl;
}
/*运行结果
5
8 6 2 0 0

Process returned 0 (0x0)   execution time : 1.480 s
Press any key to continue.
*/
/*或者说你要是想改变排序规则可以直接改变cmp里面的函数体*/

     然后是和结构体关联

/*我好懒,不写新的了,拿上回写的代码来弄吧*/
#include <bits/stdc++.h>

using namespace std;

struct person
{
    string name;
    int count;
}leader[3]={"特朗普",0,"普朗特",0,"朗普特",0}/*此处定义了结构体数组并且初始化了*/;
bool cmp(person x,person y)
{
    return x.count<y.count;/*此处根据选票高低进行结构体数组的排序*/
}
int main()
{
    cout<< "请输入选民个数"<<endl;
    int t;
    for(cin>>t;t;t--)
    {
        cout<<"请进行选择,1是特朗普,2是普朗特,3是朗普特,非法输入视为弃权"<<endl;
        int a;
        cin>>a;
        switch (a)
        {
        case 1:
            {
                leader[0].count++;
                break;
            }
        case 2:
            {
                leader[1].count++;
                break;
            }
        case 3:
            {
                leader[2].count++;
                break;
            }
            default :
            {
                cout<<"非法输入视为弃权"<<endl;
                break;
            }
        }
    }
    sort(leader,leader+3,cmp);
    cout<<leader[2].name<<" 共有:"<<leader[2].count<<"票"<<endl;

    return 0;
}
/*可见,sort可以“牵一发而动全身”*/
/*
运行结果
请输入选民个数
1
请进行选择,1是特朗普,2是普朗特,3是朗普特,非法输入视为弃权
1
特朗普 共有:1票
*/

然后我想写写关于这周学的STL的简单应用和新学的结构体与类的相关知识中我觉得比较重要的,其中优先队列已经写过了,下面就不写了。
1. set
关于set可以直接理解为一个不重复的有序数组(默认升序),当外界想要插入内部已有的元素时是无法插入的。
set 简单应用
学会了用迭代器,美滋滋~

#include <bits/stdc++.h>

using namespace std;
struct test
{
    char x,y;
    bool operator <(const test &b)const
    {
      if(x==b.x)
      {
          return x>b.x;
      }
      else
      {
          return y>b.y;
      }
    }
};
int main()
{
    cout<<"想要测试几组?"<<endl;
    int t;
    for(cin>>t;t;t--)
    {
    test test1;
   set <test> s1;
   set <test>::iterator iter;
   cout<<"input:"<<endl;
   for(int i=0;i<3;i++)
   {
       cin>>test1.x>>test1.y;
          s1.insert(test1);
   }
   cout<<"output:"<<endl;
   for(iter=s1.begin();iter!=s1.end();iter++)
   {
          cout<<(iter->x)<<" "<<(iter->y)<<endl;/*注意“->”符号的运用*/

   }
    }
   return 0;
}

/*用几组输入输出来说明一下
想要测试几组?
4
input:
4 5
5 4
4 4
output:
4 5
5 4
input:
4 4
4 5
5 4
output:
4 4
input:
1 2
2 3
3 4
output:
3 4
2 3
1 2
对比前两组可以看到只有当结构体两个数据都不一样的时候set才会判定是不重复元素
看最后一组输入输出会发现,这个实现了降序,这里就是重载运算符起了作用
*/

2.vector
动态数组,这个东西比较好用。在增删元素都有比较优秀的表现,你一开始开了个10大小的空间,当空间不够用的时候会自动再追加10个大小的空间

#include <bits/stdc++.h>
/*动态数组*/
using namespace std;
int main(){
    vector<int> a;
    for (int i = 0; i < 5; ++i){
        a.push_back(5 - i);
    }
    cout << "大小" << a.size() << endl;
    a.pop_back();/*移除末端元素*/
    a[0] = 1;/*类似数组操作的赋值*/
    cout << "大小" << a.size()/*看看有多少元素*/ << endl;
    for (int i = 0; i < (int)a.size(); ++i){
        cout << a[i] << ", " << endl;
    }
    cout << endl;
    return 0;
}
/*
定义:vector <data_type> vector_name;
	如:vector <int> v;
操作:
	empty() -- 返回bool型,表示vector是否为空 (v.empty() )
	size() -- 返回vector内元素个数 (v.size() )
	push_back(data_type a) 将元素a插入最尾端
	pop_back() 将最尾端元素删除
	v[i] 类似数组取第i个位置的元素(v[0] )
*/

3.string
字符串类型,非常好用且方便,但是有时候会有溢出问题,需要注意,基本操作之前已经比较熟悉了我来写一下连个比较有意思的。
在这里插入图片描述fing和sting的getline

#include <bits/stdc++.h>
/*关于find的返回,返回的是一个下表,如果查找到了,就返回第一个匹配到的下表,否则就返回一个特定的nops*/

using namespace std;
int main()
{
    int t;
    for(cin>>t;t;t--)
    {
    string a,b="abc";
    cin>>a;
    cout<<a<<" "<<b<<endl;
    cout<<a.find(b)<<endl;
    }
    return 0;
}

/*5
a
a abc
4294967295
b
b abc
4294967295
c
c abc
4294967295
abc
abc abc
可以看到find的匹配是全匹配,nops是一个定值
0*/

I have string
I have set
Ahhhhh~~
set < string > s;

#include <bits/stdc++.h>


using namespace std;
int main()
{
 set <string> s;
 set <string>::iterator iter;
 s.insert("abc");
  s.insert("ab");
   s.insert("ac");
    s.insert("bc");
     cout<<"目前拥有元素"<<endl;
    for(iter=s.begin();iter!=s.end();iter++)
    {
        cout<<*iter<<" ";
    }
    cout<<endl;
    cout<<"查找"<<endl;
    iter=s.find("bc");
    if(iter!=s.end())
    {
        cout<<*iter<<endl;
    }
    return 0;
}
/*
注意,如果没有查到的话迭代器会停在指向s.end(),也就是第一个无效元素地址
*/

getline

string a;
getline(cin,a);

除了这些这个周还学了一些小东西
0.

这个就没有必要代码块了,
就是在主函数外边加一个
#define MAX 1000
意思就是接下来有个MAX是1000了
#define foi for(int i=0;i<10;i++)
意思就会foi代表了后边那个for循环,以后写可以适当用一些提高速度
 1.
对数

  /*对数的函数应该是log,当我们要规定以啥为底谁的对数时可以这么写*/
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    double c=log(n)/log(16);
     cout<<c<<endl;
}

/*这就是爱~~~~(bushi)
这就是换底公式
可以用于16进制换底之类的进位的题目,找不到之前我写的那个了,,等以后找到了再补上代码*/

2.
去重函数——unique

/*主要注意两个点,一个是去重前先排序一下因为这个函数只针对相邻的元素(功能也不是很强大),另一个是去重后的元素数*/
#include <bits/stdc++.h>

using namespace std;

int main()
{
   int a[100];
   int t,n;
   cin>>n;
   for(int i=0;i<n;i++)
   {
       cin>>a[i];
   }
   sort(a,a+n);
   t=unique(a,a+n)-a;
   for(int i=0;i<t;i++)
   {
       cout<<a[i]<<" ";
   }
   cout<<endl;
   return 0;
}
/*运行结果
6
1 1 2 2 3 3
1 2 3

3.
向上和向下取整

ceil(1.5)=2
ceil(0.5)=1
ceil(1)=1

floor(10.5) = 10
/*很简单对吧*/

4.
文本输入和快速输入

文本输入,记得提交代码时一定要注销掉
freopen("in.txt","r",stdin);
快速输入
 ios::sync_with_stdio(false);

5.
随机输入,用于测试

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a[100];
    int n;
    cin>>n;
   srand(time(0));
   for(int i=0;i<n;i++)
   {
    a[i]=rand()%10;
   }
   for(int i=0;i<n;i++)
   {
       cout<<a[i]<<endl;
   }
   return 0;
}

2021.3.13
追随远方的某R

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值