我正在尝试用C ++编写一个方法,该方法使用流操作符确定整数是否在其十六进制表示中包含字母,而不使用任何for循环。
一种方法是使用getline
并进行一些数学运算。有人可以提供所需计算类型的提示吗?
感谢您的帮助或使用循环算术,但我正在寻找不会迭代字符串的东西
答案 0 :(得分:6)
使用C ++ ostringstream
和string
:
bool hasHexDigits(int number)
{
std::ostringstream output;
output << std::hex << number;
return output.str().find_first_of("abcdefABCDEF") != string::npos;
}
编辑:其他解决方案,不使用流。这是更好的性能(没有分支)和内存明智(没有分配),但可能对你的“家庭作业”来说太高级了:
template<int I> struct int_to_type
{
static const int value = I;
};
inline bool __hasHexCharacters(int number, int_to_type<0>)
{
return false;
}
template <int digitsLeft>
inline bool __hasHexCharacters(int number, int_to_type<digitsLeft>)
{
return ((number & 0xF) > 9) | __hasHexCharacters(number >> 4, int_to_type<digitsLeft-1>());
}
inline bool hasHexCharacters(int number)
{
return __hasHexCharacters(number, int_to_type<2 * sizeof(int)>());
}
答案 1 :(得分:5)
将int除以16,直到达到零。每次检查剩余部分,如果是> 9 hex包含字母。
答案 2 :(得分:2)
sprintf(string,"%x",<your integer>);
将给出您的十六进制数字。
所以在此之后,使用一些可用的字符串函数检查你的字符串下面是否有任何字母。
a,b,c,d,e,f
答案 3 :(得分:2)
// Assumes 32-bit int. Computing the mask based on UINT_MAX is left as an
// exercise for the reader.
int has_hex_alpha(unsigned int num) {
return !!((num & (num << 1 | num << 2)) & 2290649224);
}
答案 4 :(得分:2)
您的要求非常奇怪,因此很难给出正确的答案。到目前为止,所有的解决方案似乎都要迭代(尽管一些内部的std函数),或者使用直线整数,这似乎违背了你的要求? “十六进制表示”告诉我你有一个字符串形式的数字。如果这是事实,那么不使用for循环(?)&amp;迫使我们使用流操纵器使它成为一个不行。如果表示形式是ascii字符串,并且我们不允许迭代,那么一个既不需要迭代也不需要转换(可能会自行迭代)的解决方案可以利用所有字母数字的事实字符至少有2个MSB中的一个:
#include <iostream>
#include <string>
#include <cassert>
#include "boost\cstdint.hpp"
union StrInt
{
boost::uint64_t val;
char str[ sizeof( boost::uint64_t ) ];
};
int main()
{
std::string someString("A9999999" );
StrInt tmp;
assert( someString.size() <= sizeof( tmp.str ) );
memcpy( tmp.str, &someString[0], someString.size() );
std::cout << !!( tmp.val & 0xC0C0C0C0C0C0C0C0ul ) << std::endl;
}
答案 5 :(得分:1)
也许使用正则表达式?
regex rgx("[A-Za-z]+");
smatch result;
if(regex_search(integer_string, result, rgx)) {
cout << "There is alpha chars in the integer string" << endl;
}
答案 6 :(得分:1)
bool func(int num) {
while ( num > 0 ) {
if ( num % 16 > 9 ) return true;
num /= 16;
}
return false;
}
答案 7 :(得分:1)
使用stringstream的另一种方法:
std::stringstream ss;
std::string str;
std::string::iterator it;
bool hasChar = false;
// Use the hex modifier to place the hex representation of 75 into the
// stream and then spit the hex representation into a string.
ss << std::hex << 75;
str = ss.str(); // str contains "4b"
it = str.begin();
// Check for characters in hex representation.
while (!hasChar && it != str.end())
{
if (isalpha(*it))
hasChar = true;
++it;
}
答案 8 :(得分:1)
使用流操纵器,例如:
bool
hasAlphaInRepresentation( unsigned number )
{
std::ostringstream s;
s << std::hex << number;
std::string str = s.str();
return std::find( str.begin(), str.end(), IsAlpha()) != str.end();
}
会做到这一点。 (我假设你有一个IsAlpha功能 工具箱中的对象。如果没有,那就相当简单了 实施,它总是有用的。)
当然,如果唯一的要求是没有循环:
bool
hasAlphaInRepresentation( unsigned number )
{
return number != 0
&& (number % 16 > 9 || hasAlphaInRepresentation( number / 16 ));
}
: - )
答案 9 :(得分:0)
你可以做一些事情(只有逻辑操作和一个减法)
bool has_letters(int num)
{
if(num < 0) num = 0- num; //abs value
unsigned char * c;
c = (unsigned char * )(& num)
for(int i=0;i<sizeof(int),i++)
{
if(((c[i] & 0xf) > 0x9) || ((((c[i]>>4) & 0xf) > 0x9) )
return true;
//or you can use precomputed table
//if(table[c[i]])
// return true;
}
return false;
}
Trick是字符串的二进制表示已经是十六进制(几乎)你只需要每个nibble