You need to enable JavaScript to run this app. C++交易模拟中对象数据修改后恢复初始值问题求助
优惠活动
大模型
产品
解决方案
定价
更多

C++交易模拟中对象数据修改后恢复初始值问题求助

交易模拟程序数据回滚问题排查与修复

问题现象

开发的交易模拟程序中,对象内的数据被临时修改后会恢复为初始值,无法保留交易后的状态。程序逻辑为:钱包向资金池支付token换取USDT,或反向操作。

问题根源

核心问题在于vector中存储的是Wallet对象的副本,而非原对象的引用:

  • vector<Wallet> wallets = {w1, w2, w3, w4, w5}; 此代码将w1-w5复制到vector中,后续对wallets元素的修改仅作用于副本,原对象w1-w5的状态不会改变。
  • vector<Wallet> allWallets = {main, w1, w2, w3, w4, w5, pool}; 同样,该vector存储的是所有钱包初始状态的副本,循环打印时输出的永远是初始值,而非交易后的最新状态。
  • 两个vector中的副本完全独立,修改wallets里的副本不会同步到allWallets的对应副本。

修复方案

将vector的元素类型改为std::reference_wrapper<Wallet>(存储原对象的引用),确保所有操作直接作用于原对象,而非副本。

修正后的完整代码

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <string>
#include <functional> // 引入reference_wrapper所需头文件

using namespace std;

int getRandomNumber(int min, int max) {
    return min + rand() % ((max + 1) - min);
}

class Wallet {
private:
    double token;
    double usdt;

public:
    Wallet(double token, double usdt) : token(token), usdt(usdt) {}

    void deposit(const string& asset, double amount) {
        if (asset == "token") {
            token += amount;
        }
        else {
            usdt += amount;
        }
    }

    void withdraw(const string& asset, double amount) {
        if (asset == "token") {
            token -= amount;
        }
        else {
            usdt -= amount;
        }
    }

    double get(const string& asset) const {
        if (asset == "token") {
            return token;
        }
        else {
            return usdt;
        }
    }

    void print() const {
        cout << "Token Amount: " << token << " | Usdt Amount: " << usdt << endl;
    }
};

int main() {
    srand(static_cast<unsigned int>(time(nullptr)));

    Wallet main(150, 5000);
    Wallet w1(150, 50);
    Wallet w2(150, 50);
    Wallet w3(150, 50);
    Wallet w4(150, 50);
    Wallet w5(150, 50);
    Wallet pool(2500, 1000);

    // 使用reference_wrapper存储原对象的引用,避免副本创建
    vector<reference_wrapper<Wallet>> wallets = {w1, w2, w3, w4, w5};
    vector<reference_wrapper<Wallet>> allWallets = {main, w1, w2, w3, w4, w5, pool};

    while (true) {
        // 计算当前资金池价格
        double price = pool.get("usdt") / pool.get("token");
        cout << "Current Price: $" << price << endl;

        // 遍历所有钱包的引用,打印最新状态
        for (const auto& wal_ref : allWallets) {
            wal_ref.get().print();
        }
        Sleep(3000);

        // 随机选择一个钱包
        int ind = getRandomNumber(0, wallets.size() - 1);
        Wallet& wal = wallets[ind].get(); // 获取引用指向的原对象
        double usdt = wal.get("usdt");
        double token = wal.get("token");
        cout << "Selected Wallet Token Amount: " << token << endl;
        cout << "Selected Wallet Usdt Amount: " << usdt << endl;
        cout << endl;

        // 随机选择交易方向和金额
        int side = getRandomNumber(0, 1);
        double amount = getRandomNumber(3, 6);

        // 买入Token(支付USDT)
        if (side == 0) {
            if (usdt >= amount) {
                wal.withdraw("usdt", amount);
                pool.deposit("usdt", amount);

                double tokenAmount = amount / price;
                wal.deposit("token", tokenAmount);
                pool.withdraw("token", tokenAmount);
            }
            else {
                // 资金不足时从主钱包转账
                double transferAmount = main.get("usdt") / 5;
                wal.deposit("usdt", transferAmount);
                main.withdraw("usdt", transferAmount);
                continue;
            }
        }
        else { // 卖出Token(获取USDT)
            double tokenAmount = amount / price;
            if (token >= tokenAmount) {
                wal.withdraw("token", tokenAmount);
                pool.deposit("token", tokenAmount);

                double usdtAmount = tokenAmount * price;
                wal.deposit("usdt", usdtAmount);
                pool.withdraw("usdt", usdtAmount);
            }
            else {
                // Token不足时从主钱包转账
                double transferAmount = main.get("token") / 5;
                wal.deposit("token", transferAmount);
                main.withdraw("token", transferAmount);
                continue;
            }
        }

        cout << endl;
    }

    return 0;
}

关键修改点

  1. 添加<functional>头文件,用于使用std::reference_wrapper。
  2. 将vector的元素类型从Wallet改为reference_wrapper<Wallet>,确保存储的是原对象的引用而非副本。
  3. 遍历vector时,通过wal_ref.get()获取原对象,确保操作和打印的是最新状态。
  4. 获取选中钱包时,同样通过get()方法拿到原对象的引用,修改操作直接作用于原对象,数据不会回滚。

内容的提问来源于stack exchange,提问作者Umut Altındağ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 19:29:54