高级数据类型 Advanced Data
Advanced Data
- 数组 Arrays
- 字符序列 Characters Sequences
- 指针 Points
- 动态内存分配
- 数据结构 Data Structures
- 自定义数据类型
Array
type 为任何 object type
// type name [elements];
int a[5];
int b[5] = {16, 2, 77, 40, 1201};
int c[] = {16, 2, 77, 40, 1201}; //长度由后面数据决定
多维数组 Multidimensional Arrays
int a[3][5];
char century [100][365][24][60][60];
字符序列
char mystring [ ] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char mystring [ ] = "Hello";
输入:
#include <string.h>
#include <iostream>
using namespace std;
signed main() {
char str[20];
strcpy(str, "Hello.");
cout << str;
}
输出:
Hello.
字符串转Type
#include <stdlib.h>
atoi | string to int |
---|---|
atol | string to long |
atof | string float |
#include <iostream>
#include <stdlib.h>
using namespace std;
int main () {
char mybuffer [100];
float price;
int quantity;
cout << "Enter price: ";
cin.getline (mybuffer,100);
price = atof (mybuffer);
cout << "Enter quantity: ";
cin.getline (mybuffer,100);
quantity = atoi (mybuffer);
cout << "Total price: " << price*quantity;
return 0;
}
Enter price: 2.75
Enter quantity: 21
Total price: 57.75
指针 Point
#include <iostream>
using namespace std;
int main() {
int a, *p;
cin >> a;
p = &a;
cout << *p;
}
动态内存分配 Dynamic memory
new
pointer = new type [elements];
例:
int * b;
b = new int[5]; //开 5 个整型元素合法内存空间
数组的长度必须是一个常量,而采用动态内存分配,数组的长度可以常量或变量