is it safe to memcopy myvect.size()*sizeof(foo) bytes from the memoryadress of the first element of a
从a的第一个元素的内存中拷贝myvect.size()*sizeof(foo)字节是否安全
std::vector<std::pair<T1, T2> > myvect
into an array of
为一个数组的
struct foo{
T1 first;
T2 second;
}
if the array is allocated with the same number of elements as the vector's size?
如果数组的元素个数与向量的大小相同?
thanks
谢谢
3 个解决方案
#1
8
No, a class containing T1
and T2
is not guaranteed the same layout or alignment as std::pair<T1, T2>
, at least in C++98 (since std::pair
is not a POD type). The story may be different in C++0x.
不,包含T1和T2的类不能保证与std::对
#2
4
The answer to the question you didn't ask is probably std::transform
:
你没有问的问题的答案可能是std:转换:
struct pairToFoo {
// optionally this can be a function template.
// template<typename T1, typename T2>
foo operator()(const std::pair<T1,T2> &p) const {
foo f = {p.first, p.second};
return f;
}
};
std::transform(myvect.begin(), myvect.end(), myarray, pairToFoo());
Or std::copy
, but give foo an operator=
taking a pair as parameter. This assumes you can re-write foo, though:
或者std::copy,但是给foo一个运算符=取一对作为参数。假设您可以重新编写foo,但是:
struct foo {
T1 first;
T2 second;
foo &operator=(const std::pair<T1,T2> &p) {
first = p.first;
second = p.second;
return *this;
}
};
std::copy(myvect.begin(), myvect.end(), myarray);
#3
0
In general, no. On some platforms/compilers/STL implementations it might be, but don't do it anyway. You'd be relying on the implementation details of both pair<> and vector<>.
一般来说,没有。在某些平台/编译器/STL实现上可能是这样,但无论如何不要这样做。您将依赖于<>和向量<>的实现细节。
I myself have committed the sin of relying on vector<> being a contiguous array. For that, I deeply repent. But the pair<>... Just say no.
我自己犯了一个罪,就是依赖向量<>是一个连续数组。为此,我深感忏悔。但两人< >…只是说不。
#1
8
No, a class containing T1
and T2
is not guaranteed the same layout or alignment as std::pair<T1, T2>
, at least in C++98 (since std::pair
is not a POD type). The story may be different in C++0x.
不,包含T1和T2的类不能保证与std::对
#2
4
The answer to the question you didn't ask is probably std::transform
:
你没有问的问题的答案可能是std:转换:
struct pairToFoo {
// optionally this can be a function template.
// template<typename T1, typename T2>
foo operator()(const std::pair<T1,T2> &p) const {
foo f = {p.first, p.second};
return f;
}
};
std::transform(myvect.begin(), myvect.end(), myarray, pairToFoo());
Or std::copy
, but give foo an operator=
taking a pair as parameter. This assumes you can re-write foo, though:
或者std::copy,但是给foo一个运算符=取一对作为参数。假设您可以重新编写foo,但是:
struct foo {
T1 first;
T2 second;
foo &operator=(const std::pair<T1,T2> &p) {
first = p.first;
second = p.second;
return *this;
}
};
std::copy(myvect.begin(), myvect.end(), myarray);
#3
0
In general, no. On some platforms/compilers/STL implementations it might be, but don't do it anyway. You'd be relying on the implementation details of both pair<> and vector<>.
一般来说,没有。在某些平台/编译器/STL实现上可能是这样,但无论如何不要这样做。您将依赖于<>和向量<>的实现细节。
I myself have committed the sin of relying on vector<> being a contiguous array. For that, I deeply repent. But the pair<>... Just say no.
我自己犯了一个罪,就是依赖向量<>是一个连续数组。为此,我深感忏悔。但两人< >…只是说不。