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;
}