我正在使用此代码来尝试找出一个数字中有多少位.下面的十六进制数字已打开所有位.
for (var i = 0x1FFFFFFFFFFFFF, m = 0; i & 1; ++m, i >>>= 1);
Run Code Online (Sandbox Code Playgroud)
出于某种原因打印m给出32,但在SO帖子中我读了以下内容:
JavaScript中的所有数字实际上都是符合IEEE-754标准的浮点数双精度数.它们具有53位尾数,这意味着将精确表示任何大小约为9千万亿或更小的整数值.
除非我错误地实现了这个,否则我不明白为什么m当应该有53位时打印给出32.有人可以解释一下吗?
当bar超出范围时,我希望var丢失参考值,但是当我将其打印出来时,它正确地给出了分配给它的初始值.为什么是这样?
#include <iostream>
struct S
{
int const& var;
S(int const& bar = 5) : var(bar)
{}
};
int main()
{
S s;
std::cout << s.var; // 5
}
Run Code Online (Sandbox Code Playgroud) 我有简单的代码,我开车去,std::vector<std::string>>但它没有工作.我收到错误:
prog.cpp:在函数中
‘int main()’:
prog.cpp:14:9:错误:在‘f’
Fun f {"a", "b", "c"};^
prog.cpp 之前缺少模板参数:14:9:错误:预期‘;’之前‘f’
这是代码.谁能告诉我我做错了什么?
Run Code Online (Sandbox Code Playgroud)#include <string> #include <vector> template< class String = std::string, class List = std::vector<String> > class Fun : public List { }; int main() { Fun f {"a", "b", "c"}; }
如果我有一个像这样的指针数组:
char* p[3];
p[0] = new char;
p[1] = new char[10];
p[2] = &c;
Run Code Online (Sandbox Code Playgroud)
假设我无法使用std::string,我怎么知道如何在不看清楚定义的情况下解除分配?我如何知道使用delete或delete[]迭代数组,或者它是指向堆栈变量还是堆栈变量?
这是对TopCoder SRM 466“彩票”问题的提交。我已经看到这种模式多次用于解决这个问题。
尼克喜欢玩彩票。单张彩票的成本就是价格。尼克具有值恰好四个纸币
b1,b2,b3和b4(一些值可以是相等的)。他想知道是否有可能购买一张彩票而无需找零。换句话说,他想使用他的钞票的任何子集支付一张票的确切价格。如果可能,则返回“POSSIBLE”,否则返回“IMPOSSIBLE”(为清楚起见,所有引号)。
string buy(int p, int b1, int b2, int b3, int b4) {
int arr[] = {b1, b2, b3, b4};
for (int msk = 0; msk < (1 << 4); ++msk) {
int sum = 0;
for (int i = 0; i < 4; ++i) {
if (msk & (1 << i)) {
sum += arr[i];
}
}
if (sum == p) return "POSSIBLE";
}
return "IMPOSSIBLE"; …Run Code Online (Sandbox Code Playgroud) 可能重复:
STL删除无法按预期工作?
对不起,我是C++ 11和迭代器的新手.这应该删除数组中的所有数字3,但它不会删除最后一个.为什么?
#include <algorithm>
#include <array>
#include <iostream>
int main() {
std::array<int, 8> a{{9, 3, 4, 5, 33, 5, 6, 3}};
int N(3);
std::remove(a.begin(), a.end(), N);
for (int i : a) {
std::cout << i << '\n';
}
}
Run Code Online (Sandbox Code Playgroud)
我得到输出:
{ 9, 4, 5, 33, 5, 6, 6, 3 }
^
|
// the last 3 is still there
Run Code Online (Sandbox Code Playgroud) 我知道我不应该使用C风格的数组,但我还是试图寻找一种方法来做到这一点.我正在尝试按字母顺序排列const char*阵列,但std::sort没有做到正确.我究竟做错了什么?
#include <iostream>
#include <algorithm>
int main() {
const char * str[5] = {"alpha", "gamma", "beta", "delta", "chi"};
int size = sizeof(str)/sizeof(*str);
std::sort(str, str + size);
for (int i = 0; i < size; i++) std::cout << str[i] << ", ";
}
Run Code Online (Sandbox Code Playgroud)
它根本不会改变阵列.我做得不对劲?
我遇到了虚拟方法的问题.当我打电话f它不起作用.为什么?
#include <iostream>
struct A {
virtual void f() const { std::cout << "In A"; }
virtual ~A() {};
};
struct B : A {
void f() const { std::cout << "In B"; }
};
int main()
{
A* a = new A();
B* b = dynamic_cast<B*>(a);
(*b).f();
delete a;
}
Run Code Online (Sandbox Code Playgroud)
它根本不打印任何东西,我也没有任何错误.我做错了什么?
我有十六进制值0x48656c6c6f,每个字节代表字符串中每个字符的ASCII值"Hello".我也有一个char我想要插入这些值的数组.
当我有一个较小的十六进制值(例如,0x48656c6c代表"Hell")时,打印出char数组会得到正确的输出.但是下面的代码打印"olle"(在little-endian中)但不是"olleH".为什么是这样?
#include <iostream>
#include <cstring>
int main()
{
char x[6] = {0};
int y = 0x48656c6c6f;
std::memcpy(x, &y, sizeof y);
for (char c : x)
std::cout << c;
}
Run Code Online (Sandbox Code Playgroud)
我知道unique_ptrs不能复制只移动,他们没有引用计数.但我们可以有两个共享资源的智能指针:
Foo* f = new Foo;
auto p1 = std::unique_ptr<Foo>(f);
auto p2 = std::unique_ptr<Foo>(f);
Run Code Online (Sandbox Code Playgroud)
现在这两个类共享一个指针*f.此外,我知道这最终将导致UB,因为我们将进行双重删除但仍然:unique_ptr如果可能的话,我们真正意味着"独特"是什么意思?