……

目录

1. #include <algorithm> 里的nique()函数.. 1

2. #include <algorithm> 里的count函数.. 2

4.字符串反转#include<algorithm>的reverse(s.begin(),s.end()); 3

5.最大和最小.. 4

6. #include <algorithm> 里的:排列生成器(全排列).. 4

7.字符串截取string中的.. 5

学一下强制类型转换.. 5

8.strlwr,strupr函数大写转小写  小写转大写.. 6

9.strcmp比较字符串s1和s2,区分字母的大小写.. 6

10.strcmpi比较字符串s1和s2,但不区分字母的大小写。.. 6

11.swap是用于交换两个变量的值的.. 7

12.c++ string怎样判断字符串里面是否含有某个字符串?.. 8

13.C++位运算.. 11

14. 11

15.itoa(num,arr,10)在stdlib.h里:int转char[] 12

16.strcat()连接两个字符串.. 13

 

1. #include <algorithm> 里的nique()函数

unique()函数是一个去重函数,STL中unique的函数 unique的功能是去除相邻的重复元素(只保留一个),还有一个容易忽视的特性是它并不真正把重复的元素删除。他是c++中的函数,所以头文件要加#include<algorithm>,具体用法如下:

    int num[100];

   unique(num,mun+n)返回的是num去重后的尾地址后面的地址,之所以说比不真正把重复的元素删除,其实是,该函数把重复的元素一到后面去了,然后依然保存到了原数组中,然后返回去重后最后一个元素的地址,因为unique去除的是相邻的重复元素,所以一般用之前都会要排一下序。

#include<iostream>

#include<algorithm>

using namespace std;

int main()

{

    int a[]={1,2,2,3,3,4,4,8,8,5};

    int *m=unique(a,a+10);

    int k=m-a;

    for(int i=0;i<k;i++)

    {

        cout<<a[i]<<endl;

    }

    return 0;

}

2. #include <algorithm> 里的count函数

.count函数:返回这个值出现次数的统计结果

#include<iostream>

#include<algorithm>

using namespace std;

int main()

{

    int a[]={1,2,2,3,3,4,4,8,8,5,2};

    int n=11;

    cout<<count(a, a+11, 2)<<endl;

    return 0;

}

3. #include <algorithm> 里的find()

#include <iostream>

#include <algorithm>

using namespace std;

int main()

{

    int m[]={35,5,5,9,3,7,10};

    int n=7;

    if (find(m,m+n,10) == m+n-1)

        cout << "yes" << endl;

    else

        cout << "no" << endl;

    cout<<find(m,m+n,5)-m<<endl;//在数组中第一次出现的位置的数组下标

}

4.字符串反转#include<algorithm>的reverse(s.begin(),s.end());

#include<iostream>

#include<string>

#include<algorithm>

using namespace std;

int main ()

{

        string s;

        s="123456";

        reverse(s.begin(),s.end());

        cout<<s;

        return 0;

}

5.最大和最小

两个值中较大的max()//在#include<iostream>里

两个值中较小的min()//在#include<iostream>里

 

序列中的最小元素:min_element()  //返回的是地址  在#include<algorithm>里

序列中的最大元素:max_element()  //返回的是地址 在#include<algorithm>里

 

#include <iostream>

#include <algorithm>

using namespace std;

int main()

{

 

    int m[]={35,5,5,9,3,7,10};

  cout<<max(35,5)<<endl;

  cout<<min(35,5)<<endl;

  cout<<max_element(m,m+7)<<endl;

   cout<<min_element(m,m+7)-m<<endl;//输出数组下标

}

6. #include <algorithm> 里的:排列生成器(全排列)

按字典序的前一个排列:prev_permutation()

按字典序的下一个排列:next_permutation()

(next_permutation()用法http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html)

next_permutation()用之前得先用sort()升序   sort不能对string类型的排序

7.字符串截取string中的

   s.substr(pos, n)    截取s中从pos开始(包括0)的n个字符的子串,并返回

    s.substr(pos)        截取s中从从pos开始(包括0)到末尾的所有字符的子串,并返回

#include<iostream>

#include<string>

using namespace std;

int main ()

{

     string s="0123456";

        string a=s.substr(4,2);

         cout<<s<<endl;

      cout<<a<<endl;

}

0123456

45

学一下强制类型转换

#include<iostream>

#include<string>

using namespace std;

 

int main ()

{

    char ch='A'+1 ;

    cout<<ch<<endl;

   return 0;

   }

8.strlwr,strupr函数大写转小写  小写转大写

#include <string.h>

#include<iostream>

using namespace std;

int main()

{

  char a[]="1233asdSDSFAsdaSDFG";

  cout<<strlwr(a)<<endl;//a不能是string类型的,必须是char型数组

  cout<<strupr(a)<<endl;

    return 0;

}

