C++编程:多维数组、字符串、引用与类关系详解
立即解锁
发布时间: 2025-08-20 01:44:35 阅读量: 1 订阅数: 3 


C++编程入门:从零基础到游戏开发
### C++编程:多维数组、字符串、引用与类关系详解
#### 1. 多维数组
多维数组是C++中处理复杂数据结构的重要工具。以下代码展示了一个二维数组的示例:
```cpp
#include <iostream>
int main(void)
{
using std::cout;
int numbers[10][10];
for (int i = 0; i < 10; i++)
for (int c = 0; c < 10; c++)
cout << (numbers[i][c] = (i * 10) + c) << "\n";
return 0;
}
```
此代码创建了一个10x10的二维数组`numbers`,并将每个元素初始化为`(i * 10) + c`的形式,最终输出从0到99的数字。
#### 2. 字符串再探
在C++中,字符串的处理方式有多种。
##### 2.1 字符串字面量
字符串字面量(如 “Hello”)的官方类型是`const char []`。以 “Hello” 为例,它的类型是`const char [6]`,多出来的一个字符是终止空字符`\0`,用于告知计算机字符串的长度。
```cpp
char* x = "Hello";
```
这里将字符串字面量赋值给`char*`类型的变量`x`,但不能修改`x`所指向的值。若要修改,需将字符串字面量赋值给字符串对象或字符数组。
```cpp
char s[] = "Hello";
s[0] = 'S';
```
上述代码将字符数组`s`的第一个字符修改为`'S'`,新的字符串值为 “Sello”。
##### 2.2 字符数组
字符数组是表示字符串的另一种方式,可以使用字符串字面量初始化字符数组。
```cpp
char s[] = "Hello World!";
```
这种方式声明字符串几乎和使用标准库中的`string`类一样方便,C++会为数组分配足够的空间来存储字符串末尾的空字符。
##### 2.3 确定字符串长度
可以使用`sizeof`运算符或`strlen()`函数来确定字符串的长度。
```cpp
#include <iostream>
#include <cstring>
int main() {
char s[] = "Hello World!";
std::cout << sizeof(s) << std::endl; // 输出包含空字符的长度
std::cout << strlen(s) << std::endl; // 输出不包含空字符的长度
return 0;
}
```
`sizeof`运算符会包含空字符,而`strlen()`函数通过计数到第一个空字符来确定字符串的长度。
##### 2.4 使用其他C风格字符串函数
标准库中还有许多C风格的字符串函数,如`strcpy()`、`strcat()`和`strncpy()`。
- **strcpy()**:用于复制字符串。
```cpp
#include <iostream>
#include <cstring>
int main() {
char s[6];
char t[] = "Hello";
std::cout << strcpy(s, t) << std::endl;
return 0;
}
```
- **strcat()**:用于连接字符串。
```cpp
#include <iostream>
#include <cstring>
int main() {
char s[12] = "Hello";
std::cout << strcat(s, " World") << std::endl;
return 0;
}
```
- **strncpy()**:用于复制指定数量的字符。
```cpp
#include <iostream>
#include <cstring>
int main() {
char s [7]= "Say ";
char t[]= "Hi";
std::cout << strncpy(s, t, 2) << std::endl;
return 0;
}
```
##### 2.5 字符串转换为数字
可以使用`<cstdlib>`中的函数将包含数值的字符串转换为相应的数值类型。
- **atoi()**:将字符串表示的整数转换为整数。
```cpp
#include <iostream>
#include <cstdlib>
int main() {
char s[] = "567";
int x = atoi(s) + 3;
std::cout << x << std::endl;
return 0;
}
```
- **atof()**:将字符串转换为双精度浮点数。
- **atol()**:将字符串转换为长整数。
#### 3. 引用入门
引用是变量的别名,可以将其看作是一个始终被解引用的常量指针。引用必须在声明时初始化,且初始化后其值不能改变。
```cpp
int x;
int& rx = x;
rx++; // 实际上是x++
```
引用主要用作函数参数和返回类型。
##### 3.1 在函数参数中使用引用
可以将函数参数声明为引用,以便函数能够修改传入的参数。
```cpp
#include <iostream>
void decrement(int& x) {
x--;
}
int main() {
int a = 5;
decrement(a);
std::cout << a << std::endl;
return 0;
}
```
##### 3.2 作为函数返回值使用引用
函数可以返回对变量的引用,但需要注意其影响。
```cpp
#include <iostream>
class Point {
int X, Y;
public:
Point(int lX, int lY) : X(lX), Y(lY) {}
int& GetX() { return X; }
int& GetY() { return Y; }
};
int main() {
using std::cout;
Point p(5, 3);
p.GetX() = 3;
p.GetY() = 5;
cout << p.GetX() << p.GetY() << "\n";
return 0;
}
```
#### 4. 重新创建井字棋游戏
以下是一个简单的井字棋游戏的实现:
```cpp
#include <iostream>
#include <string>
using std::string;
enum SquareState { blank = ' ', X = 'X', O = 'O' };
class GameBoard {
private:
const int WIDTH;
const int HEIGHT;
int* gameBoard;
public:
GameBoard() : WIDTH(3), HEIGHT(3) {
gameBoard = new int[9];
for (int i = 0; i < 9; i++)
*(gameBoard + i) = blank;
}
~GameBoard() { delete[] gameBoard; }
void setX(int h, int w);
void setO(int h, int w);
bool isTaken(int h, int w);
SquareState isLine();
void draw();
};
void GameBoard::setX(int h, int w) {
*(gameBoard + h * HEIGHT + w) = X;
}
void GameBoard::setO(int h, int w) {
*(gameBoard + h * HEIGHT + w) = O;
}
bool GameBoard::isTaken(int h, int w) {
return *(gameBoard + h * HEIGHT + w) != ' ';
}
SquareState GameBoard::isLine() {
if (*gameBoard == X && *(gameBoard + 1) == X && *(gameBoard + 2) == X)
return X;
if (*gameBoard == O && *(gameBoar
```
0
0
复制全文
相关推荐









