首页
留言
关于
统计
友链
Search
1
[C++Quiz]#4、#116、#174
1 阅读
2
C/C++选手初学Python可能遇到的问题
0 阅读
3
[C++Quiz]#27、#41、#44
0 阅读
4
[C++Quiz]#185、#207、#219
0 阅读
5
[C++]std::variant
0 阅读
C/C++
数据结构与算法
CSLab
xv6
bustub
6.5840
数据库
doris
SQL
做个数据库
工具
CS
登录
Search
标签搜索
C++
cppquiz
xv6
算法
doris
论文
os
CS
leetcode
Git
tools
并发
6.5840
Koarz
累计撰写
24
篇文章
累计收到
4
条评论
首页
栏目
C/C++
数据结构与算法
CSLab
xv6
bustub
6.5840
数据库
doris
SQL
做个数据库
工具
CS
页面
留言
关于
统计
友链
搜索到
8
篇与
的结果
2023-07-13
[C++Quiz]#4、#116、#174
Question #174{card-describe title="描述"}According to the C++17 standard, what is the output of this program?{/card-describe}#include<iostream> void f(int& a, const int& b) { std::cout << b; a = 1; std::cout << b; } int main(){ int x = 0; f(x,x); }{card-default label="答案及解析:" width=""}01b是一个const引用,我们不能修改它,但是不能保证它不会被其他地方修改,a也是x的别名,所以可以通过a修改x使b中的值改变。{/card-default}Question #4 {card-describe title="描述"}According to the C++17 standard, what is the output of this program?{/card-describe}#include <iostream> void f(float) { std::cout << 1; } void f(double) { std::cout << 2; } int main() { f(2.5); f(2.5f); }{card-default label="答案及解析:" width=""}21emmm...过。{/card-default}Question #116{card-describe title="描述"}According to the C++17 standard, what is the output of this program?{/card-describe}#include <iostream> #include <utility> int y(int &) { return 1; } int y(int &&) { return 2; } template <class T> int f(T &&x) { return y(x); } template <class T> int g(T &&x) { return y(std::move(x)); } template <class T> int h(T &&x) { return y(std::forward<T>(x)); } int main() { int i = 10; std::cout << f(i) << f(20); std::cout << g(i) << g(20); std::cout << h(i) << h(20); return 0; }{card-default label="答案及解析:" width=""}112212T&&看起来像右值引用但是在函数模板中它其实是通用引用,它取决于用于实例化模板的类型。如果使用左值实例化,它会折叠成左值引用;如果使用右值实例化,它会折叠成右值引用。对于f函数,虽然参数20是右值,但是x始终是左值(尽管它本身是右值引用),所以无论是传入i还是20都会匹配到y(int &),g函数中move(x)会得到右值引用,所以两次都调用了y(int &&),std::forward(x)会保留原数据类型,i是左值,20是右值,所以输出12。{alert type="info"}贡献者的注释:这个示例演示了Scott Meyers建议使用std::forward进行转发引用,使用std::move进行右值引用的原则。{/alert}{/card-default}
2023年07月13日
1 阅读
0 评论
0 点赞
2023-07-13
[C++Quiz]#185、#207、#219
Question #219{card-describe title="描述"}According to the C++17 standard, what is the output of this program?{/card-describe}#include <iostream> template<typename T> T sum(T arg) { return arg; } template<typename T, typename ...Args> T sum(T arg, Args... args) { return arg + sum<T>(args...); } int main() { auto n1 = sum(0.5, 1, 0.5, 1); auto n2 = sum(1, 0.5, 1, 0.5); std::cout << n1 << n2; }{card-default label="答案及解析:" width=""}32这个很简单,n1,n2值即sum函数的返回值,这里有两个sum函数,最开始初始化时n1的第一个数据是double类型,所以实例化后就是 sum(double arg,...) ,返回值等于 0.5+sum(args...) ,执行嵌套语句,直到最后一个值匹配到 sum(T) ,我们可以看到所有数据的类型 T 其实都是由传入的第一个数据决定的,所以对于 n2 , int(0.5) 就是 0 啦所以输出32.{/card-default}Question #207{card-describe title="描述"}According to the C++17 standard, what is the output of this program?{/card-describe}#include <iostream> #include <map> using namespace std; int main() { map<int, int> m; cout << m[42]; }{card-default label="答案及解析:" width=""}0没什么好说的,秒了,map[42]不存在,对于这样的操作会 try_emplace 初始化为0。{/card-default}Question #185{card-describe title="描述"}According to the C++17 standard, what is the output of this program?{/card-describe}#include <iostream> template <typename T> void f() { static int stat = 0; std::cout << stat++; } int main() { f<int>(); f<int>(); f<const int>(); }{card-default label="答案及解析:" width=""}010模板对 f() 实例化时对类型为 int 和 const int 会实例化成不同的函数,所以他们有各自的 int stat,所以f(),f()调用的还是不同的函数。{/card-default}
2023年07月13日
0 阅读
0 评论
0 点赞
2023-07-12
[C++Quiz]#27、#41、#44
Question #41{card-describe title="描述"}According to the C++17 standard, what is the output of this program?{/card-describe}#include <iostream> int main() { std::cout << 1["ABC"]; }{card-default label="答案及解析:" width=""}B根据 标准 规定 1["ABC"] 就等同于 *(1+"ABC") 根据加法运算可以交换,所以就等同于 *("ABC"+1)即常见的"ABC"[1]。{/card-default}Question #27{card-describe title="描述"}According to the C++17 standard, what is the output of this program?{/card-describe}#include <iostream> struct A { virtual std::ostream &put(std::ostream &o) const { return o << 'A'; } }; struct B : A { virtual std::ostream &put(std::ostream &o) const { return o << 'B'; } }; std::ostream &operator<<(std::ostream &o, const A &a) { return a.put(o); } int main() { B b; std::cout << b; }{card-default label="答案及解析:" width=""}BB类型通过虚函数重写了A类型的 put 函数。对<<运算符进行了重载,重载之后对b进行<<操作会执行b的 put 函数输出“B”。{/card-default}Question #44{card-describe title="描述"}According to the C++17 standard, what is the output of this program?{/card-describe}#include <iostream> struct X { virtual void f() const { std::cout << "X"; } }; struct Y : public X { void f() const { std::cout << "Y"; } }; void print(const X &x) { x.f(); } int main() { X arr[1]; Y y1; arr[0] = y1; print(y1); print(arr[0]); }{card-default label="答案及解析:" width=""}YXarr[0] = y1的操作使 Y 类型的对象被转化成 X 类型,这个过程会丢掉对象的 Y 部分。这个过程一般称作 slicing (切片)。{/card-default}
2023年07月12日
0 阅读
0 评论
0 点赞
1
2