目錄
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()連接兩個字符串
