英文:
Cast vector<int> to vector<unsigned int>
问题
I have a vector<int>
and functions that only accept vector<unsigned int>
references. I know that I could change/template the functions (and this is likely the best thing to do), but ideally, I would have a way to cast/convert the vector<unsigned int>
reference to a vector<int>
reference. I know that all values in the vector are positive, and none of the functions will even come close to overflowing the integers.
I have tried using static_cast<vector<int>>
but that doesn't work.
Edit:
I say cast/convert of the references, but I am not looking to create a new copy and reference that.
英文:
I have a vector<int>
and functions that only accepts a vector<unsigned int>
references.I know that I could change/template the functions(and this is likely the best thing to do), but ideally I would have a way to cast/convert the vector<unsigned int>
reference to a vector<int>
reference. I know that all values in the vector are positive, and non of the functions will even come close to overflowing the intigers.
I have tried using static_cast<vector<int>>
but that doesnt work.
Edit:
I say cast/convert of the references but I am not looking to create a new copy and reference that.
答案1
得分: 3
你不能进行强制类型转换,但你提到可以进行转换,可以像这样轻松完成:
void foo(const std::vector<unsigned int>& data)
{
// ...
}
void main()
{
std::vector<int> vec;
foo({ vec.begin(), vec.end() });
}
英文:
you cannot cast, but you mention conversion is an option, which is easily done like this:
void foo(const std::vector<unsigned int>& data)
{
// ...
}
void main()
{
std::vector<int> vec;
foo({ vec.begin(), vec.end() });
}
答案2
得分: 1
使用std::transform来创建一个具有转换值的新向量副本。您不能也不应该使用reinterpret_cast在不相关的类型之间进行强制转换(std::vector<int> != std::vector<unsigned int>
)。
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
int main()
{
std::vector<int> values{ 1, 2, 3, 4, 5 };
std::vector<unsigned int> converted_values;
// 避免使用原始for循环
std::transform(values.begin(), values.end(), std::back_inserter(converted_values), [](const int value)
{
return static_cast<unsigned int>(value);
});
for (const auto& value : converted_values)
{
std::cout << value << " ";
}
return 0;
}
英文:
Use std::transform to make a new copy of the vector with converted values. You cannot and should not reinterpret_cast to cast between unrelated types (std::vector<int> != std::vector<unsigned int>
)
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
int main()
{
std::vector<int> values{ 1,2,3,4,5 };
std::vector<unsigned int> converted_values;
// avoid raw for loops
std::transform(values.begin(), values.end(), std::back_inserter(converted_values), [](const int value)
{
return static_cast<unsigned int>(value);
});
for (const auto& value : convered_values)
{
std::cout << value << " ";
}
return 0;
}
答案3
得分: 0
One C++20,23 approach would be to use ranges:
#include <ranges>
template<typename result>
constexpr auto cast_trans = std::views::transform( // C++20
[](auto const& x)
{ return static_cast<result>(x); });
std::vector<int> vint;
auto ruint = vint | cast_trans<unsigned>;
auto vuint = ruint | std::ranges::to<std::vector>; // C++23
Ranges library has got a lot to explore.
英文:
One C++20,23 approach would be to use ranges:
#incluce <ranges>
template<typename result>
constexpr auto cast_trans = std::views::transform( // C++20
[](auto const& x)
{ return static_cast<result>(x); });
std::vector<int> vint;
auto ruint = vint | cast_trans<unsigned>;
auto vuint = ruint | std::ranges::to<std::vector>; // C++23
Ranges library has got a lot explore.
答案4
得分: -1
以下是已翻译的内容:
没有强制转换不会导致未定义行为,但如果您真的想这样做,那么这可能在实践中有效。
reinterpret_cast<vector
创建一个指针,进行类型转换,然后解引用。我以前在做C程序员时经常这样做。
但实际上,您不应该这样做。
英文:
There is no cast that does not result in undefined behaviour, but if you really want to do this then this will probably work in practise
*reinterpret_cast<vector<unsigned int>*>(&vec)
Create a pointer, cast the pointer, then dereference. I used to do this sort of thing all the time when I was a C programmer.
Really though, you should not do this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论