前两天学到的几个函数(很好用的)

本文介绍了标准模板库(STL)中的几个重要函数,包括快速排序(sort)、稳定排序(stable_sort)、内存填充(memset)及字符串操作函数如子串获取(substr)和查找(find)等,并通过实例展示了这些函数的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      首先说明,今天所说的函数都是StL(标准模板库)中提供的。stl是非常重要的一块内容,目前只是学习了几个函数而已,但这是必须掌握的只是。今天就急着几个函数。

一。各种‘sort’

      1.快速排序(sort):对给定区间进行快速排序。

          例题。

描述

使用折半查找找出目标值所在位置。

输入一个整数n n个整数 要找的目标值输出要找的目标值在序列中的位置,如果找不到,输出"no answer"样例输入

样例1输入
3
1 2 3
2
样例2输入
4
1 5 6 8
4

样例输出

样例1输出
2
样例2输出
no answer
思路:进行折半查找需要对原数列进行排序,在这里我们选用sort快速排序。
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int b(int a[], int x, int n)
{
int left = 0; int right = n - 1;
while (left <= right)
{
int middle = (left + right)/2;
if (x == a[middle]) return middle;
if (x > a[middle]) left = middle + 1;
else right = middle - 1;
}
return -1;
}
int main()
{
    int n,i,t,m,a[10000];
    cin>>n;
    for(i=0;i<n;i++) cin>>a[i];
    cin>>t;
    sort(a,a+n);
    m=b(a,t,n);
    if(m!=-1)cout<<m+1<<endl;
    else cout<<"no answer"<<endl;
    return 0;
}
2.稳定排序(stable_sort):
对给定区间所有元素进行稳定排序.
用法与sort一致,但是在稳定性上要比sort好很多。
二。
    memset
作用:
void *memset(void *s,int c,size_t n) 将已开辟内存空间s的前n个字节的值设为值c。其实主要用于数组初始化。
例题:

            har str[100];

            memset(str,0,100);


三。
substr
          string的子串: str.substr(first,len);
          first代表str的起始位置(从0开始)
          len代表字符个数,省略该参数则默认取到最后

Description

Igor K. always used to trust his favorite Kashpirovsky Antivirus. That is why he didn't hesitate to download the link one of his groupmates sent him via QIP Infinium. The link was said to contain "some real funny stuff about swine influenza". The antivirus had no objections and Igor K. run the flash application he had downloaded. Immediately his QIP Infinium said: "invalid login/password".

Igor K. entered the ISQ from his additional account and looked at the info of his main one. His name and surname changed to "H1N1" and "Infected" correspondingly, and the "Additional Information" field contained a strange-looking binary code 80 characters in length, consisting of zeroes and ones. "I've been hacked" — thought Igor K. and run the Internet Exploiter browser to quickly type his favourite search engine's address.

Soon he learned that it really was a virus that changed ISQ users' passwords. Fortunately, he soon found out that the binary code was actually the encrypted password where each group of 10 characters stood for one decimal digit. Accordingly, the original password consisted of 8 decimal digits.

Help Igor K. restore his ISQ account by the encrypted password and encryption specification.

Input

The input data contains 11 lines. The first line represents the binary code 80 characters in length. That is the code written in Igor K.'s ISQ account's info. Next 10 lines contain pairwise distinct binary codes 10 characters in length, corresponding to numbers 0, 1, ..., 9.

Output

Print one line containing 8 characters — The password to Igor K.'s ISQ account. It is guaranteed that the solution exists.

Sample Input

Input
01001100100101100000010110001001011001000101100110010110100001011010100101101100
0100110000
0100110010
0101100000
0101100010
0101100100
0101100110
0101101000
0101101010
0101101100
0101101110
Output
12345678
Input
00001000011100010110010011111101111010100111101010111000101001001111111110001010
1100011011
1100010110
0011101111
0100111111
0001010100
1110001010
0111101010
0000100001
1110011011
0010011010
Output
71366535
代码:

#include <iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int main()
{
string a,b[10];
cin>>a;
for(int i=0; i<10; i++)
cin>>b[i];
for(int i=0; i<80; i=i+10)
{
if(a.substr(i,10)==b[0]) cout<<"0";
if(a.substr(i,10)==b[1]) cout<<"1";
if(a.substr(i,10)==b[2]) cout<<"2";
if(a.substr(i,10)==b[3]) cout<<"3";
if(a.substr(i,10)==b[4]) cout<<"4";
if(a.substr(i,10)==b[5]) cout<<"5";
if(a.substr(i,10)==b[6]) cout<<"6";
if(a.substr(i,10)==b[7]) cout<<"7";
if(a.substr(i,10)==b[8]) cout<<"8";
if(a.substr(i,10)==b[9]) cout<<"9";
}
cout<<endl;
}

 四。

    find.

         string的查找:
         find(s),从前往后查找是否存在某个字符串s,存在返回开始的下标,不存在则返回-1.
例题:
    
#include <stdio.h>
#include <string>
using namespace std;
int main(){
  int cnt = 0;
  string str = "aaaaa";
  while(1){
  int get = str.find("a");
  if(get == -1) break;
  cnt++; str = str.substr(get+1);
  }
  printf("%d\n",cnt);
}

转载于:https://www.cnblogs.com/liumenggo/p/3243625.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值