简体   繁体   English

C++ typecast数组

[英]c++ typecast array

How can one typecast an array of int to an array of float?如何将 int 数组类型转换为 float 数组? Thanks.谢谢。

#include <algorithm>
#include <iostream>

#define N 50

int main() {
    int intArray[N] = { ... };
    float floatArray[N];
    std::copy(intArray, intArray + N, floatArray);
    std::cout
        << std::boolalpha << std::equal(intArray, intArray + N, floatArray)
        << std::endl;
    return 0;
}

If you have an array of int s, what you basically have is a block of N int s stored contiguously in memory.如果你有一个int数组,你基本上拥有的是一块 N int连续存储在内存中。 An array of floats, however, would be N floats stored contiguously in memory, ie an entirely different sequence of bits in memory.然而,浮点数组将是连续存储在内存中的 N 个浮点数,即内存中完全不同的位序列。 Additionally, floating point values are represented in binary in an entirely different way than integral values.此外,浮点值以与整数值完全不同的方式以二进制表示。 In fact, you can't even be sure that an int is the same size as a float.实际上,您甚至无法确定intfloat.大小相同float.

So therefore, you either have to cast each int to a float separately as you process the array, or else create an entirely different array by copying the original array.因此,您要么在处理数组时分别将每个int转换为float ,要么通过复制原始数组创建一个完全不同的数组。

For example, you could simply convert each int to a float lazily as you process the array:例如,您可以在处理数组时简单地将每个 int 转换为浮点数:

int array[100];
// ... fill array with some values

for (int i = 0; i < 100; ++i)
{
  float f = array[i]; // implicit conversion here 
  // now do something with this float
}

If you use vectors instead of arrays you can then use the iterator in the constructor of the vector to do the copy.如果使用向量而不是数组,则可以在向量的构造函数中使用迭代器进行复制。

std::vector<int> vi;
vi.push_back(1);
vi.push_back(2);
vi.push_back(3);

std::vector<float> vf(vi.begin(), vi.end());
assert(vf.size() == 3);

If you have as input an array, but you can have as output a vector, you could also do this:如果你有一个数组作为输入,但你可以有一个向量作为输出,你也可以这样做:

int ai[] = {1,2,3};
std::vector<float> vf(ai, ai+3);
assert(vf.size() == 3);

If you need as input and output an array, you can use std::copy , but just make sure your output array is big enough:如果你需要一个数组作为输入和输出,你可以使用std::copy ,但只要确保你的输出数组足够大:

int ai[] = {1,2,3};
float af[] = {0, 0, 0};
std::copy(ai, ai+3, af);

Note: std::copy , and the vector constructor will not blindly copy the memory, it will implicitly cast between the 2 types for each element .注意: std::copy和 vector 构造函数不会盲目复制内存,它会在每个元素的 2 种类型之间隐式转换。 It performs the assignments *result = *first, *(result + 1) = *(first + 1), and so on...它执行赋值 *result = *first, *(result + 1) = *(first + 1),依此类推...

IMO, Use tranform and convert int vector to float vector. IMO,使用转换并将 int 向量转换为浮点向量。

float convert (int i) { return static_cast<float>(i); }

int main () {
  int first[10];
  float second[10];
    // set some values:
  for (int i=0; i<10; i++) 
      first[i] =  (i*10); 

  transform (first, first + 10, second, convert);

  return 0;
}

You cannot.你不能。

You will have to create another array, and manually copy elements with a loop.您将不得不创建另一个数组,并使用循环手动复制元素。

In C++, compiler typically does not put in loops in resulting binary without having you explicitly see that in your code.在 C++ 中,编译器通常不会在生成的二进制文件中放入循环,除非您在代码中明确看到它。

With c++17 and std::array (or any similar class) this problem can be solved generically.使用 c++17 和std::array (或任何类似的类)可以一般地解决这个问题。

template <typename Y, typename X, std::size_t N, template <typename, std::size_t> typename A, std::size_t... Is>
constexpr A<Y, N> elements_cast(const A<X, N> &a, std::index_sequence<Is...>) {
    return {std::get<Is>(a)...};
}

template <typename Y, typename X, std::size_t N, template <typename, std::size_t> typename A, typename Indices = std::make_index_sequence<N>>
constexpr A<Y, N> elements_cast(const A<X, N> &a) {
    return elements_cast<Y>(a, Indices{});
}

One would use elements_cast like this:人们会像这样使用elements_cast

std::array<int, 5> array_of_ints;
auto array_of_floats = elements_cast<float>(array_of_ints);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM