gpt4 book ai didi

C++:从 vector 中获取值

转载 作者:行者123 更新时间:2023-11-30 02:57:38 26 4
gpt4 key购买 nike

我有一个函数,我将指针传递给一个 unsigned char vector 。

有人可以告诉我如何获取函数内的值之一吗?

double CApp::GetCost(unsigned char *uBytes)
{
unsigned char iHP;
iHP=uBytes[49]; //this does not work
}

编辑:抱歉,我首先想到我应该简化我的代码,但我认为太多可能会出错。现在这是真正的声明:

// ---------------------------------------
struct ByteFeature
{
unsigned char Features[52];
};

class clsByteFeatures : public CBaseStructure
{
private:
vector<ByteFeature> m_content;

protected:
virtual void ProcessTxtLine(string line);

public:
vector<ByteFeature> &Content();
void Add(ByteFeature &bf);
};

vector<ByteFeature> &clsByteFeatures::Content()
{
return m_content;
}

这就是我使用它的方式:

dblTargetCost  = GetCost(m_ByteFeatures.Content()[iUnitID].Features);

另一个问题:像这样简单地传递 vector 会不好吗?

double CApp::GetCost(vector<unsigned char> &uBytes)
{
//...
}

最佳答案

Would it be bad to simply pass the vector like this?
double CApp::GetCost(vector<unsigned char> &uBytes)

这不是通过引用传递它的更好方法。但是,如果您不想修改 uBytes,则可能需要添加 const 限定符。

double CApp::GetCost(const vector<unsigned char> &uBytes)
{
try
{
unsigned char iHP = uBytes.at(49);
//...
}
catch(std::exception& e)
{
// process e
}
//...
}

编辑:

在你发布新帖子后,我觉得你只需要返回对 m_content 元素的引用,然后将引用传递给 GetCost 函数

ByteFeature& clsByteFeatures::operator[](int i) { return m_content.at(i); }


double GetCost(const ByteFeature& bf)
{
std::cout << bf.Features[49]; << std::endl;
return 0.0;
}

然后调用:

GetCost(m_ByteFeatures[iUnitID]); 

关于C++:从 vector 中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14412496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com