【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#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

Linux下如何使用C++获取硬件信息

《Linux下如何使用C++获取硬件信息》这篇文章主要为大家详细介绍了如何使用C++实现获取CPU,主板,磁盘,BIOS信息等硬件信息,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录方法获取CPU信息:读取"/proc/cpuinfo"文件获取磁盘信息:读取"/proc/diskstats"文

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

C++ vector的常见用法超详细讲解

《C++vector的常见用法超详细讲解》:本文主要介绍C++vector的常见用法,包括C++中vector容器的定义、初始化方法、访问元素、常用函数及其时间复杂度,通过代码介绍的非常详细,... 目录1、vector的定义2、vector常用初始化方法1、使编程用花括号直接赋值2、使用圆括号赋值3、ve

如何高效移除C++关联容器中的元素

《如何高效移除C++关联容器中的元素》关联容器和顺序容器有着很大不同,关联容器中的元素是按照关键字来保存和访问的,而顺序容器中的元素是按它们在容器中的位置来顺序保存和访问的,本文介绍了如何高效移除C+... 目录一、简介二、移除给定位置的元素三、移除与特定键值等价的元素四、移除满足特android定条件的元

Python获取C++中返回的char*字段的两种思路

《Python获取C++中返回的char*字段的两种思路》有时候需要获取C++函数中返回来的不定长的char*字符串,本文小编为大家找到了两种解决问题的思路,感兴趣的小伙伴可以跟随小编一起学习一下... 有时候需要获取C++函数中返回来的不定长的char*字符串,目前我找到两种解决问题的思路,具体实现如下:

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++