c++學習筆記(4)
1. 一維陣列和C字串:數字是用來儲存相同型別變數的資料集合
c++要求在宣告陣列時,陣列的大小是常量:
int size=4;
double numbers[size]; // wrong
const int size=4;
double numbers[size]; // correct
2. 陣列初始化語句
double number[] = {1.1, 3, 4, 9.6}
必須在一條語句內完成宣告和初始化
注 c++ 中陣列複製,需要在兩個陣列之間逐個元素進行復制:
for(int i=0; i<size; i++)
{
a[i] = b[i];
}
3.實現陣列reshuffle
方法:對於陣列元素numbers[i], 隨機產生一個下標 j (0 <= j <= i ), 交換 numbers[i] 和 numbers[j] 的順序:
#include <iostream> #include <cstdlib> #include <time.h> using namespace std; const int SIZE=10; int main(int argc, char *argv[]) { srand(time(0)); int numbers[SIZE] = {0,1,2,3,4,5,6,7,8,9}; // 實現陣列reshuffle for(int i=SIZE-1; i>0; i--) { int j = rand()%(i+1); // generator a random number between 0~i int tmp = numbers[i]; numbers[i] = numbers[j]; numbers[j] = tmp; } for(int i=0; i<SIZE; i++) { cout << numbers[i] << " "; } cout << endl; return 0; }
string month[ ] = {"January" , " Feburary", ......., "December"}
5. 一副紙牌
思路: 陣列實現
int cards[52];
分組: 0-12 , 13-25, 26-38, 39-51 (spade, heart, diamond, club)
使用string suit[ ] ={"spade", ......}
程式碼:
#include <iostream> #include <ctime> #include <string> using namespace std; const int SIZE = 52; // number of cards; int main(int argc, char *argv[]) { srand(time(0)); int cards[SIZE]; string suits[] = {"spade", "heart", "diamond", "club"}; string ranks[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}; // 復初值 for(int i=0; i<SIZE; i++) { cards[i] = i; } for(int i=SIZE-1; i>0; i--) // reshuffle { int j= rand() % (i+1); // generate a random index between 0 and i int tmp = cards[i]; cards[i] = cards[j]; cards[j] = tmp; } for(int i=0; i<SIZE; i++) { cout << cards[i] << " "; if(i%10 == 0) cout << endl; } cout << endl; for(int i=0; i<4; i++) { string suit = suits[cards[i]/13]; string rank = ranks[cards[i]%13]; cout << "The selected card is " << suit << " " << rank << endl; } return 0; }
執行結果:
4.陣列作為函式引數
當一個數組引數傳遞給函式,陣列的起始地址被傳遞給了函式中的陣列引數。實際引數和形式引數使用的是同一個陣列。
5.防止函式修改傳遞引數的陣列:可以將陣列引數宣告為const型別的陣列
6. 陣列作為函式的返回值
c++不允許直接返回陣列,可以向函式傳遞兩個陣列
void reverse(int numbers[ ], int newNumbers[ ] , int SIZE)
{
..............
}
陣列逆序實現:
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
void printList(int numbers[], int size)
{
for(int i=0; i<size; i++)
{
cout << numbers[i] << " ";
}
cout << endl;
}
void reverseList(const int numbers[], int newList[], int size)
{
for(int i=0; i<size; i++)
{
newList[size-1-i] = numbers[i];
}
}
int main(int argc, char *argv[])
{
int num[5] = {1, 2, 3, 4, 5};
int newlist[5];
reverseList(num, newlist, 5);
printList(newlist, 5);
return 0;
}
另一種實現:
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
// 這種方式只用傳遞一個數組作為引數,但是原陣列會被改變
void reverseList(int numbers[], int size)
{
for(int i=0; i<size/2; i++)
{
int tmp = numbers[i];
numbers[i] = numbers[size-1-i];
numbers[size-1-i] = tmp;
}
}
void printList(int numbers[], int size)
{
for(int i=0; i<size; i++)
{
cout << numbers[i] << " ";
}
cout << endl;
}
int main(int argc, char *argv[])
{
int num[5] = {1, 2, 3, 4, 5};
reverseList(num, 5);
printList(num, 5);
return 0;
}
搜尋陣列:順序搜尋和二分搜尋
#include <iostream>
#include <ctime>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
int binarySearch(int [], int, int);
int main(int argc, char *argv[])
{
int test[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int tmp = binarySearch(test, 10, 6);
if(tmp==-1)
{
cout << "Not Found" << endl;
}
else
{
cout << "The index is " << tmp << endl;
}
return 0;
}
int binarySearch(int list[], int listSize, int key)
{
int low = 0;
int high = listSize - 1;
int mid = listSize / 2;
while(low <= high)
{
if(key<list[mid])
high = mid - 1;
else if(key>list[mid])
low = mid + 1;
else
return mid;
mid = (low + high) / 2;
}
return -1;
}
陣列排序+二分還查詢+逆序排列:
#include <iostream>
#include <ctime>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
int binarySearch(int [], int, int);
void select_sorted(int [], int); //選擇排序
void printList(int [], int);
void reverseList(int [], int);
int main(int argc, char *argv[])
{
int test[10];
cout << "Enter ten numbers: " << endl;
for(int i=0; i<10; i++)
{
cout << "Enter: ";
cin >> test[i];
}
cout << "Yuo Enter a list: " << endl;
printList(test, 10);
select_sorted(test, 10); // 對陣列進行排序
cout << "The list after sorted is: " << endl;
printList(test, 10);
reverseList(test, 10);
cout << "The list after reverse: " << endl;
printList(test, 10);
cout << "Enter the number you want to find: ";
int num;
cin >> num;
int index = binarySearch(test, 10, num);
if(index == -1)
{
cout << "Not found in the list!" << endl;
}
else
{
cout << "The index is " << index << endl;
}
return 0;
}
int binarySearch(int list[], int listSize, int key)
{
int low = 0;
int high = listSize - 1;
int mid = listSize / 2;
while(low <= high)
{
if(key<list[mid])
high = mid - 1;
else if(key>list[mid])
low = mid + 1;
else
return mid;
mid = (low + high) / 2;
}
return -1;
}
// 函式過載
//double binarySearch(double list[], int listSize, double key)
void select_sorted(int list[], int listSize)
{
/* 選擇排序*/
//int min_number = list[0]; 寫到這裡你是錯誤的
//int min_number_index = 0;
for(int i=0; i<listSize-1; i++)
{
int min_number = list[i];
int min_number_index = i;
for(int j=i; j<listSize; j++)
{
if(min_number > list[j])
{
min_number = list[j];
min_number_index = j;
}
}
int tmp = list[i];
list[i] = list[min_number_index];
list[min_number_index] = tmp;
}
}
void reverseList(int list[], int size)
{
for(int i=0; i<size/2; i++)
{
int tmp = list[i];
list[i] = list[size-i-1];
list[size-i-1] = tmp;
}
}
void printList(int list[], int size)
{
for(int i=0; i<size; i++)
{
cout << list[i] << " ";
}
cout << endl;
}
C字串函式s
<cstring> 中的函式
strlen()
strcpy()
strncpy()
strcat()
strncat()
strcmp()
strncmp()
cstdlib()中的函式
int atoi() 返回字串對應的int值
float atof()
double atol()
itoa()將數子轉化為字串
6. 字串和數字之間的轉化
#include <iostream>
#include <ctime>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[])
{
// 使用atoi將字串轉化為int
char s1[] = "250";
char s2[] = "100";
int num = atoi(s1) + atoi(s2);
cout << "The number is " << num << endl;
char s3[] = "20.78";
char s4[] = "90.33";
double num1 = atof(s3) + atof(s4);
cout << "THE number is " << num1 << endl;
// 使用itoa()函式將整數轉化為字串:
char ss1[15];
char ss2[15];
char ss3[15];
itoa(100, ss1, 16); // 將100按16進位制轉化為字串存到ss1中
itoa(100, ss2, 2);
itoa(100, ss3, 10);
cout << "The hex for 100 is " << ss1 << endl;
cout << "The binary for 100 is " << ss2 << endl;
cout << "The decimal for 100 is " << ss3 << endl;
return 0;
}