【C++】<图形库> 三人成棋(面向对象写法)

2024-05-26 05:20

本文主要是介绍【C++】<图形库> 三人成棋(面向对象写法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 目录

一、游戏需求

二、程序架构

三、代码实现

四、实现效果

五、已知BUG


一、游戏需求

构建一个五子棋游戏,在自定义棋盘宽度和高度的基础上,实现三人对战功能,并且能判定谁输谁赢。


二、程序架构

(1) 对象分析:

【1】 需要一个棋盘(ChessBoard)类来绘制棋盘。

【2】有三人对战,用白棋、黑棋和黄棋区分。因此,需要构建白棋玩家、黑棋玩家和黄棋玩家。另外,每个玩家下的棋子用vector容器来装。每个玩家还应该具有判定自己是否赢得比赛的方法。由于vector容器和相关方法只有略微不同,可以先构建一个玩家基类(Player),再派生出白棋(WhitePlayer)、黑棋(BlackPlayer)和黄棋(YellowPlayer)玩家类。

【3】需要创建棋子类(ChessPiece),有坐标和颜色属性。当不同的玩家落子时,白棋、黑棋和黄棋玩家类会创建各自颜色的棋子。

【4】玩家根据鼠标点击来落子,因此需要一个鼠标类(Mouse)来返回鼠标信息。

【5】菜单类(Menu)将主要的逻辑封装好,便于在main()中调用。

(2) 文件构成:

有main.cpp、ChessBoard.cpp、Player.cpp(将涉及玩家的类都放于此)、ChessPiece.cpp、Mouse.cpp、Menu.cpp,还有对应的头文件。


三、代码实现

【1】main.cpp:

#include <iostream>
#include <graphics.h>
#include "ChessBoard.h"
#include "Menu.h"
#include "Mouse.h"
#include "Player.h"/***************************************************************说明:【1】若使用win11系统,需修改相关设置才能正常运行*	        设置-->系统-->开发者选项-->终端-->windows控制台主机*     【2】在创建棋盘对象时,可以自定义棋盘的宽度和高度************************************************************/int main()
{//创建相关对象ChessBoard board;	//棋盘对象Menu menu;			//菜单对象Mouse mouse;		//鼠标对象WhitePlayer wp;		//白棋玩家BlackPlayer bp;		//黑棋玩家YellowPlayer yp;	//黄棋玩家while (1) {//显示主交互界面int choice = menu.mainInterface();//根据choice显示不同界面switch (choice) {case '1'://游戏对战menu.startGame(mouse, board, wp, bp, yp);break;case '2'://游戏介绍menu.introInterface();break;case '0'://退出游戏return 0;}}return 0;
}

【2】Player.h:

#pragma once
#include <vector>
#include <algorithm>
#include "ChessPiece.h"
#include "Mouse.h"
#include "ChessBoard.h"/************************************************************ @类名:	Player* @摘要:	玩家基类(抽象类)* @作者:	柯同学* @注意:	派生类必须重写generatePiece方法*********************************************************/
class Player{
private:std::vector<ChessPiece> pieces;	//保存棋子的动态数组public:virtual ~Player() {}			//虚析构函数:防止内存泄漏std::vector<ChessPiece>& getPieces();	//返回棋子动态数组引用virtual void generatePiece(Mouse& mouse, Player& p1, Player& p2) = 0;	//纯虚函数:玩家落子bool isWin(ChessBoard& board);	//判断当前玩家是否赢
};/************************************************************ @类名:	WhitePlayer* @摘要:	白玩家类(继承Player)* @作者:	柯同学* @注意:	generatePiece下白棋*********************************************************/
class BlackPlayer;
class YellowPlayer;
class WhitePlayer : public Player{
public:void generatePiece(Mouse& mouse, Player& p1, Player& p2) override;
};/************************************************************ @类名:	BlackPlayer* @摘要:	黑玩家类(继承Player)* @作者:	柯同学* @注意:	generatePiece下黑棋*********************************************************/
class BlackPlayer : public Player {
public:void generatePiece(Mouse& mouse, Player& p1, Player& p2) override;
};/************************************************************ @类名:	YellowPlayer* @摘要:	黄玩家类(继承Player)* @作者:	柯同学* @注意:	generatePiece下黄棋*********************************************************/
class YellowPlayer : public Player {
public:void generatePiece(Mouse& mouse, Player& p1, Player& p2) override;
};

【3】Player.cpp:

#include "Player.h"/************************************************************ @函数名:Player::getPieces* @功  能:返回棋子动态数组的引用* @参  数:无* @返回值:棋子动态数组的引用*********************************************************/
std::vector<ChessPiece>& Player::getPieces() {return this->pieces; 
}/************************************************************ @函数名:Player::isWin* @功  能:判断当前玩家是否赢得游戏* @参  数:board---棋盘对象* @返回值:true---赢得游戏,false---没赢游戏*********************************************************/
bool Player::isWin(ChessBoard& board) {/* 得到最近一次下的棋子 */ChessPiece lastChess;if (pieces.size() > 0) {lastChess = pieces[pieces.size() - 1];}/* 获胜情况1:左右五连 */int leftWinFlag = 0;//胜利标志:4则赢std::vector<ChessPiece>::iterator p;for (int i = -4; i <= 4; ++i) {//越界则跳过if ((lastChess.getX() + i * BOARD_INTERVAL < 0) ||(lastChess.getX() + i * BOARD_INTERVAL > board.getWidth())) {continue;}//查找连续相连的棋子ChessPiece targetChess(lastChess.getX() + i * BOARD_INTERVAL, lastChess.getY(), lastChess.getColor());p = std::find(pieces.begin(), pieces.end(), targetChess);if (p != pieces.end()) {leftWinFlag++;}else {leftWinFlag = 0;}//胜利if (leftWinFlag >= 5)return true;}/* 获胜情况2:上下五连 */int upWinFlag = 0;for (int i = -4; i <= 4; ++i) {//越界则跳过if ((lastChess.getY() + i * BOARD_INTERVAL < 0) ||(lastChess.getY() + i * BOARD_INTERVAL > board.getWidth())) {continue;}//查找连续相连的棋子ChessPiece targetChess(lastChess.getX(), lastChess.getY() + i * BOARD_INTERVAL, lastChess.getColor());p = std::find(pieces.begin(), pieces.end(), targetChess);if (p != pieces.end()) {upWinFlag++;}else {upWinFlag = 0;}//胜利if (upWinFlag >= 5)return true;}/* 获胜情况3:左上--右下(斜右)五连 */int diarightWinFlag = 0;for (int i = -4; i <= 4; ++i) {//越界则跳过if ((lastChess.getY() + i * BOARD_INTERVAL < 0) ||(lastChess.getY() + i * BOARD_INTERVAL > board.getWidth())) {continue;}//查找连续相连的棋子ChessPiece targetChess(lastChess.getX() + i * BOARD_INTERVAL, lastChess.getY() - i * BOARD_INTERVAL, lastChess.getColor());p = std::find(pieces.begin(), pieces.end(), targetChess);if (p != pieces.end()) {diarightWinFlag++;}else {diarightWinFlag = 0;}//胜利if (diarightWinFlag >= 5)return true;}/* 获胜情况4:左下--右上(斜左)五连 */int dialeftWinFlag = 0;for (int i = -4; i <= 4; ++i) {//越界则跳过if ((lastChess.getY() + i * BOARD_INTERVAL < 0) ||(lastChess.getY() + i * BOARD_INTERVAL > board.getWidth())) {continue;}//查找连续相连的棋子ChessPiece targetChess(lastChess.getX() + i * BOARD_INTERVAL, lastChess.getY() + i * BOARD_INTERVAL, lastChess.getColor());p = std::find(pieces.begin(), pieces.end(), targetChess);if (p != pieces.end()) {dialeftWinFlag++;}else {dialeftWinFlag = 0;}//胜利if (dialeftWinFlag >= 5)return true;}return false;
}/************************************************************ @函数名:createNonDUP(中介函数)* @功  能:生成不重复的棋* @参  数:my---当前对象* @参  数:bplay---黑棋玩家* @参  数:yplay---白棋玩家* @参  数:target---目标棋子* @参  数:mouse---鼠标对象* @返回值:无*********************************************************/
void createNonDUP(Player& my, Player& bplay, Player& yplay, ChessPiece& target, Mouse& mouse) {//查找三个玩家的棋库中是否有target的坐标std::vector<ChessPiece>::iterator wp, bp, yp;wp = find(my.getPieces().begin(), my.getPieces().end(), target);bp = find(bplay.getPieces().begin(), bplay.getPieces().end(), target);yp = find(yplay.getPieces().begin(), yplay.getPieces().end(), target);//不存在重复的棋就加入对应容器,并绘制if (wp == my.getPieces().end() && bp == bplay.getPieces().end()&& yp == yplay.getPieces().end()){my.getPieces().push_back(target);setfillcolor(target.getColor());fillcircle(target.getX(), target.getY(), PIECE_RADIUS);mouse.setClickTotal(mouse.getClickTotal() + 1);}
}/************************************************************ @函数名:WhitePlayer::generatePiece* @功  能:白玩家:下白棋,并将棋子对象加入vector容器中* @参  数:mouse---鼠标对象* @参  数:p1---其他玩家* @参  数:p2---其他玩家* @返回值:无*********************************************************/
void WhitePlayer::generatePiece(Mouse& mouse, Player& p1, Player& p2) {if (mouse.detectClick()) {//创建棋对象,设置颜色为白色ChessPiece cp(mouse.getMouseMesg().x, mouse.getMouseMesg().y, WHITE);cp.fixChessPiece();//生成不重复的棋子,并绘制createNonDUP(*this, p1, p2, cp, mouse);}
}/************************************************************ @函数名:BlackPlayer::generatePiece* @功  能:黑玩家:下黑棋,并将棋子对象加入vector容器中* @参  数:mouse---鼠标对象* @参  数:p1---其他玩家* @参  数:p2---其他玩家* @返回值:无*********************************************************/
void BlackPlayer::generatePiece(Mouse& mouse, Player& p1, Player& p2) {if (mouse.detectClick()) {//创建棋对象,设置颜色为黑色ChessPiece cp(mouse.getMouseMesg().x, mouse.getMouseMesg().y, BLACK);cp.fixChessPiece();//生成不重复的棋子,并绘制createNonDUP(*this, p1, p2, cp, mouse);}
}/************************************************************ @函数名:YellowPlayer::generatePiece* @功  能:黄玩家:下黄棋,并将棋子对象加入vector容器中* @参  数:mouse---鼠标对象* @参  数:p1---其他玩家* @参  数:p2---其他玩家* @返回值:无*********************************************************/
void YellowPlayer::generatePiece(Mouse& mouse, Player& p1, Player& p2) {if (mouse.detectClick()) {//创建棋对象,设置颜色为黄色ChessPiece cp(mouse.getMouseMesg().x, mouse.getMouseMesg().y, YELLOW);cp.fixChessPiece();//生成不重复的棋子,并绘制createNonDUP(*this, p1, p2, cp, mouse);}
}

【4】ChessPiece.h:

#pragma once
#include <graphics.h>
#include "Mouse.h"
#include "ChessBoard.h"
#define PIECE_RADIUS	5	//棋子半径/************************************************************ @类名:	ChessPiece* @摘要:	棋子类* @作者:	柯同学* @注意:	默认棋子为白色*********************************************************/
class ChessPiece {
private:int x;		//横坐标int y;		//纵坐标int color;	//白色、黑色、黄色(分别对应白玩家、黑玩家、黄玩家)
public:ChessPiece(int x = 0, int y = 0, int color = WHITE) : x(x), y(y), color(color) {}bool operator==(const ChessPiece& cp);	//比较两个棋子坐标是否相等void setColor(int color);			//设置棋子颜色void setXY(int x, int y);			//设置棋子坐标int getX() { return x; }			//获取棋子x坐标int getY() { return y; }			//获取棋子y坐标int getColor() { return color; }	//获取棋子颜色void fixChessPiece();				//修正棋子坐标,并绘制棋子
};

【5】ChessPiece.cpp:

#include "ChessPiece.h"/************************************************************ @函数名:operator==* @功  能:==运算符重载:比较两个棋子的坐标是否相等* @参  数:cp---另一个棋子* @返回值:无*********************************************************/
bool ChessPiece::operator==(const ChessPiece& cp) {if (this->x == cp.x && this->y == cp.y)return true;return false;
}/************************************************************ @函数名:setColor* @功  能:设置棋子颜色* @参  数:color---棋子的颜色* @返回值:无*********************************************************/
void ChessPiece::setColor(int color) {this->color = color;
}/************************************************************ @函数名:setXY* @功  能:设置棋子坐标* @参  数:x---棋子的横坐标* @参  数:y---棋子的纵坐标* @返回值:无*********************************************************/
void ChessPiece::setXY(int x, int y) {this->x = x;this->y = y;
}/************************************************************ @函数名:createChessPiece* @功  能:修正棋子坐标* @参  数:无* @返回值:无*********************************************************/
void ChessPiece::fixChessPiece() {if (this->x >= 0 && this->y >= 0) {//范围合理if (this->x % BOARD_INTERVAL > BOARD_INTERVAL / 2) {int i;for (i = 0; i * BOARD_INTERVAL < this->x; i++);this->x = i * BOARD_INTERVAL;}else {this->x -= this->x % BOARD_INTERVAL;}if (this->y % BOARD_INTERVAL > BOARD_INTERVAL / 2) {int i;for (i = 0; i * BOARD_INTERVAL < this->y; i++);this->y = i * BOARD_INTERVAL;}else {this->y -= this->y % BOARD_INTERVAL;}}else {//坐标越界this->x = 0;this->y = 0;}
}

【6】ChessBoard.h:

#pragma once
#include <iostream>
#include <graphics.h>#define BOARD_INTERVAL	20	//网格的间隔/************************************************************ @类名:	ChessBoard* @摘要:	棋盘类* @作者:	柯同学* @注意:	默认宽度和高度为400,400*********************************************************/
class ChessBoard {
private:int width;		//棋盘宽度int height;		//棋盘高度
public:ChessBoard(int w = 400, int h = 400) : width(w), height(h) {}void setWidth(int width);	//设置棋盘宽度void setHeight(int height);	//设置棋盘高度int getWidth();				//获取棋盘宽度int getHeight();			//获取棋盘高度void showBoard();			//绘制棋盘网格
};

【7】ChessBoard.cpp:

#include "ChessBoard.h"/************************************************************ @函数名:setWidth* @功  能:设置棋盘宽度* @参  数:width---要设置的属性* @返回值:无*********************************************************/
void ChessBoard::setWidth(int width) {this->width = width;
}/************************************************************ @函数名:setHeight* @功  能:设置棋盘高度* @参  数:height---要设置的属性* @返回值:无*********************************************************/
void ChessBoard::setHeight(int height) {this->height = height;
}/************************************************************ @函数名:getWidth* @功  能:获取棋盘宽度* @参  数:无* @返回值:棋盘的宽度*********************************************************/
int ChessBoard::getWidth() {return width;
}/************************************************************ @函数名:getHeight* @功  能:获取棋盘高度* @参  数:无* @返回值:棋盘的高度*********************************************************/
int ChessBoard::getHeight() {return height;
}/************************************************************ @函数名:showBoard* @功  能:绘制棋盘并显示* @参  数:无* @返回值:无*********************************************************/
void ChessBoard::showBoard() {std::cout << width << " " << height << std::endl;for (int i = 0; i <= width; i += BOARD_INTERVAL) {line(0, i, width, i);//横线line(i, 0, i, height);//竖线}
}

【8】Mouse.h:

#pragma once
#include <graphics.h>
#include <iostream>
#include "ChessPiece.h"/************************************************************ @类名:	Mouse* @摘要:	鼠标类* @作者:	柯同学* @注意:	无*********************************************************/
class Mouse {
private:ExMessage mouseMesg;	//鼠标信息,包含坐标、点击的键int clickTotal;			//鼠标有效点击次数
public:Mouse() : clickTotal(0) {}		//无参构造:鼠标次数初始化为0void setClickTotal(int num);	//设置鼠标点击次数int getClickTotal();			//获取鼠标点击次数ExMessage& getMouseMesg();		//获取鼠标信息bool detectClick();				//返回鼠标是否被点击
};

【9】mouse.cpp:

#include "Mouse.h"/************************************************************ @函数名:setClickTotal* @功  能:设置鼠标有效点击次数* @参  数:num---要设置的次数* @返回值:无*********************************************************/
void Mouse::setClickTotal(int num) {this->clickTotal = num;
}/************************************************************ @函数名:getClickTotal* @功  能:获取鼠标有效点击次数* @参  数:无* @返回值:鼠标有效点击次数*********************************************************/
int Mouse::getClickTotal() {return this->clickTotal;
}/************************************************************ @函数名:getMouseMesg* @功  能:获取鼠标信息:包含点击的坐标、鼠标键* @参  数:无* @返回值:鼠标信息*********************************************************/
ExMessage& Mouse::getMouseMesg() {return this->mouseMesg;
}/************************************************************ @函数名:detectClick* @功  能:返回鼠标是否被点击* @参  数:无* @返回值:true---鼠标被点击,false---鼠标没有被点击*********************************************************/
bool Mouse::detectClick() {if (peekmessage(&this->mouseMesg) && this->mouseMesg.message == WM_LBUTTONDOWN) {//鼠标左击return true;}return false;
}

【10】Menu.h:

#pragma once
#include <iostream>
#include <graphics.h>
#include <conio.h>
#include "ChessBoard.h"
#include "Mouse.h"
#include "Player.h"/************************************************************ @类名:	Menu* @摘要:	菜单类* @作者:	柯同学* @注意:	无*********************************************************/
class Menu {
/*************以下为Menu内部调用的函数***********************/
private:void multiPlayerPK(Mouse& mouse,		//哪个玩家落子WhitePlayer& wp,BlackPlayer& bp,YellowPlayer& yp);	void multiPlayerWin(ChessBoard& board,	//哪个玩家获胜WhitePlayer& wp,BlackPlayer& bp,YellowPlayer& yp);	/*************以下为menu对外开放的函数**********************/
public:int mainInterface();					//主界面显示void introInterface();					//游戏介绍界面void startGame(Mouse& mouse, ChessBoard& board, WhitePlayer& wp, BlackPlayer& bp,YellowPlayer& yp);		//三人对战界面
};

【11】Menu.cpp:

#include "Menu.h"/************************************************************ @函数名:mainInterface* @功  能:游戏主界面* @参  数:无* @返回值:无*********************************************************/
int Menu::mainInterface() {initgraph(800, 600);setbkcolor(WHITE);cleardevice();settextstyle(70, 0, "Arial");settextcolor(BLACK);outtextxy(150, 120, "小游戏:三人成棋");setlinecolor(BLACK);settextstyle(50, 0, "Arial");outtextxy(280, 250, "1.开始游戏");outtextxy(280, 350, "2.游戏介绍");outtextxy(280, 450, "0.退出游戏");int keyNum = -1;while (1) {if (_kbhit()) {keyNum = _getch();if (keyNum == '1' || keyNum == '2' || keyNum == '0')break;}}closegraph();return keyNum;
}/************************************************************ @函数名:introInterface* @功  能:游戏介绍的界面* @参  数:无* @返回值:无*********************************************************/
void Menu::introInterface() {initgraph(800, 600, 0);setbkcolor(WHITE);cleardevice();settextstyle(70, 0, "Arial");settextcolor(BLACK);outtextxy(0, 0, "游戏介绍:");settextstyle(50, 0, "Arial");outtextxy(0, 100, "【1】游戏人数:三人");outtextxy(0, 200, "【2】下棋顺序:白棋、黑棋、黄棋");outtextxy(0, 300, "【3】返回提示:默认按0返回");outtextxy(0, 400, "【4】其他说明:基本已实现,但仍有bug");outtextxy(450, 500, "按0返回主菜单!");while (!_kbhit() || _getch() != '0');closegraph();
}/************************************************************ @函数名:startGame* @功  能:三人对战界面* @参  数:mouse---鼠标对象* @参  数:board---棋盘对象* @参  数:wp---白棋玩家* @参  数:bp---黑棋玩家* @参  数:yp---黄棋玩家* @返回值:无*********************************************************/
void Menu::startGame(Mouse& mouse, ChessBoard& board, WhitePlayer& wp, BlackPlayer& bp, YellowPlayer& yp) 
{//创建窗口,显示棋盘initgraph(board.getWidth(), board.getHeight(), 0);setbkcolor(RGB(245, 222, 181));setlinecolor(BLACK);cleardevice();board.showBoard();//主循环while (!_kbhit() || _getch() != '0') {//按'0'退出程序//玩家落子multiPlayerPK(mouse, wp, bp, yp);//玩家胜负判定multiPlayerWin(board, wp, bp, yp);}closegraph();
}/************************************************************ @函数名:multiPlayerPK* @功  能:确定哪个玩家落子,根据鼠标有效点击次数* @参  数:mouse---鼠标对象* @参  数:wp---白棋玩家* @参  数:bp---黑棋玩家* @参  数:yp---黄棋玩家* @返回值:无*********************************************************/
void Menu::multiPlayerPK(Mouse& mouse, WhitePlayer& wp, BlackPlayer& bp, YellowPlayer& yp)
{if (mouse.getClickTotal() % 3 == 0) {wp.generatePiece(mouse, bp, yp);//白棋落子}else if (mouse.getClickTotal() % 3 == 1) {bp.generatePiece(mouse, wp, yp);//黑棋落子}else {yp.generatePiece(mouse, wp, bp);//黄棋落子}}/************************************************************ @函数名:multiPlayerWin* @功  能:确定哪个玩家获胜* @参  数:board---棋盘对象* @参  数:wp---白棋玩家* @参  数:bp---黑棋玩家* @参  数:yp---黄棋玩家* @返回值:无*********************************************************/
void Menu::multiPlayerWin(ChessBoard& board, WhitePlayer& wp, BlackPlayer& bp, YellowPlayer& yp) 
{if (wp.isWin(board)) {cleardevice();settextcolor(BLACK);settextstyle(30, 0, "Arial");outtextxy(25, board.getHeight() / 2 - 30, "白棋获胜!按0返回主界面!");}else if (bp.isWin(board)) {cleardevice();settextcolor(BLACK);settextstyle(30, 0, "Arial");outtextxy(25, board.getHeight() / 2 - 30, "黑棋获胜!按0返回主界面!");}else if (yp.isWin(board)) {cleardevice();settextcolor(BLACK);settextstyle(30, 0, "Arial");outtextxy(25, board.getHeight() / 2 - 30, "黄棋获胜!按0返回主界面!");}
}

四、实现效果

五、已知BUG

由于时间原因,已有的BUG在未来有时间后再修改:

【1】一局游戏下完后,从主菜单进来可能会停留在上次下棋后的界面。

【2】游戏输赢判定后,点击屏幕依然能下棋。

这篇关于【C++】<图形库> 三人成棋(面向对象写法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1003605

相关文章

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

C++ STL-string类底层实现过程

《C++STL-string类底层实现过程》本文实现了一个简易的string类,涵盖动态数组存储、深拷贝机制、迭代器支持、容量调整、字符串修改、运算符重载等功能,模拟标准string核心特性,重点强... 目录实现框架一、默认成员函数1.默认构造函数2.构造函数3.拷贝构造函数(重点)4.赋值运算符重载函数

C++ vector越界问题的完整解决方案

《C++vector越界问题的完整解决方案》在C++开发中,std::vector作为最常用的动态数组容器,其便捷性与性能优势使其成为处理可变长度数据的首选,然而,数组越界访问始终是威胁程序稳定性的... 目录引言一、vector越界的底层原理与危害1.1 越界访问的本质原因1.2 越界访问的实际危害二、基

c++日志库log4cplus快速入门小结

《c++日志库log4cplus快速入门小结》文章浏览阅读1.1w次,点赞9次,收藏44次。本文介绍Log4cplus,一种适用于C++的线程安全日志记录API,提供灵活的日志管理和配置控制。文章涵盖... 目录简介日志等级配置文件使用关于初始化使用示例总结参考资料简介log4j 用于Java,log4c

C++归并排序代码实现示例代码

《C++归并排序代码实现示例代码》归并排序将待排序数组分成两个子数组,分别对这两个子数组进行排序,然后将排序好的子数组合并,得到排序后的数组,:本文主要介绍C++归并排序代码实现的相关资料,需要的... 目录1 算法核心思想2 代码实现3 算法时间复杂度1 算法核心思想归并排序是一种高效的排序方式,需要用

C++11范围for初始化列表auto decltype详解

《C++11范围for初始化列表autodecltype详解》C++11引入auto类型推导、decltype类型推断、统一列表初始化、范围for循环及智能指针,提升代码简洁性、类型安全与资源管理效... 目录C++11新特性1. 自动类型推导auto1.1 基本语法2. decltype3. 列表初始化3

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

C++中detach的作用、使用场景及注意事项

《C++中detach的作用、使用场景及注意事项》关于C++中的detach,它主要涉及多线程编程中的线程管理,理解detach的作用、使用场景以及注意事项,对于写出高效、安全的多线程程序至关重要,下... 目录一、什么是join()?它的作用是什么?类比一下:二、join()的作用总结三、join()怎么

C++中全局变量和局部变量的区别

《C++中全局变量和局部变量的区别》本文主要介绍了C++中全局变量和局部变量的区别,全局变量和局部变量在作用域和生命周期上有显著的区别,下面就来介绍一下,感兴趣的可以了解一下... 目录一、全局变量定义生命周期存储位置代码示例输出二、局部变量定义生命周期存储位置代码示例输出三、全局变量和局部变量的区别作用域