C++ 使用带有向量的静态字符串
我的编程历史是用C和CPython编写的。请容忍我在这里 为了帮助我学习C++,我把我的一个旧C程序转换成使用C++ OOP,但是它并不是我想用的。我不在乎速度。我只关心学习 下面是我想放入校验和类的旧C代码:C++ 使用带有向量的静态字符串,c++,string,vector,C++,String,Vector,我的编程历史是用C和CPython编写的。请容忍我在这里 为了帮助我学习C++,我把我的一个旧C程序转换成使用C++ OOP,但是它并不是我想用的。我不在乎速度。我只关心学习 下面是我想放入校验和类的旧C代码: //This is the standard CRC32 implementation //"rollingChecksum" is used so the caller can maintain the current //checksum between f
//This is the standard CRC32 implementation
//"rollingChecksum" is used so the caller can maintain the current
//checksum between function calls
unsigned int CalculateChecksum(unsigned char* eachBlock, int* sbox, long lengthOfBlock, unsigned int rollingChecksum)
{
int IndexLookup;
int blockPos;
for(blockPos = 0; blockPos < lengthOfBlock; blockPos++)
{
IndexLookup = (rollingChecksum >> 0x18) ^ eachBlock[blockPos];
rollingChecksum = (rollingChecksum << 0x08) ^ sbox[IndexLookup];
}
return rollingChecksum;
}
//这是标准的CRC32实现
//使用“rollingChecksum”以便调用方可以维护当前
//函数调用之间的校验和
unsigned int CalculateChecksum(unsigned char*eachBlock,int*sbox,long lengthOfBlock,unsigned int rollingChecksum)
{
int IndexLookup;
int blockPos;
对于(blockPos=0;blockPos>0x18)^eachBlock[blockPos];
rollingChecksum=(rollingChecksum checksum>>0x18)^binaryData[blockPos];
this->checksum=(this->checksum sbox[indexLookup];
}
}
但当我尝试使用它时:
int main(int argc, char* argv[])
{
Checksum Test;
Test.UpdateStream("foo bar foobar");
std::cout << Test.getChecksum() << std::endl;
}
intmain(intargc,char*argv[])
{
校验和检验;
Test.UpdateStream(“foobar foobar”);
std::cout with
1> [
1> _Ty=无符号字符
1> ]
1> 没有构造函数可以采用源类型,或者构造函数重载解析不明确
我决定使用上面的vector容器而不是string类,因为我想在这里使用二进制数据
期望的结果:如何将字符串和二进制数据传递到此方法以计算其校验和?是否需要重载它或在main中键入字符串?我完全丢失了。您可以使用
std::copy
将char
数组内容复制到向量中:
std::vector< char > vector;
char str[] = "foo bar foobar";
vector.resize( sizeof(str)-1 ); // thanks to Alf (see comments)
std::copy( str, str+sizeof(str)-1, vector.begin() );
请注意,此代码将复制整个字符串,但终止的
\0
(再次,有关详细信息,请参阅注释)。您可以使用std::copy
将char
数组内容复制到向量中:
std::vector< char > vector;
char str[] = "foo bar foobar";
vector.resize( sizeof(str)-1 ); // thanks to Alf (see comments)
std::copy( str, str+sizeof(str)-1, vector.begin() );
请注意,此代码将复制整个字符串,但将复制终止的
\0
(再次,请参阅注释以了解更多详细信息)。您不能使用字符串文字创建向量。至少不能直接创建。这应该可以:
std::string tmp = "foo bar foobar"
Test.UpdateStream(std::vector<unsigned char>(tmp.begin(), tmp.end()));
std::string tmp=“foo bar foobar”
Test.UpdateStream(std::vector(tmp.begin(),tmp.end());
您还可以更新UpdateStream签名以使用(const std::vector&binaryData)
避免复制,因为您不需要修改它
要缩短代码,可以为字符串提供UpdateStream重载:
void Checksum::UpdateStream(const std::string& str) {
UpdateStream(std::vector<unsigned char>(str.begin(), str.end()));
}
void校验和::UpdateStream(const std::string&str){
UpdateStream(std::vector(str.begin(),str.end());
}
不能用字符串文字创建向量。至少不能直接创建。这应该可以:
std::string tmp = "foo bar foobar"
Test.UpdateStream(std::vector<unsigned char>(tmp.begin(), tmp.end()));
std::string tmp=“foo bar foobar”
Test.UpdateStream(std::vector(tmp.begin(),tmp.end());
您还可以更新UpdateStream签名以使用(const std::vector&binaryData)
避免复制,因为您不需要修改它
要缩短代码,可以为字符串提供UpdateStream重载:
void Checksum::UpdateStream(const std::string& str) {
UpdateStream(std::vector<unsigned char>(str.begin(), str.end()));
}
void校验和::UpdateStream(const std::string&str){
UpdateStream(std::vector(str.begin(),str.end());
}
您的校验和类处理来自任何地方的原始字节数据,因此基本接口不应强制转换为例如std::vector
也就是说,原始C代码的无符号字符常量*
形式参数类型过去是,现在是
但是,您可以为常见调用方的类型提供更高级别的包装,例如字符串文本、std::vector
,您可以随意命名
如果绝对希望将std::vector
作为基本函数的形式参数类型,则可以按如下方式将字符串文本复制到它:
char const s[] = "blah blah";
std::vector<unsigned char> const v( s, s + strlen( s ) );
char const s[]=“诸如此类”;
向量常数v(s,s+strlen(s));
Cheers&hth.,您的校验和类处理来自任何地方的原始字节数据,因此基本接口不应强制转换为例如
std::vector
也就是说,原始C代码的无符号字符常量*
形式参数类型过去是,现在是
但是,您可以为常见调用方的类型提供更高级别的包装,例如字符串文本、std::vector
,您可以随意命名
如果绝对希望将std::vector
作为基本函数的形式参数类型,则可以按如下方式将字符串文本复制到它:
char const s[] = "blah blah";
std::vector<unsigned char> const v( s, s + strlen( s ) );
char const s[]=“诸如此类”;
向量常数v(s,s+strlen(s));