C++ 如何将 std::array 的内容复制到另一个?
C++ how to copy contents of std::array to another?
关于如何将一个标准的 c 数组复制到另一个数组是众所周知的:
char test[20] = "asdasd";
char test2[19] = "asdassdsdfd";
strcpy_s(test, sizeof(test), test2);
但是我怎样才能对 std::array
做同样的事情呢? (最好没有 for 循环)
std::array<char, 20> test = {"asdasd"};
std::array<char, 19> test2 = {"asdassdsdfd"};
// copy test2 into test
这是对您正在尝试做的事情的更详尽的回答。
不过,我真的不建议使用数组来保存字符串。
#include <array>
#include <iostream>
// copy solution without loops, though probably it would have been more readable with them :)
template<typename type_t, std::size_t N1, std::size_t N2>
void copy_array(const std::array<type_t, N1>& source, std::array<type_t, N2>& destination)
{
// copy to a larger destination
if constexpr (N1 <= N2)
{
// this will copy the whole array! not just the characters from the string literal!
std::copy(source.begin(), source.end(), destination.begin());
// fill remainder of destination with 0's
auto it = destination.begin();
std::advance(it, N1);
std::fill(it, destination.end(), 0);
}
else
// copy into a smaller array, then copy only the beginning
// note this will also result in an array without trailing 0's
// an array is NOT a string.
{
auto end = source.begin();
std::advance(end, N2);
std::copy(source.begin(), end, destination.begin());
}
}
int main()
{
// Note you're probably better of just using const std::strings instead of std::arrays
// this also avoids the pain involved in copying rules for mismatching array sizes.
// The arrays initialized with string literals smaller then their size initialize the
// rest of the array to 0's
std::array<char, 20> test{ "asdasd" };
std::array<char, 19> test2{ "asdassdsdfd" };
copy_array(test2, test);
for (const auto c : test)
{
std::cout << c;
}
}
方法有很多种。您可以使用 strcpy_s(test.data(), sizeof(test), test2.data())
,但我不推荐它。基本相同的更通用版本是 std::copy_n(test.begin(), test.size(), test2.begin());
,即使 std::array
中的类型发生变化,它也将继续正确。鉴于它们的大小是静态的,我会投入 static_assert(test.size() <= test2.size());
以备不时之需。
我可以提供使用算法深度优先搜索。毫无疑问,它是不合理的,但它执行了当前问题中描述的要求。
#include <iostream>
#include <array>
#include <vector>
using namespace std;
const int maximumSize=20;
//copy test2 into test
array<char, 20> test = {"asdasd"};
array<char, 19> test2 = {"asdassdsdfd"};
vector<int> visited(maximumSize, 0);
template<typename Type>
void showContent1D(Type input)
{
for(int i=0; i<input.size(); ++i)
{
cout<<input[i]<<", ";
}
return;
}
void depthFirstSearch(int firstIndex)
{
if(visited[firstIndex]==1)
{
return;
}
visited[firstIndex]=1;
test[firstIndex]=test2[firstIndex];
for(int index=0; index<test2.size(); ++index)
{
depthFirstSearch(index);
}
return;
}
int main()
{
cout<<"Before copying:"<<endl<<"test <- ";
showContent1D(test);
cout<<endl<<"test2 <- ";
showContent1D(test2);
cout<<endl<<"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"<<endl;
depthFirstSearch(0);
cout<<"After copying:"<<endl<<"test <- ";
showContent1D(test);
cout<<endl<<"test2 <- ";
showContent1D(test2);
return 0;
}
输出在这里:
Before copying:
test <- a, s, d, a, s, d, , , , , , , , , , , , , , ,
test2 <- a, s, d, a, s, s, d, s, d, f, d, , , , , , , , ,
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
After copying:
test <- a, s, d, a, s, s, d, s, d, f, d, , , , , , , , , ,
test2 <- a, s, d, a, s, s, d, s, d, f, d, , , , , , , , ,
关于如何将一个标准的 c 数组复制到另一个数组是众所周知的:
char test[20] = "asdasd";
char test2[19] = "asdassdsdfd";
strcpy_s(test, sizeof(test), test2);
但是我怎样才能对 std::array
做同样的事情呢? (最好没有 for 循环)
std::array<char, 20> test = {"asdasd"};
std::array<char, 19> test2 = {"asdassdsdfd"};
// copy test2 into test
这是对您正在尝试做的事情的更详尽的回答。 不过,我真的不建议使用数组来保存字符串。
#include <array>
#include <iostream>
// copy solution without loops, though probably it would have been more readable with them :)
template<typename type_t, std::size_t N1, std::size_t N2>
void copy_array(const std::array<type_t, N1>& source, std::array<type_t, N2>& destination)
{
// copy to a larger destination
if constexpr (N1 <= N2)
{
// this will copy the whole array! not just the characters from the string literal!
std::copy(source.begin(), source.end(), destination.begin());
// fill remainder of destination with 0's
auto it = destination.begin();
std::advance(it, N1);
std::fill(it, destination.end(), 0);
}
else
// copy into a smaller array, then copy only the beginning
// note this will also result in an array without trailing 0's
// an array is NOT a string.
{
auto end = source.begin();
std::advance(end, N2);
std::copy(source.begin(), end, destination.begin());
}
}
int main()
{
// Note you're probably better of just using const std::strings instead of std::arrays
// this also avoids the pain involved in copying rules for mismatching array sizes.
// The arrays initialized with string literals smaller then their size initialize the
// rest of the array to 0's
std::array<char, 20> test{ "asdasd" };
std::array<char, 19> test2{ "asdassdsdfd" };
copy_array(test2, test);
for (const auto c : test)
{
std::cout << c;
}
}
方法有很多种。您可以使用 strcpy_s(test.data(), sizeof(test), test2.data())
,但我不推荐它。基本相同的更通用版本是 std::copy_n(test.begin(), test.size(), test2.begin());
,即使 std::array
中的类型发生变化,它也将继续正确。鉴于它们的大小是静态的,我会投入 static_assert(test.size() <= test2.size());
以备不时之需。
我可以提供使用算法深度优先搜索。毫无疑问,它是不合理的,但它执行了当前问题中描述的要求。
#include <iostream>
#include <array>
#include <vector>
using namespace std;
const int maximumSize=20;
//copy test2 into test
array<char, 20> test = {"asdasd"};
array<char, 19> test2 = {"asdassdsdfd"};
vector<int> visited(maximumSize, 0);
template<typename Type>
void showContent1D(Type input)
{
for(int i=0; i<input.size(); ++i)
{
cout<<input[i]<<", ";
}
return;
}
void depthFirstSearch(int firstIndex)
{
if(visited[firstIndex]==1)
{
return;
}
visited[firstIndex]=1;
test[firstIndex]=test2[firstIndex];
for(int index=0; index<test2.size(); ++index)
{
depthFirstSearch(index);
}
return;
}
int main()
{
cout<<"Before copying:"<<endl<<"test <- ";
showContent1D(test);
cout<<endl<<"test2 <- ";
showContent1D(test2);
cout<<endl<<"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"<<endl;
depthFirstSearch(0);
cout<<"After copying:"<<endl<<"test <- ";
showContent1D(test);
cout<<endl<<"test2 <- ";
showContent1D(test2);
return 0;
}
输出在这里:
Before copying:
test <- a, s, d, a, s, d, , , , , , , , , , , , , , ,
test2 <- a, s, d, a, s, s, d, s, d, f, d, , , , , , , , ,
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
After copying:
test <- a, s, d, a, s, s, d, s, d, f, d, , , , , , , , , ,
test2 <- a, s, d, a, s, s, d, s, d, f, d, , , , , , , , ,