在阅读C ++ Primer书籍时,我遇到了这样的说法:“数组中元素的数量是数组类型的一部分。” 因此,我想使用以下代码进行查找:
#include<iostream>
int main()
{
char Array1[]{'H', 'e', 'l', 'p'};
char Array2[]{'P', 'l', 'e', 'a', 's', 'e'};
std::cout<<typeid(Array1).name()<<std::endl; //prints A4_c
std::cout<<typeid(Array2).name()<<std::endl; //prints A6_c
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有趣的是,两个数组上的typeid结果表明它们有所不同。
只是希望能够深刻理解这个概念。
初始化的返回值是什么?例如:在下面的代码中;
#include<iostream>
int main()
{
int age = 30; //does this statement return (thus; yield) anything?
while(int i = 0){ //a similar statement (expression) used here in this condition
std::cout<<"OK";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
int age = 27;返回什么?我想知道这一点的原因是,当我们查看上面的条件语句时,我们看到用作条件的变量的类似初始化。我知道返回的任何值都会转换为 bool 类型。
注意:我不想在 while 语句的条件下将 i 与 0 进行比较。
嗨,我对以下代码有些困惑:
int main()
{
int sum = 0, val = 1;
while(val <= 10)
sum += val, ++val; //source of problem
cout<<"Sum of 1 to 10 inclusive is " << sum << endl;
//
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我目前正在学习运算符优先级。知道逗号运算符在 C++ 运算符中的优先级最低,我希望 while 循环中的语句按以下顺序计算:
++val; //so that val is incremented to 2 the first time the code in the while loop is evaluated
sum += val; //so that the incremented value of val is added to the sum variable
Run Code Online (Sandbox Code Playgroud)
但代码按此顺序评估:
sum += …Run Code Online (Sandbox Code Playgroud) 根据 C++ Primer 一书,“箭头运算符需要一个指针操作数并产生一个左值”。总是这样吗?例如:
#include<iostream>
#include<string>
using std::string; using std::vector;
int main()
{
vector<string> temp {"aaa", "bbb", "ccc"};
vector<string>::iterator iter_str = temp.begin();
bool result = iter_str->empty(); //the result of iter_str->empty() is not an lvalue right?
return 0;
}
Run Code Online (Sandbox Code Playgroud)