以下代码给出了悬空指针错误。
std::vector<std::pair<std::string, int>> c;
for (auto& b : c) {
const auto& [s, i] = b;
std::string_view v = s.substr(i);
std::cout << v;
}
Run Code Online (Sandbox Code Playgroud)
我认为它b保存了对 的引用std::pair<std::string, int>,所以s应该是对pair对象中字符串的引用。为什么这会产生悬空指针错误?我在这里缺少什么?godbolt 链接: https: //godbolt.org/z/4zMvbr
为什么下面的代码不起作用?我的意思是,它在控制台输出上显示各种奇怪的字符.
#include <stdio.h>
char mybuffer[80];
int main()
{
FILE * pFile;
pFile = fopen ("example.txt","r+");
if (pFile == NULL) perror ("Error opening file");
else {
fputs ("test",pFile);
fgets (mybuffer,80,pFile);
puts (mybuffer);
fclose (pFile);
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,下面的代码运行良好.
#include <stdio.h>
char mybuffer[80];
int main()
{
FILE * pFile;
pFile = fopen ("example.txt","r+");
if (pFile == NULL) perror ("Error opening file");
else {
fputs ("test",pFile);
fflush (pFile);
fgets (mybuffer,80,pFile);
puts (mybuffer);
fclose (pFile);
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我需要刷新流以获得正确的结果?
假设我们有两个具有相同字段(和类型)的结构。
struct A {
pub data1: i32;
pub data2: string;
}
struct B {
pub data1: i32;
pub data2: string;
}
Run Code Online (Sandbox Code Playgroud)
要复制结构 A 和 B,我必须这样做
fn convert_A_to_B(a: A) -> B {
B {
data1: a.data1,
data2: a.data2
}
}
Run Code Online (Sandbox Code Playgroud)
我希望我能做类似的事情
B {
..a
}
Run Code Online (Sandbox Code Playgroud)
但这是不可能的,因为类型不同。是否有任何宏或语法可以对所有相同字段进行简单移动?
假设我有这段代码:
pub trait A {}
pub trait B {}
pub trait SomeBehavior {
fn func() -> bool;
}
Run Code Online (Sandbox Code Playgroud)
我想提供这样的总体A实现B:
impl <T> SomeBehavior for T where T: A {
fn func() -> bool { true }
}
impl <T> SomeBehavior for T where T: B {
fn func() -> bool { false }
}
Run Code Online (Sandbox Code Playgroud)
但这会产生以下错误:
pub trait A {}
pub trait B {}
pub trait SomeBehavior {
fn func() -> bool;
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器将不同特征的两种不同实现视为相同实现?
#include <iostream>
using namespace std;
class A
{
protected:
int a;
};
class B : public A
{
public:
int func(A* p)
{
cout << p->a;
}
};
Run Code Online (Sandbox Code Playgroud)
我真的不明白为什么我不能通过'p-> a'访问'a'.
无论如何在B级中访问p的成员'a'而不将'protected'更改为'public'?
#include <iostream>
using namespace std;
class A
{
int x;
public:
A(int c) : x(c) {}
A(const A& a) { x = a.x; cout << "copy constructor called" << endl;}
};
class B
{
A a;
public:
B(int c) : a(c) {}
B(const B& b) : a(b.a) { }
A get_A() { return a;}
};
int main()
{
B b(10);
A a1 = b.get_A();
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我预计'复制构造函数调用'消息会弹出两次,因为首先,b.get_A()将创建一个临时对象,它调用复制构造函数(1),其次,它会将其引用复制到a1' s复制构造函数(2),从而显示两条消息.
但是,代码实际上会产生一个名为'复制构造函数'的消息.为什么?
在Windows上,
char c;
int i;
scanf("%d", &i);
scanf("%c", &c);
Run Code Online (Sandbox Code Playgroud)
计算机跳过从控制台检索字符,因为'\n'保留在缓冲区上.但是,我发现下面的代码效果很好.
char str[10];
int i;
scanf("%d", &i);
scanf("%s", str);
Run Code Online (Sandbox Code Playgroud)
就像上面的情况一样,'\n'仍然保留在缓冲区上但为什么scanf这次成功从控制台获取字符串?
为什么无法迭代字符串切片?
fn main() {
let text = "abcd";
for (i, c) in text.iter().enumerate() {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
给出一个错误
fn main() {
let text = "abcd";
for (i, c) in text.iter().enumerate() {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
如何使字符串切片可迭代?
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
void wrapper(T& u)
{
g(u);
}
class A {};
void g(const A& a) {}
int main()
{
const A ca;
wrapper(ca);
wrapper(A()); // Error
}
Run Code Online (Sandbox Code Playgroud)
嗨,我有一个问题,为什么最后一个语句给出编译器错误.
:18:10:错误:无法将类型为'A&'的非const左值引用绑定到类型'A'包装器(A())的右值;
我认为模板类型T将被推断为,const A&因为ca它也被推断为const A&.在这种情况下,为什么类型扣除失败?
请解释RVA和VA的含义
c++ ×4
rust ×3
c ×2
c++17 ×1
class ×1
compilation ×1
constructor ×1
executable ×1
inheritance ×1
iostream ×1
scanf ×1
stdio ×1
stdstring ×1
string-view ×1
templates ×1
temporary ×1
traits ×1