C++:从向量中获取值
C++: Get value from a 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);
另一个问题:像这样简单地传递向量会不好吗?
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++中第一个值和最后一个值的和
- 从 exprTk 获取向量作为输出
- C++ 获取向量中常量字符* [ ] 的长度
- 如何使用模板获取向量中的数据类型?
- C++ 通过 std::transform 的 vector 索引获取向量
- C++ 运算符"="重载 - 在 lhs 上获取向量中的所有值,使其等于 rhs 上的双精度值
- 通过交互器获取向量中的索引
- 无法获取向量中元素的总和
- 获取向量中元素的地址
- 如何获取向量的每个第 N 个元素的"address"
- 我应该如何从特征 3 中的张量切片中获取向量
- 如何获取向量的排序索引
- 向量C ,获取向量的第二个元素有什么问题
- 获取向量<float>中 k 个最大值的 indize 的有效方法
- 获取向量的指定元素
- 如何在具有相同基类的两个不同类中获取向量
- 如何获取向量的这些元素,其中另一个向量在相应的索引处具有'1
- 一个模板函数,用于获取向量中的最大值及其相应的索引
- 获取向量 c++ 中的字符串计数
- C++linux获取向量迭代器不兼容