将转换复制到 C++ 中的标准容器
Copy conversion to standard containers in C++
这个问题是 How to convert an array into a vector in C++? 的延续,有人建议我使用非常棘手的技术和一些 sizeof()
操作。
实际上我希望在标准库中找到一些函数来将数组转换为任何容器。我试着自己写了一个,看起来并不难:
#include <list>
#include <vector>
// from C-array to std-container
template<typename T, int N>
auto make_copy(auto (&from)[N])
{ return T{ std::begin(from), std::end(from) }; }
// from std::container to another std::container
template<typename T>
auto make_copy(const auto &from)
{ return T{ std::begin(from), std::end(from) }; }
int main()
{
int myArray[2] = { 1, 2 };
auto myVector = make_copy<std::vector<int>>( myArray );
auto myList = make_copy<std::list<int>>( myVector );
return myList.size();
}
https://gcc.godbolt.org/z/fv4ddadax
标准 C++ 库或 boost
中是否有类似于 make_copy
的内容?
where I was suggested to use very tricky technique with a few sizeof() manipulations.
sizeof
对于数组来说完全没有必要。如果您需要元素数量的大小,可以使用 std::size
。但在这种情况下,我会推荐您在示例中使用的内容:(从技术上讲,myArray
与 std::begin(myArray)
一样有效,但后者对于一致性很好):
std::vector myVector(std::begin(myArray), std::end(myArray));
Is there anything similar to make_copy in the standard C++ library
没有
根据你的建议,你不需要数组重载,因为更通用的方法同样有效。您可以通过使用 std::ranges::begin
和 end
来支持不适用于 std::begin
和 end
的范围来改进它。
or in boost?
是:
auto myVector = boost::copy_range<std::vector<int>>(myArray);
您可以使用 range-v3 的 ranges::to
:
#include <range/v3/range/conversion.hpp>
#include <list>
int main() {
int myArray[2] = {1, 2};
auto myVector = ranges::to<std::vector>(myArray);
auto myList = ranges::to<std::list>(myVector);
return myList.size();
}
这个问题是 How to convert an array into a vector in C++? 的延续,有人建议我使用非常棘手的技术和一些 sizeof()
操作。
实际上我希望在标准库中找到一些函数来将数组转换为任何容器。我试着自己写了一个,看起来并不难:
#include <list>
#include <vector>
// from C-array to std-container
template<typename T, int N>
auto make_copy(auto (&from)[N])
{ return T{ std::begin(from), std::end(from) }; }
// from std::container to another std::container
template<typename T>
auto make_copy(const auto &from)
{ return T{ std::begin(from), std::end(from) }; }
int main()
{
int myArray[2] = { 1, 2 };
auto myVector = make_copy<std::vector<int>>( myArray );
auto myList = make_copy<std::list<int>>( myVector );
return myList.size();
}
https://gcc.godbolt.org/z/fv4ddadax
标准 C++ 库或 boost
中是否有类似于 make_copy
的内容?
where I was suggested to use very tricky technique with a few sizeof() manipulations.
sizeof
对于数组来说完全没有必要。如果您需要元素数量的大小,可以使用 std::size
。但在这种情况下,我会推荐您在示例中使用的内容:(从技术上讲,myArray
与 std::begin(myArray)
一样有效,但后者对于一致性很好):
std::vector myVector(std::begin(myArray), std::end(myArray));
Is there anything similar to make_copy in the standard C++ library
没有
根据你的建议,你不需要数组重载,因为更通用的方法同样有效。您可以通过使用 std::ranges::begin
和 end
来支持不适用于 std::begin
和 end
的范围来改进它。
or in boost?
是:
auto myVector = boost::copy_range<std::vector<int>>(myArray);
您可以使用 range-v3 的 ranges::to
:
#include <range/v3/range/conversion.hpp>
#include <list>
int main() {
int myArray[2] = {1, 2};
auto myVector = ranges::to<std::vector>(myArray);
auto myList = ranges::to<std::list>(myVector);
return myList.size();
}