【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++中的虚拟继承的一些总结(虚拟继承,覆盖,派生,隐藏)

1.为什么要引入虚拟继承 虚拟继承是多重继承中特有的概念。虚拟基类是为解决多重继承而出现的。如:类D继承自类B1、B2,而类B1、B2都继承自类A,因此在类D中两次出现类A中的变量和函数。为了节省内存空间,可以将B1、B2对A的继承定义为虚拟继承,而A就成了虚拟基类。实现的代码如下: class A class B1:public virtual A; class B2:pu

C++对象布局及多态实现探索之内存布局(整理的很多链接)

本文通过观察对象的内存布局,跟踪函数调用的汇编代码。分析了C++对象内存的布局情况,虚函数的执行方式,以及虚继承,等等 文章链接:http://dev.yesky.com/254/2191254.shtml      论C/C++函数间动态内存的传递 (2005-07-30)   当你涉及到C/C++的核心编程的时候,你会无止境地与内存管理打交道。 文章链接:http://dev.yesky

C++的模板(八):子系统

平常所见的大部分模板代码,模板所传的参数类型,到了模板里面,或实例化为对象,或嵌入模板内部结构中,或在模板内又派生了子类。不管怎样,最终他们在模板内,直接或间接,都实例化成对象了。 但这不是唯一的用法。试想一下。如果在模板内限制调用参数类型的构造函数会发生什么?参数类的对象在模板内无法构造。他们只能从模板的成员函数传入。模板不保存这些对象或者只保存他们的指针。因为构造函数被分离,这些指针在模板外

C++工程编译链接错误汇总VisualStudio

目录 一些小的知识点 make工具 可以使用windows下的事件查看器崩溃的地方 dumpbin工具查看dll是32位还是64位的 _MSC_VER .cc 和.cpp 【VC++目录中的包含目录】 vs 【C/C++常规中的附加包含目录】——头文件所在目录如何怎么添加,添加了以后搜索头文件就会到这些个路径下搜索了 include<> 和 include"" WinMain 和

C/C++的编译和链接过程

目录 从源文件生成可执行文件(书中第2章) 1.Preprocessing预处理——预处理器cpp 2.Compilation编译——编译器cll ps:vs中优化选项设置 3.Assembly汇编——汇编器as ps:vs中汇编输出文件设置 4.Linking链接——链接器ld 符号 模块,库 链接过程——链接器 链接过程 1.简单链接的例子 2.链接过程 3.地址和

C++必修:模版的入门到实践

✨✨ 欢迎大家来到贝蒂大讲堂✨✨ 🎈🎈养成好习惯,先赞后看哦~🎈🎈 所属专栏:C++学习 贝蒂的主页:Betty’s blog 1. 泛型编程 首先让我们来思考一个问题,如何实现一个交换函数? void swap(int& x, int& y){int tmp = x;x = y;y = tmp;} 相信大家很快就能写出上面这段代码,但是如果要求这个交换函数支持字符型

C++入门01

1、.h和.cpp 源文件 (.cpp)源文件是C++程序的实际实现代码文件,其中包含了具体的函数和类的定义、实现以及其他相关的代码。主要特点如下:实现代码: 源文件中包含了函数、类的具体实现代码,用于实现程序的功能。编译单元: 源文件通常是一个编译单元,即单独编译的基本单位。每个源文件都会经过编译器的处理,生成对应的目标文件。包含头文件: 源文件可以通过#include指令引入头文件,以使

C++面试八股文:std::deque用过吗?

100编程书屋_孔夫子旧书网 某日二师兄参加XXX科技公司的C++工程师开发岗位第26面: 面试官:deque用过吗? 二师兄:说实话,很少用,基本没用过。 面试官:为什么? 二师兄:因为使用它的场景很少,大部分需要性能、且需要自动扩容的时候使用vector,需要随机插入和删除的时候可以使用list。 面试官:那你知道STL中的stack是如何实现的吗? 二师兄:默认情况下,stack使

剑指offer(C++)--孩子们的游戏(圆圈中最后剩下的数)

题目 每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0...m-1报数....这样下去

剑指offer(C++)--扑克牌顺子

题目 LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为1