Find()返回unsigned int的最大值
find() returning maximum value of unsigned int
我正在完成一项家庭作业,该作业要求我在同一个命令中重定向输入和输出。在命令(字符串)中搜索"<"answers">"时,if语句总是返回true。这是因为它在64位系统上查找unsigned int的最大值。
if (uCommand.find("<",0) && uCommand.find(">", 0))
语句将始终为真。当我运行u命令时。在GDB中查找(">",0)返回18446744073709551615.
int main(int argc, char *argv[]) {
// local variables
char **toks;
int retval;
int ii;
int redirect;
int search;
string fileName;
// initialize local variables
toks = NULL;
retval = 0;
ii = 0;
// main (infinite) loop
while (true)
{
// get arguments
toks = gettoks();
if (toks[0] != NULL)
{
for (ii = 0; toks[ii] != NULL; ii++) {
uCommand += " ";
uCommand += (string)toks[ii];
}
// Search for input, output, or dual redirection
if (uCommand.find("<", 0) && uCommand.find(">", 0))
{
redirect = dualRedirect(toks, uCommand);
addHist(uCommand);
}
else if (uCommand.find("<", 0)) {
redirect = inRedirect(toks, uCommand);
addHist(uCommand);
}
else if (uCommand.find(">", 0)) {
redirect = outRedirect(toks, uCommand);
addHist(uCommand);
}
// Look for hist or r and execute the relevant functions
if (!strcmp(toks[0], "r"))
repeatCommand(uCommand);
else if (!strcmp(toks[0], "hist")) {
addHist(uCommand);
showHist();
}
else if (redirect == 0) {
execCommand(toks);
addHist(uCommand);
}
// Increment the command count. This only runs if a something is entered
// on the command line. Blank lines do not increment this value.
commandCount++;
}
}
// return to calling environment
return(retval);
}
我假设uCommand
是std::string
,因为你没有包括它的声明。
std::string::find
在查找失败时返回std::string::npos
。通常是(size_t)-1
, size_t
是无符号类型,这意味着npos
是一个非常大的值。你不能把它当作bool
,因为任何非零的东西都被当作true
。
if (uCommand.find("<", 0) != std::string::npos && uCommand.find(">", 0) != std::string::npos)
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 使用指针从C++中的数组中获取最大值
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 在二维数组中查找最小值和最大值?
- 是否可以将无符号 int 的最大值转换为 int 并将结果转换为 -1?
- 查找不在标准中的第一个值::设置<int>最小-最大值
- 在MSDOS编译器上,用大于最大值的值初始化int时输出混乱
- 如何在给定范围内找到 std::set<pair<int、int> > 中的最大值?
- 为什么从矩阵中获取最大值的函数建议 ->无法将 'int (*)[C]' 转换为 'int (*)[0]
- 如果超过 short int(c++) 的最大值会发生什么
- 如何使用函数参数中的int指针来获取2d数组中最大值的索引并返回指针
- 当变量达到最大值时,unsigned int的增量是否会导致未定义的行为
- 为什么浮点数和 int 具有如此不同的最大值,即使它们是相同的位数?
- 如何在C++中将 const int 设置为最大值
- 将 -1 分配给无符号 int 以获得最大值是否安全
- "unsigned long int" c++ 中的最大值
- 自动标准最大输入的类型扣除失败 = 标准::最大值<int>;
- Find()返回unsigned int的最大值
- 如何简化此代码(在无符号 int 的基础上生成最小值和最大值之间的随机 int)