小编yas*_*ash的帖子

switch 语句中的 case 内具有相同的表达式

int main(){
char c='a';
switch(c){
    case 'a' && 1: printf("Hello");
    case 'b' && 1: printf("hey");
                   break;
         default : printf("Goodbye");
          }
}
Run Code Online (Sandbox Code Playgroud)

当我编译此代码时,结果是“编译错误”,这(根据我的说法)是因为在内部两个表达式都是 true,因此对于我们为“c”采用的任何字符,两种情况下的常量表达式都将始终为 true 。

但现在出现的疑问是我无法理解,内部如何解释代码以及编译器实际上如何解释该代码?

c label compiler-errors switch-statement logical-and

3
推荐指数
1
解决办法
387
查看次数

向量 (STL) 中的累加函数给出负和

当我编写下面的代码时,我得到一个负数 (-294967296)。

#include<iostream>
#include<vector>
#include<numeric>
using namespace std;
int main() {
    vector<long long int> v={1000000000 , 1000000000 , 1000000000 , 1000000000};
    cout<<"Sum of all the elements are:"<<endl;
    cout<<accumulate(v.begin(),v.end(),0);
}
Run Code Online (Sandbox Code Playgroud)

但是当我编写下面的代码时,我得到了一个正数 (2000000000)

#include<iostream>
#include<vector>
#include<numeric>
using namespace std;
int main() {
    vector<long long int> v={1000000000 , 1000000000};
    cout<<"Sum of all the elements are:"<<endl;
    cout<<accumulate(v.begin(),v.end(),0);
}
Run Code Online (Sandbox Code Playgroud)

可能是什么原因?

c++ stl vector accumulate

0
推荐指数
1
解决办法
61
查看次数