在结构数组上使用 c++ std::copy
Using c++ std::copy on an array of structure
我想使用 std::copy 将现有结构数组复制到新结构数组。常规选项可以使用“'local_copy()'”。我想知道在下面描述的这种情况下使用 std::copy 的程序 -
我尝试了代码并在编译时得到以下错误
#include <iostream>
class BigClass{
public:
struct Astruct{
double x[2], v[2];
int rank;
}one_struct;
};
void allocate(struct BigClass::Astruct& one_struct, int i)
{
one_struct.x[0] = 1.1;
one_struct.x[1] = 1.2;
one_struct.v[0] = 2.1;
one_struct.v[1] = 2.2;
one_struct.rank = i;
}
void local_copy(struct BigClass::Astruct& dest, struct BigClass::Astruct& source)
{
dest.x[0] = source.x[0];
dest.x[1] = source.x[1];
dest.v[0] = source.v[0];
dest.v[1] = source.v[1];
dest.rank = source.rank;
}
void print(struct BigClass::Astruct one_struct)
{
std::cout << one_struct.rank << " " << one_struct.x[0] << " " << one_struct.x[1] << " " << one_struct.v[0] << " " << one_struct.v[1] << "\n";
}
int main(int argc, char *argv[]) {
int size = 10;
struct BigClass::Astruct BCobj[size];
for(int i = 0; i < size; i++) allocate(BCobj[i], i);
for(int i = 0; i < size; i++) print(BCobj[i]);
struct BigClass::Astruct second_BCobj[size];
//for(int i = 0; i < size; i++) local_copy(second_BCobj[i], BCobj[i]); // this works
for(int i = 0; i < size; i++) std::copy(BCobj[i+1], BCobj[i], second_BCobj[i]); // not working
for(int i = 0; i < size; i++) print(BCobj[i]);
}
编译时错误如下-
/usr/include/c++/7/bits/stl_algobase.h:377:57: error: no type named ‘value_type’ in ‘struct std::iterator_traits<BigClass::Astruct>’
typedef typename iterator_traits<_II>::value_type _ValueTypeI;
^~~~~~~~~~~
/usr/include/c++/7/bits/stl_algobase.h:378:57: error: no type named ‘value_type’ in ‘struct std::iterator_traits<BigClass::Astruct>’
typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
^~~~~~~~~~~
/usr/include/c++/7/bits/stl_algobase.h:379:64: error: no type named ‘iterator_category’ in ‘struct std::iterator_traits<BigClass::Astruct>’
typedef typename iterator_traits<_II>::iterator_category _Category;
^~~~~~~~~
/usr/include/c++/7/bits/stl_algobase.h:383:9: error: no type named ‘value_type’ in ‘struct std::iterator_traits<BigClass::Astruct>’
const bool __simple = (__is_trivial(_ValueTypeI)
~~~~~~~~~~~~~~~~~~~~~~~~~~
&& __is_pointer<_II>::__value
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
&& __is_pointer<_OI>::__value
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
&& __are_same<_ValueTypeI, _ValueTypeO>::__value);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/7/bits/stl_algobase.h:386:44: error: no type named ‘iterator_category’ in ‘struct std::iterator_traits<BigClass::Astruct>’
return std::__copy_move<_IsMove, __simple,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_Category>::__copy_m(__first, __last, __result);
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
提供了向量和 int 数据类型的一些示例。谁能为这种代码结构详细说明 std::copy 的适当顺序?
谢谢
std::copy
用于 而不是 for
循环,而不是一个
std::copy(BCobj, BCobj + size, second_BCobj);
与
基本相同
for(int i = 0; i < size; ++i) {
second_BCobj[i] = BCobj[i];
}
此外,别忘了 #include <algorithm>
for std::copy
std::copy
参数的解释:
std::copy
takes as arguments 2 arguments of type matching InputIterator requirements and single argument of type OutputIteretor。幸运的是,指针符合这些要求,所以我们可以直接使用它们。因为数组名称被解释为指向其第一个元素的指针,我们可以将它直接作为参数传递给 std::sort
Chef Gladiator 建议的更好版本:
std::copy(std::begin(BCobj), std::end(BCobj), std::begin(second_BCobj))
欢迎来到 Stack Overflow。您已经提交了完整的代码。下次还请描述您正在构建的开发环境。更好的是,尝试提供与操作系统和编译器无关的标准 C++ 代码。
这是C++论坛,所以我们喜欢在这里使用标准的C++。使用 std::array. That makes it simple. Please study and enjoy the standard C++.
重写您的代码
#include <array>
#include <iostream>
#include <algorithm>
namespace stack_overflow {
using namespace std;
struct BigClass final {
struct Astruct final {
double x[2], v[2];
int rank;
}one_struct;
friend ostream& operator << (ostream& os, Astruct const & one_struct)
{
return os << one_struct.rank << " " << one_struct.x[0] << " "
<< one_struct.x[1] << " " << one_struct.v[0] << " "
<< one_struct.v[1] ;
}
};
constexpr int size = 10;
using bcobj_arr = std::array<BigClass::Astruct, size>;
void populate( bcobj_arr& bcobjects_)
{
int j{ 0 };
for (auto& one_struct : bcobjects_) {
one_struct.x[0] = 1.1;
one_struct.x[1] = 1.2;
one_struct.v[0] = 2.1;
one_struct.v[1] = 2.2;
one_struct.rank = j++;
}
}
void print(const char prompt[BUFSIZ], bcobj_arr const & bcobjects_ )
{
cout << "\n\n" << prompt << "\n\n" ;
for (auto& one_struct : bcobjects_) {
cout << one_struct << "\n";
}
}
bool test (int argc, const char* argv[])
{
bcobj_arr BCobj;
populate(BCobj);
print("BCobj", BCobj);
// std::array instances can be copied
bcobj_arr second_BCobj = BCobj ;
print("second_BCobj", second_BCobj);
return true;
}
}
int main(const int argc, const char * argv[])
{
stack_overflow::test(argc, argv);
return 42;
}
代码完全没有注释。我想你有很多问题,请在下面的评论中提问。我会通过指向您在线的相关文档来尝试回答所有问题。
我想使用 std::copy 将现有结构数组复制到新结构数组。常规选项可以使用“'local_copy()'”。我想知道在下面描述的这种情况下使用 std::copy 的程序 -
我尝试了代码并在编译时得到以下错误
#include <iostream>
class BigClass{
public:
struct Astruct{
double x[2], v[2];
int rank;
}one_struct;
};
void allocate(struct BigClass::Astruct& one_struct, int i)
{
one_struct.x[0] = 1.1;
one_struct.x[1] = 1.2;
one_struct.v[0] = 2.1;
one_struct.v[1] = 2.2;
one_struct.rank = i;
}
void local_copy(struct BigClass::Astruct& dest, struct BigClass::Astruct& source)
{
dest.x[0] = source.x[0];
dest.x[1] = source.x[1];
dest.v[0] = source.v[0];
dest.v[1] = source.v[1];
dest.rank = source.rank;
}
void print(struct BigClass::Astruct one_struct)
{
std::cout << one_struct.rank << " " << one_struct.x[0] << " " << one_struct.x[1] << " " << one_struct.v[0] << " " << one_struct.v[1] << "\n";
}
int main(int argc, char *argv[]) {
int size = 10;
struct BigClass::Astruct BCobj[size];
for(int i = 0; i < size; i++) allocate(BCobj[i], i);
for(int i = 0; i < size; i++) print(BCobj[i]);
struct BigClass::Astruct second_BCobj[size];
//for(int i = 0; i < size; i++) local_copy(second_BCobj[i], BCobj[i]); // this works
for(int i = 0; i < size; i++) std::copy(BCobj[i+1], BCobj[i], second_BCobj[i]); // not working
for(int i = 0; i < size; i++) print(BCobj[i]);
}
编译时错误如下-
/usr/include/c++/7/bits/stl_algobase.h:377:57: error: no type named ‘value_type’ in ‘struct std::iterator_traits<BigClass::Astruct>’
typedef typename iterator_traits<_II>::value_type _ValueTypeI;
^~~~~~~~~~~
/usr/include/c++/7/bits/stl_algobase.h:378:57: error: no type named ‘value_type’ in ‘struct std::iterator_traits<BigClass::Astruct>’
typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
^~~~~~~~~~~
/usr/include/c++/7/bits/stl_algobase.h:379:64: error: no type named ‘iterator_category’ in ‘struct std::iterator_traits<BigClass::Astruct>’
typedef typename iterator_traits<_II>::iterator_category _Category;
^~~~~~~~~
/usr/include/c++/7/bits/stl_algobase.h:383:9: error: no type named ‘value_type’ in ‘struct std::iterator_traits<BigClass::Astruct>’
const bool __simple = (__is_trivial(_ValueTypeI)
~~~~~~~~~~~~~~~~~~~~~~~~~~
&& __is_pointer<_II>::__value
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
&& __is_pointer<_OI>::__value
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
&& __are_same<_ValueTypeI, _ValueTypeO>::__value);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/7/bits/stl_algobase.h:386:44: error: no type named ‘iterator_category’ in ‘struct std::iterator_traits<BigClass::Astruct>’
return std::__copy_move<_IsMove, __simple,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_Category>::__copy_m(__first, __last, __result);
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
提供了向量和 int 数据类型的一些示例。谁能为这种代码结构详细说明 std::copy 的适当顺序? 谢谢
std::copy
用于 而不是 for
循环,而不是一个
std::copy(BCobj, BCobj + size, second_BCobj);
与
基本相同for(int i = 0; i < size; ++i) {
second_BCobj[i] = BCobj[i];
}
此外,别忘了 #include <algorithm>
for std::copy
std::copy
参数的解释:
std::copy
takes as arguments 2 arguments of type matching InputIterator requirements and single argument of type OutputIteretor。幸运的是,指针符合这些要求,所以我们可以直接使用它们。因为数组名称被解释为指向其第一个元素的指针,我们可以将它直接作为参数传递给 std::sort
Chef Gladiator 建议的更好版本:
std::copy(std::begin(BCobj), std::end(BCobj), std::begin(second_BCobj))
欢迎来到 Stack Overflow。您已经提交了完整的代码。下次还请描述您正在构建的开发环境。更好的是,尝试提供与操作系统和编译器无关的标准 C++ 代码。
这是C++论坛,所以我们喜欢在这里使用标准的C++。使用 std::array. That makes it simple. Please study and enjoy the standard C++.
重写您的代码 #include <array>
#include <iostream>
#include <algorithm>
namespace stack_overflow {
using namespace std;
struct BigClass final {
struct Astruct final {
double x[2], v[2];
int rank;
}one_struct;
friend ostream& operator << (ostream& os, Astruct const & one_struct)
{
return os << one_struct.rank << " " << one_struct.x[0] << " "
<< one_struct.x[1] << " " << one_struct.v[0] << " "
<< one_struct.v[1] ;
}
};
constexpr int size = 10;
using bcobj_arr = std::array<BigClass::Astruct, size>;
void populate( bcobj_arr& bcobjects_)
{
int j{ 0 };
for (auto& one_struct : bcobjects_) {
one_struct.x[0] = 1.1;
one_struct.x[1] = 1.2;
one_struct.v[0] = 2.1;
one_struct.v[1] = 2.2;
one_struct.rank = j++;
}
}
void print(const char prompt[BUFSIZ], bcobj_arr const & bcobjects_ )
{
cout << "\n\n" << prompt << "\n\n" ;
for (auto& one_struct : bcobjects_) {
cout << one_struct << "\n";
}
}
bool test (int argc, const char* argv[])
{
bcobj_arr BCobj;
populate(BCobj);
print("BCobj", BCobj);
// std::array instances can be copied
bcobj_arr second_BCobj = BCobj ;
print("second_BCobj", second_BCobj);
return true;
}
}
int main(const int argc, const char * argv[])
{
stack_overflow::test(argc, argv);
return 42;
}
代码完全没有注释。我想你有很多问题,请在下面的评论中提问。我会通过指向您在线的相关文档来尝试回答所有问题。