實例html
#include <iostream> using namespace std; int main () { // 數字定義 short s; int i; long l; float f; //32位 double d; //64位 // 數字賦值 s = 10; i = 1000; l = 1000000; f = 230.47; d = 30949.374; // 數字輸出 cout << "short s :" << s << endl; cout << "int i :" << i << endl; cout << "long l :" << l << endl; cout << "float f :" << f << endl; cout << "double d :" << d << endl; return 0; }
數字運算相關的內置函數:使用的時候先要導入頭文件<cmath>ios
序號 | 函數 & 描述 |
---|---|
1 | double cos(double); 該函數返回弧度角(double 型)的餘弦。 |
2 | double sin(double); 該函數返回弧度角(double 型)的正弦。 |
3 | double tan(double); 該函數返回弧度角(double 型)的正切。 |
4 | double log(double); 該函數返回參數的天然對數。 |
5 | double pow(double, double); 假設第一個參數爲 x,第二個參數爲 y,則該函數返回 x 的 y 次方。 |
6 | double hypot(double, double); 該函數返回兩個參數的平方總和的平方根,也就是說,參數爲一個直角三角形的兩個直角邊,函數會返回斜邊的長度。 |
7 | double sqrt(double); 該函數返回參數的平方根。 |
8 | int abs(int); 該函數返回整數的絕對值。 |
9 | double fabs(double); 該函數返回任意一個十進制數的絕對值。 |
10 | double floor(double); 該函數返回一個小於或等於傳入參數的最大整數。 |
實例:c++
#include <iostream> #include <cmath> using namespace std; int main () { // 數字定義 short s = 10; int i = -1000; long l = 100000; float f = 230.47; double d = 200.374; // 數學運算 cout << "sin(d) :" << sin(d) << endl; //-634939 正弦 cout << "abs(i) :" << abs(i) << endl; //1000 絕對值 cout << "floor(d) :" << floor(d) << endl; //200 求最大整數 cout << "sqrt(f) :" << sqrt(f) << endl; //15.1812 cout << "pow( d, 2) :" << pow(d, 2) << endl; //401497.7 次方運算 return 0; }
補充隨機數:srand+rand數組
#include <iostream> //輸入輸出流頭文件 #include <ctime> //時間相關的頭文件 #include <cstdlib> //系統時間相關的頭文件 using namespace std; int main () { int i,j; // 設置種子 srand( (unsigned)time( NULL ) ); /* srand函數是隨機數發生器的初始化函數。原型: void srand(unsigned seed);用法:它須要提供一個種子,這個種子會對應一個隨機數,若是使用相同的種子後面的rand()函數會出現同樣的隨機數。如: srand(1); 直接使用 1 來初始化種子。不過爲了防止隨機數每次重複,經常使用系統時間來初始化,即便用 time 函數來得到系統時間生成 10 個隨機數 */ for( i = 0; i < 10; i++ ) { // 生成實際的隨機數 j= rand(); cout <<"隨機數: " << j << endl; } return 0; }
補充獲取必定範圍內的隨機數:數據結構
#include <iostream> #include<stdio.h> #include<time.h> #define random(x)(rand()%x) //須要提早聲明一下random函數才能用,函數方法是求餘數 using namespace std; int main() { srand((int)time(0));//部署隨機種子 for (int i = 0; i < 10; i++){ cout << random(100) << endl; //求0-100內的隨機數就是把系統經常使用的求隨機數的方法除以100而後取餘數 //輸出0-100的隨機數 }; return 0; }
C++ 支持數組數據結構,它能夠存儲一個固定大小的相同類型元素的順序集合。數組是用來存儲一系列數據,但它每每被認爲是一系列相同類型的變量。app
聲明數組:dom
double balance[10]; //聲明一個包含10個double元素的數組balance,balance 是一個可用的數組,能夠容納 10 個類型爲 double 的數字。
初始化:函數
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0}; //大括號 { } 之間的值的數目不能大於咱們在數組聲明時在方括號 [ ] 中指定的元素數目,固然方括號內也能夠不加數字,此時系統會自動添加上元素的初始化時候的個數,如:double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};和上面的例子是同樣的
爲某個元素賦值:spa
balance[4] = 51;
賦值和獲取數組內的元素實例:指針
#include <iostream> using namespace std; int main () { int n[ 10 ]; // n 是一個包含 10 個整數的數組 // 初始化數組元素 for ( int i = 0; i < 10; i++ ) { n[ i ] = i + 100; // 設置元素 i 爲 i + 100 } cout << "Element" << "Value" << endl; // 輸出數組中每一個元素的值 for ( int j = 0; j < 10; j++ ) { cout << j << n[ j ] << endl; } return 0; }
補充:
多維數組:
初始化(以二維數組爲例):
int a[3][4] = { {0, 1, 2, 3} , /* 初始化索引號爲 0 的行 */ {4, 5, 6, 7} , /* 初始化索引號爲 1 的行 */ {8, 9, 10, 11} /* 初始化索引號爲 2 的行 */ };
//如下也是能夠的:int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
訪問二維數組:
#include <iostream> using namespace std; int main () { // 一個帶有 5 行 2 列的數組 int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; // 輸出數組中每一個元素的值 ,直接全局遍歷一遍好了 for ( int i = 0; i < 5; i++ ) for ( int j = 0; j < 2; j++ ) { cout << "a[" << i << "][" << j << "]: "; cout << a[i][j]<< endl; } return 0; }
數組詳解看2-4篇介紹
c風格的字符串:
本質上是一堆字符數組,存儲在數組中,由於用null‘\0’結尾,因此數組比普通的數組長1,
聲明和初始化:
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; char greeting[] = "Hello";
字符串相關操做:
序號 | 函數 & 目的 |
---|---|
1 | strcpy(s1, s2); 複製字符串 s2 到字符串 s1。 |
2 | strcat(s1, s2); 鏈接字符串 s2 到字符串 s1 的末尾。 |
3 | strlen(s1); 返回字符串 s1 的長度。 |
4 | strcmp(s1, s2); 若是 s1 和 s2 是相同的,則返回 0;若是 s1<s2 則返回值小於 0;若是 s1>s2 則返回值大於 0。 |
5 | strchr(s1, ch); 返回一個指針,指向字符串 s1 中字符 ch 的第一次出現的位置。 |
6 | strstr(s1, s2); 返回一個指針,指向字符串 s1 中字符串 s2 的第一次出現的位置。 |
c++標準庫提供的string類:須要引入頭文件<string>
#include <iostream> #include <string> using namespace std; int main () { string str1 = "Hello"; string str2 = "World"; string str3; //聲明可是沒有初始化 int len ; // 複製 str1 到 str3 str3 = str1; cout << "str3 : " << str3 << endl; //World // 鏈接 str1 和 str2 str3 = str1 + str2; //HelloWorld cout << "str1 + str2 : " << str3 << endl; // 鏈接後,str3 的總長度 len = str3.size(); cout << "str3.size() : " << len << endl; //10 return 0; }
string其餘的可用函數:
實例:
#include <iostream> #include <string> using namespace std; int main() { //定義一個string類對象 string http = "www.runoob.com"; //打印字符串長度 cout<<http.length()<<endl; //拼接 http.append("/C++"); cout<<http<<endl; //打印結果爲:www.runoob.com/C++ //刪除 int pos = http.find("/C++"); //查找"C++"在字符串中的位置 cout<<pos<<endl; http.replace(pos, 4, ""); //從位置pos開始,以後的4個字符替換爲空,即刪除 cout<<http<<endl; //找子串runoob int first = http.find_first_of("."); //從頭開始尋找字符'.'的位置 int last = http.find_last_of("."); //從尾開始尋找字符'.'的位置 cout<<http.substr(first+1, last-first-1)<<endl; //提取"runoob"子串並打印 return 0; }