9.strcmp比较字符串s1和s2,区分字母的大小写

C/C++函数,比较两个字符串

设这两个字符串为str1,str2,

若str1==str2,则返回零;

若str1<str2,则返回负数;

若str1>str2,则返回正数。

 

10.strcmpi比较字符串s1和s2,但不区分字母的大小写。

用法:#include <string.h>

功能:比较字符串s1和s2,但不区分字母的大小写。(字符串必须是char型数组,不能是string)

说明:strcmpi是到stricmp的宏定义,实际未提供此函数。

当s1<s2时,返回值<0

当s1=s2时,返回值=0

当s1>s2时,返回值>0

 

#include<iostream>

#include<cstring>//cstring=string.h但!=string

using namespace std;

int main(){

    char str1[11],str2[11];

    int type;

    cin>>str1;//cin输入的是不含空格的字符串

    cin>>str2;

    if(strlen(str1)!=strlen(str2)) cout<<"1";

    else{

        if(!strcmp(str1,str2)) cout<<"2";

        else if(!strcmpi(str1,str2)) cout<<"3";

        else cout<<"4";

    }

    cout<<endl;

    return 0;

}         

11.swap是用于交换两个变量的值的

需要使用#include<algorithm>

导入algorithm头文件才可以使用。

这个函数在交换两个Int 类型的变量的时候,可以这样写:

int x = 10, y = 20;                         // x:10 y:20
swap(x, y);

经过上面的操作后,x就等于20,y就等于10了

这个就是swap的用法

 

 

swap()交换string型字符串中的两个不同位置字符

12.c++ string怎样判断字符串里面是否含有某个字符串?

例如:string str="afdsdfs_hello_sdfas#@!";
怎样判断str里面是否含有“hello",,谢谢
 
使用 string 的 find 成员函数。 
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "afdsdfs_hello_sdfas#@!";
string str1 = "hello";

string::size_type idx = str.find( str1 );

if ( idx != string::npos )
{
cout << "字符串含有“<< str1 << "\n";
}
else
{
cout << "字符串没有" << str1 << "\n";
}
}
解析:string::npos是个返回值

string 类提供了 6 种查找函数,每种函数以不同形式的 find 命名。
这些操作全都返回 string::size_type 类型的值,以下标形式标记查找匹配所发生的位置;或者返回一个名为 string::npos 的特殊值(它说明查找没有匹配的)。string 类将 npos 定义为保证大于任何有效下标的值。

所以 当 str.find("哦")==string::npos时则说明字符串str中不存在“哦”这个字符,
反之,str.find("哦")!=string::npos则说明字符串str中存在“哦”这个字符

法二 strstr()函数

包含文件:string.h

函数名: strstr

函数原型:

     extern char *strstr(char *str1, const char *str2);

语法:

* strstr(str1,str2)

str1: 被查找目标 string expression to search.

str2: 要查找对象 The string expression to find.

返回值:若str2是str1的子串,则返回str2在str1的首次出现的地址;如果str2不是str1的子串,则返回NULL。

 

#include <iostream>

#include <string>

#include <string.h>

using namespace std;

int main()

{

char a[]="12345";

char b[]="123";

if (  strstr(a,b)!=NULL )

{

cout << "字符串含有"<< "\n";

}

else

{

cout << "字符不串有"<< "\n";

}

}

 

 

 

13.C++位运算

#include <stdio.h>
main()
{
 int a=3;
 int b = 5;
 printf("%d",a&b);
}

#include <stdio.h>
main()
{
 int a=060;
 int b = 017;
 printf("%d",a|b);
}

#include <stdio.h>
main()
{
 int a=071;
 int b = 052;
 printf("%d",a^b);
}

#include <stdio.h>
main()
{
 int a=071;
 int b = 052;
 printf("%d",a^b);
}

#include <stdio.h>
main()
{
 int a=077;
 printf("%d",~a);
}

14

#include<iostream>

using namespace std;

void f(char a[])

{

      cout<<a[1]<<endl;

}

void m(char *a)

{

      cout<<a[1]<<endl;

}

int main()

{

      char *a={"adff"};

      char b[]={"aaaaagaaaaaaaa"};

      f(a);

      f(b);

      m(a);

      m(b);

      return 0;

}

15.itoa(num,arr,10)在stdlib.h里:int转char[]

atoi()

#include <stdio.h>

#include <stdlib.h>

int main ()

{

    int i;

    char buffer[256];

    printf ("Enter a number: ");

    scanf("%s",&buffer);

    i = atoi (buffer);

    printf ("The value entered is %d.", i);

    return 0;

}

16.strcat()连接两个字符串

 

posted on 2018-03-28 07:57  兔子子子  阅读(1154)  评论(0编辑  收藏  举报