Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 编撰者是否将&&;语句的顺序是否与您编写它们的顺序相同?_C++_Algorithm_Compilation_Logic_Machine Code - Fatal编程技术网

C++ 编撰者是否将&&;语句的顺序是否与您编写它们的顺序相同?

C++ 编撰者是否将&&;语句的顺序是否与您编写它们的顺序相同?,c++,algorithm,compilation,logic,machine-code,C++,Algorithm,Compilation,Logic,Machine Code,我正在写一个函数,到目前为止我已经 size_t CalculusWizard :: _grabDecimal ( std::string::const_iterator it1, std::string::const_iterator it2, std::string & ds ) { /* it1: iterator to the beginning of the decimal string it2: iterator to the 1-off-the-end of

我正在写一个函数,到目前为止我已经

size_t CalculusWizard :: _grabDecimal ( std::string::const_iterator it1, std::string::const_iterator it2, std::string & ds )
{
/*
    it1: iterator to the beginning of the decimal string
    it2: iterator to the 1-off-the-end of the range of which the decimal can span
     ds: string to hold the decimal representation

    Reads the decimal in the range [it1, it2) into the string ds
*/
    ds.clear();
    size_t ncp = 0; /* # of characters parsed */
    if (it1 != it2 && *it1 == '-') ds.push_back(*it1++); /* Handle possible minus sign */
    bool foundDot = false;
    while (it1 != it2)
    {
        if (*it1 == '.')
        {
            if (foundDot) break;
            else foundDot = true;
        }
        else if (_digMap.count(*it1) > 0)
        {
         // ...
        }
        else
        {
            break;
        }
        ++it1;
    }
    return ncp;
}
我的主要问题是关于状态
if(it1!=it2&&*it1=='-')
。我的意思是让它成为一种更简洁的写作方式

if (it1 != it2)
{
    if (*it == '-') // ...
}
因为有可能
it2
不在字符串末尾,我希望避免意外行为。但我想知道

(1) 我写它的方式被认为是可读的

(2) 它可能会导致问题,因为它假定由
&&
分隔的语句从左到右有条件执行

希望对计算机科学概念有更深入了解的人能给我解释一下

作为奖励,有没有人有更好的方法来完成我试图用这个函数做的事情?我所要做的就是获取字符串中包含的十进制表示,同时跟踪在获取十进制数时解析的字符数。我不能使用stod,因为我丢失了我需要的信息。

1)当然。。。如果其他程序员不可读,该语言就不会有
&&

2) 不,它不会引起问题。请注意,
&
运算符是一个“短路”逻辑运算符,左手边在右手边之前求值,因此当
p
nullptr
时,即使像
p&&*p==2这样的代码也是安全的

至于如何提高功能。。。我建议使用
std::istringstream
来解析字符串表示形式中的数字(如果需要解析字符串的一部分,请使用
std::string::substr
),然后可以在流上使用来查看解析了多少


一个更为“C”风格的替代方法是使用-str_end
参数可以捕获第一个未转换字符的位置。

@TartanLlama:不,这完全无关。优先级决定表达式的解析方式,而不是它的运行方式。即使我们知道LHS是先评估的,我们也无法回答这个问题。这里的关键是,如果LHS为假,则根本不评估RHS。