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


      首先说明,今天所说的函数都是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 code80characters 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 of10characters stood for one decimal digit. Accordingly, the original password consisted of8decimal digits.

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

Input

The input data contains11lines. The first line represents the binary code80characters in length. That is the code written in Igor K.'s ISQ account's info. Next10lines contain pairwise distinct binary codes10characters in length, corresponding to numbers 0, 1, ..., 9.

Output

Print one line containing8characters — 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); —}

			

                        优质内容筛选与推荐>>
1、20141126-DotNetStack
2、苹果公司一道面试题
3、html b加粗与strong加粗标签区别
4、面向对象--初始面向对象
5、Android 面试系列 二


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn