将`vector`转换为`vector`。

huangapple go评论56阅读模式
英文:

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&lt;int&gt; and functions that only accepts a vector&lt;unsigned int&gt; 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&lt;unsigned int&gt; reference to a vector&lt;int&gt; 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&lt;vector&lt;int&gt;&gt; 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&lt;unsigned int&gt;&amp; data)
{
	 // ...
}

void main()
{
	std::vector&lt;int&gt; 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&lt;int&gt; != std::vector&lt;unsigned int&gt;)

#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;iterator&gt;
#include &lt;iostream&gt;

int main()
{
	std::vector&lt;int&gt; values{ 1,2,3,4,5 };
	std::vector&lt;unsigned int&gt; converted_values;

	// avoid raw for loops
	std::transform(values.begin(), values.end(), std::back_inserter(converted_values), [](const int value)
	{ 
		return static_cast&lt;unsigned int&gt;(value); 
	});

	for (const auto&amp; value : convered_values)
	{
		std::cout &lt;&lt; value &lt;&lt; &quot; &quot;;
	}

	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 &lt;ranges&gt;
template&lt;typename result&gt;
constexpr auto cast_trans = std::views::transform( // C++20
            [](auto const&amp; x)
            { return static_cast&lt;result&gt;(x); });

std::vector&lt;int&gt; vint;
auto ruint = vint | cast_trans&lt;unsigned&gt;; 

auto vuint = ruint | std::ranges::to&lt;std::vector&gt;; // C++23

Ranges library has got a lot explore.

答案4

得分: -1

以下是已翻译的内容:

没有强制转换不会导致未定义行为,但如果您真的想这样做,那么这可能在实践中有效。

reinterpret_cast<vector>(&vec)

创建一个指针,进行类型转换,然后解引用。我以前在做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&lt;vector&lt;unsigned int&gt;*&gt;(&amp;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.

huangapple
  • 本文由 发表于 2023年4月13日 19:23:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定