假设我们有一个指针,T* ptr;并且ptr, ptr+1, … ptr+(n-1)所有引用类型为T的有效对象.
是否有可能像STL一样访问它们array?或者执行以下代码:
std::array<T,n>* ay = (std::array<T,n>*) ptr
Run Code Online (Sandbox Code Playgroud)
调用未定义的行为?
可能重复:
常规强制转换与static_cast对比dynamic_cast
在C++中,为什么要使用static_cast <int>(x)而不是(int)x?
static_cast<float>(foo)和之间有什么区别float(foo)?我经常static_cast<float>在模板代码中看到或类似于从任何整数类型到某些特定的整数类型.例如,我想要一个函数在任何整数类型上执行浮点除法.我通常会看到这样的事情:
template <typename T>
float float_div(T lhs, T rhs)
{
// I normally see this
return static_cast<float>(lhs) / static_cast<float>(rhs);
// But I could do this
// return float(lhs) / float(rhs); // But I could do this
}
Run Code Online (Sandbox Code Playgroud)
使用float(lhs)或有任何优势static_cast<float>(lhs)吗?还有什么是float(lhs)初始化/转换的名称?
在c ++中将unsigned char数组转换为float数组的最佳方法是什么?
我现在有一个for循环如下
for (i=0 ;i< len; i++)
float_buff[i]= (float) char_buff[i];
Run Code Online (Sandbox Code Playgroud)
我还需要反转过程,即从unsigned char转换为float(float到8bit转换)
for (i=0 ;i< len; i++)
char_buff[i]= (unsigned char) float_buff[i];
Run Code Online (Sandbox Code Playgroud)
任何意见,将不胜感激
谢谢
这是我的代码:
#include <iostream>
#include <vector>
void cumulative_sum_with_decay(std::vector<double>& v)
{
for (auto i = 2; i < v.size(); i++) {
v[i] = 0.167 * v[i - 2] + 0.333 * v[i - 1] + 0.5 * v[i];
}
}
void printv(std::vector<double>& v)
{
std::cout << "{";
for (auto i = 0; i < v.size() - 1; i++) {
std::cout << i << ", ";
}
std::cout << v[v.size() - 1] << "}\n";
}
int main()
{
auto v = std::vector<double>{1, 2, …Run Code Online (Sandbox Code Playgroud) 我有一个代码,其中在函数的末尾我需要从 int 转换为数组的所有元素的两倍,以便能够在退出函数之前执行最终 push_back。我现在拥有的代码是:
template <class T, size_t dims> class A {
typedef typename std::array<int, dims> ArrayInt;
typedef typename std::array<double, dims> ArrayDouble;
typedef typename std::vector <ArrayDouble> VectorDouble;
/* ...*/
foo() {
/* ...*/
ArrayInt myArrayInt;
ArrayDouble myArrayDouble;
VectorDouble myVectorDouble;
/* Initialize myArrayInt
Do some other stuff */
for (int i = 0; i < dims; ++i)
myArrayDouble[i] = static_cast<double>(myArrayInt[i]);
myVectorDouble.push_back(myArrayDouble);
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我对这些行感到不舒服:
for (int i = 0; i < dims; ++i)
myArrayDouble[i] = static_cast<double>(myArrayInt[i]);
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点?
谢谢你。
class A : public X;
class B : public virtual A;
class C : public virtual A;
class D1 : public B, public C;
class D2 : public B, public C;
void *p1 = new D1; //after storing the pointers,
void *p2 = new D2; //there will be no exact type info.
A *pA1 = (A *) p1; // Cast 1
A *pA2 = (A *) p2;
X *pX1 = (X *) p1; // Cast 2
X *pX2 = (X …Run Code Online (Sandbox Code Playgroud) 这个问题可能会被问到,但我搜索到了,找不到答案.
我正在实现一个玩具虚拟机,其中OpCodes采用以下形式:
std::tuple<int8_t, int64_t, int64_t> // instruction op1, op2
Run Code Online (Sandbox Code Playgroud)
我正在尝试将一个double打包到其中一个操作数中,并在处理它时再次读回它.这不能可靠地工作.
double d = ...
auto a = static_cast<int64_t>(d);
auto b = static_cast<double>(a)
// sometimes, b != d
Run Code Online (Sandbox Code Playgroud)
有没有办法将double的位表示打包成int64_t,然后读取该位模式返回得到与之前完全相同的双精度数?
请原谅模糊的标题(我不知道如何解决这个问题).无论如何,在我的代码中我明确地声明了一些变量,两个是有符号/无符号的int变量,其他是有符号/无符号的char类型变量.
我的代码:
#include <iostream>
int main(void)
{
unsigned int number = UINT_MAX;
signed int number2 = INT_MAX;
unsigned char U = UCHAR_MAX;
signed char S = CHAR_MAX;
std::cout << number << std::endl;
std::cout << "The size in bytes of this variable is: " << sizeof(number) << std::endl << std::endl;
std::cout << number2 << std::endl;
std::cout << "The size in bytes of this variable is: " <<sizeof(number2) << std::endl << std::endl;
std::cout << U << std::endl;
std::cout …Run Code Online (Sandbox Code Playgroud) 鉴于以下内容:
int a = 10, b = 5, c = 3, d = 1;
int x = 3, y = 2, z = 2;
return (float) a/x + b/y + c/z + d;
Run Code Online (Sandbox Code Playgroud)
这可能会将我们的精度转换为浮动,然后以浮点精度执行我们的分割序列.
使用C++样式转换更新此方法的正确方法是什么?
这真的应该改写为:
return static_cast<float>(a) / static_cast<float>(b) + ... ?
Run Code Online (Sandbox Code Playgroud) 通常,特别是在Win32编程中,需要从一个opaque类型转换为另一个opaque类型.例如:
HFONT font = cast_here<HFONT>( ::GetStockObject( SYSTEM_FONT ) );
Run Code Online (Sandbox Code Playgroud)
static_cast和reinterpret_cast都适用于此并且具有完全相同的效果,因为HFONT是指向用于定义HFONT的虚拟结构的指针,而GetStockObject()返回的HGDIOBJ是一个void*指针.
哪一个 - static_cast或reinterpret_cast - 更可取?
我想使用strlen()在需要int值的函数中返回的数字.
functionName((strlen(word)+strlen(otherWord)+1));
Run Code Online (Sandbox Code Playgroud)
这段代码不起作用,因为它们返回size_t.
有没有办法将结果转换为int所以我可以进行加法运算并将它们用作int?
using namespace std;
int main()
{
float f1 = 5488955, f2 = 0.2197265620, f3 = 0;
f3 = f1 + f2;
f3 = f3 - f1;
cout << f3<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么 f3 值被打印为 0 而不是 0.2197265620
我有一个库的返回值,它是一个void指针.我知道它指的是一个简短的int; 我尝试以下列方式获取int值(用简单的赋值替换函数调用a void *):
short n = 1;
void* s = &n;
int k = *(int*)s;
Run Code Online (Sandbox Code Playgroud)
我尝试转换一个指向一个地址的空指针,其中有一个短的,我试图将指针转换为指向一个int,当我这样做时,输出变成一个垃圾值.虽然我理解为什么它表现得像我不知道是否有解决方案.