我想知道直接投射bool到float是否可以安全使用。
这是我的代码:
#include <iostream>
using namespace std;
int main()
{
bool b1 = false;
bool b2 = true;
float f1 = static_cast<float>(b1);
float f2 = static_cast<float>(b2);
cout << "False in float : " << f1 << endl;
cout << "True in float : " << f2 << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果:
False in float : 0
True in float : 1
Run Code Online (Sandbox Code Playgroud)
结果在所有C ++编译器和平台上都一样吗?
我们知道我们可以使用指针动态创建变量,例如:
int *p = new int(5);
Run Code Online (Sandbox Code Playgroud)
我们可以使用*as访问它的值
cout << *p;
Run Code Online (Sandbox Code Playgroud)
但这不是数组的情况,请考虑以下代码:
int size_array = 5;
int * p = new int[size_array];
Run Code Online (Sandbox Code Playgroud)
如果我们需要访问第一个元素的值,我们执行以下操作:
cout << p[0];
Run Code Online (Sandbox Code Playgroud)
但是为什么我们不能对像上面这样的动态变量做同样的事情?,即使用*:
cout << *p[0];
Run Code Online (Sandbox Code Playgroud) 我才刚刚开始我的编程之旅。我在 Ubuntu 终端中编码。我在编译使用该gets()函数的程序时遇到问题。
#include<stdio.h>
/*example of multi char i/p function*/
void main()
{
char loki[10];
gets(loki);
printf("puts(loki)");
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
warning: 'implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
Run Code Online (Sandbox Code Playgroud) 我是 Rust 的新手,看起来我在这里严重遗漏了一些概念。
use std::thread;
fn main() {
let mut children = vec![];
//spawn threads
for i in 0..10 {
let c = thread::spawn(|| {
println!("thread id is {}", i);
});
children.push(c);
}
for j in children {
j.join().expect("thread joining issue");
}
}
Run Code Online (Sandbox Code Playgroud)
它失败并出现错误:
error[E0373]: closure may outlive the current function, but it borrows `i`, which is owned by the current function
Run Code Online (Sandbox Code Playgroud)
由于iis的类型i32,并且不涉及引用,因此 Rust 不应该复制该值而不是强制为move?
这是受到我给新手用户的回答的启发,我建议他们使用 anstd::variant而不是 union。
通过工会,您可能会遇到如下情况:
struct Box {
struct Item { float value; };
using Boxes = std::vector<Box>;
union Value {
Item item;
Boxes boxes;
};
Value contents;
std::string label;
};
Run Code Online (Sandbox Code Playgroud)
(不完全是最初的问题,我在这里接受了一些诗意的许可。)并且使用一个变体,该类可能如下所示:
struct Box {
struct Item { float value; };
using Boxes = std::vector<Box>;
std::variant<Item, Boxes> contents;
std::string label;
};
Run Code Online (Sandbox Code Playgroud)
问题是,使用第一个变体,我可以写
if (box.contents.boxes.size() > 2) { foo(); }
Run Code Online (Sandbox Code Playgroud)
如果我已经确定会有子框,这就会起作用。
对于std::variant,我必须写:
if (std::get<Boxes>(box.contents).size() > 2) { foo(); }
Run Code Online (Sandbox Code Playgroud)
我觉得第二个版本的可读性要差得多,有点混乱,而且很分散注意力。另外 - 我必须知道 的类型boxes。
在我的代码中,我可以做些什么来让我的用户无需进行此类std::get()调用,并使他们的生活更加愉快?
对于 C++ - 我可以将每个添加char到string数组中的每个索引:
string *x=new string[10];
x[0] += "a";
x[0] += "b";
x[0] += "c";
x[1] += "x";
x[1] += "y";
x[1] += "z";
cout << "x[0]=" << x[0] << endl; // would be "abc"
cout << "x[1]=" << x[1] << endl; // would be "xyz"
Run Code Online (Sandbox Code Playgroud)
如何在 C 中执行相同的功能?我有一个buff2指向char数组的指针,并试图char从buf. 当我打印出buff2价值时,我不断得到奇怪的价值。
char buf[255];
char *buff2;
int i=0, count=0;
buff2=(char*)malloc(512*sizeof(char));
while((n = read(fd, buf, sizeof(buf[g]))) > 0){ …Run Code Online (Sandbox Code Playgroud) 虽然我使用带有编译器标志的 VS2019 /std:c++latest,但我无法成功包含标头<source_location>。编译出现如下错误:
fatal error C1083: Cannot open include file: 'source_location': No such file or directory
Run Code Online (Sandbox Code Playgroud) 我正在尝试添加pre-commit脚本。
该脚本有效,但 git 忽略了对文件的更改,似乎 git 忽略了我.git在.gitignore.
例如:
# add a hook that runs hello world
echo "#\!/bin/sh \n echo 'hello world'" > .git/hooks/pre-commit
# make the hook runnable
chmod +x .git/hooks/pre-commit
# check changes
git status
# > Your branch is up to date with 'origin/master'.
# > nothing to commit, working tree clean
Run Code Online (Sandbox Code Playgroud)
因此,我如何将钩子提交到存储库?