1.length()函數
只用來獲取字符串的長度。
變量名.length()
2.size()函數
既可以獲取字符串的長度也可以獲取vector類型的長度。
變量名.size()
3.sizeof()運算符
用來求對象所佔內存空間的大小。
sizeof(變量名)
4.計時函數clock()
獲取程序運行的時間。
#include <ctime>
...
double s=clock();
...
printf("Time used=%.2f\n",(double)(clock()-s)/CLOCKS_PER_SEC);
return 0;
除以常數CLOCKS_PER_SEC,時間以秒爲單位。
5.floor()函數
用於四捨五入。
floor(x+0.5)
6.變量交換
交換兩個整型、浮點型和字符型變量。
a^=b^=a^=b;
7.system(“pause”)
不讓程序“按任意鍵退出”
8.memset()函數
清零數組。
#include <string.h>
...
memset(a,0,sizeof(a));//清零數組a。
9.memcpy()函數
從數組複製元素到數組b。
#include <string.h>
...
memcpy(b,a,sizeof(a));//全部複製
...
mencpy(b,a,sizeof(int)*k);//複製k個原素
10.sort()函數
實現數組排序
#include <algorithm>
...
int a[100];
...
sort(a,a+100,less<int>());//從小到大排序
sort(a,a+100,greater<int>());//從大到小排序
11.C++11 raw string literal 技術
直接複製粘貼輸出字符
#include <iostream>
using namespace std;
...
cout<<R"(//複製粘貼字符串)";
12.64位編譯器的各種整型範圍
unsigned int:0~4294967295(0~2^32-1)
(10^10)
int:-32768~32767(-2^15 2^15-1)
(10^5)
unsigned long:0~4294967295(0~2^32-1)
(10^10)
long:-2147483648~2147483647(-2^31 - 2^31-1)
(10^10)
long long:-9223372036854775808-9223372036854775807(-2^63 -2^63-1) (10^19)
unsigned long long:0-18446744073709551615(0~2^64-1)
(10^20)
13.reverse()函數
翻轉數組中的元素
#include <iostream>
using namespace std;
#include <algorithm>
int a[5]={1,2,3,4,5};
...
reverse(a,a+5);
...
14.數值轉字符串
方法一:
string to_string (int val);
string to_string (double val);
例如:
string s = to_string(123);
方法二:
stringstream (使用stringstream 需包含頭文件 “sstream”)
例如:
#include <sstream>
...
int a = 123, b = 456, c = 789;
stringstream ss;
ss << a << b << c;
...
注意:stringstream型不能直接輸出,也不支持迭代器,需要利用stringstream類的成員函數str()才能把它當成普通字符串來用,例如:
string s = ss.str();
字符串轉爲數值也可以用stringstream
例如:
int i;
ss >> i;
方法三:
此方法在oj平臺無法使用
使用itoa()函數
函數原型 char *itoa( int value, char *string,int radix);
第一個參數是要轉換的數字,第二個參數是要寫入轉換結果的目標字符串,第三個參數是轉換數字時所用的基數。
例如:
#include <stdlib.h>
...
int num = 10;
char str[100];
itoa(num, str, 8); //將10轉化爲八進制存入字符數組
字符轉回數值:
例如:將十進制轉化爲二進制
int n = atoi(itoa(num, str, 2)); //先把num轉換爲二進制的字符串,再把該字符串轉換爲整數
15.count()函數
count(ivec.begin() , ivec.end() , searchValue);
作用:
統計在一定範圍內某一值出現的次數
前兩個參數爲起始位置和結束位置,都是迭代器,第三個參數爲需要統計的值
例如:
count(s.begin(), s.end(), x + '0');
16.malloc()函數
函數原型:void *malloc(unsigned int size)
作用:在內存的動態存儲區中分配一個長度爲size的連續空間。
參數:內存塊的大小,以字節爲單位。
返回值:
成功時,指向函數分配的內存塊的指針。
該指針的類型始終爲void*,可以將其強制轉換爲所需的數據指針類型,以便將其取消引用。
如果函數未能分配所請求的內存塊,則返回空指針。
/* malloc example: random string generator*/
#include <iostream>
using namespace std;
#include <stdlib.h> /* malloc, free, rand */
int main()
{
int i, n;
char* buffer;
cout << "How long do you want the string? "<<endl;
cin >> i;
buffer = (char*)malloc(i+1);//申請空間,並進行類型轉換
if (buffer == NULL) exit(1);
for (n = 0; n < i; n++)
buffer[n] = rand() % 26 + 'a';
buffer[i] = '\0';
cout<<"Random string:"<< buffer;
free(buffer);//釋放空間
return 0;
}
該程序生成用戶指定長度的字符串,並用字母字符填充。
17.二分查找函數
lower_bound(起始地址,結束地址,要查找的數值) 返回的是數值 第一個 出現的位置。
upper_bound(起始地址,結束地址,要查找的數值) 返回的是數值 最後一個 出現的位置。
binary_search(起始地址,結束地址,要查找的數值) 返回的是是否存在這麼一個數,是一個bool值。
18.decltype類型指示符
decltype(f()) sum = x; //sum
的類型就是函數f的返回類型
19.範圍for()語句
for(declaration : expression)
statement
作用:遍歷給定序列中的每個元素並對序列中的每個值執行某種操作
string str("hello world!");
for(auto c : str)
cout<<c<<endl; //每行輸出str中的一個字符
20.strncmp()函數
函數聲明:int strncmp(const char *str1, const char *str2, size_t n)
參數:
str1 – 要進行比較的第一個字符串。
str2 – 要進行比較的第二個字符串。
n – 要比較的最大字符數。
返回值:
如果返回值 < 0,則表示 str1 小於 str2。
如果返回值 > 0,則表示 str2 小於 str1。
如果返回值 = 0,則表示 str1 等於 str2。