如何使用 C 样式数组分配 std::vector?
从 C 样式数组初始化 std::vector
最便宜的方法是什么?
示例:在下面的类中,我有一个 vector
,但由于外部限制,数据将以 C 样式数组的形式传入:
class Foo {
std::vector<double> w_;
public:
void set_data(double* w, int len){
// how to cheaply initialize the std::vector?
}
显然,我可以调用 w_.resize()
然后循环遍历元素,或调用 std::copy()
。还有更好的方法吗?
What is the cheapest way to initialize a std::vector
from a C-style array?
Example: In the following class, I have a vector
, but due to outside restrictions, the data will be passed in as C-style array:
class Foo {
std::vector<double> w_;
public:
void set_data(double* w, int len){
// how to cheaply initialize the std::vector?
}
Obviously, I can call w_.resize()
and then loop over the elements, or call std::copy()
. Are there any better methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不要忘记您可以将指针视为迭代器:
Don't forget that you can treat pointers as iterators:
您使用了“初始化”一词,因此不清楚这是一次性分配还是可以多次发生。
如果您只需要一次初始化,您可以将其放在构造函数中并使用两个迭代器向量构造函数:
否则按照前面的建议使用分配:
You use the word initialize so it's unclear if this is one-time assignment or can happen multiple times.
If you just need a one time initialization, you can put it in the constructor and use the two iterator vector constructor:
Otherwise use assign as previously suggested:
快速通用答案:
或特定问题:
基于上述:不要忘记,您可以将指针视为迭代器
The quick generic answer:
or question specific:
based on above: Don't forget that you can treat pointers as iterators
好吧,Pavel 已经很接近了,但是还有一个更简单、更优雅的解决方案来从 ac 样式数组初始化顺序容器。
在你的情况下:
Well, Pavel was close, but there's even a more simple and elegant solution to initialize a sequential container from a c style array.
In your case:
您可以自动“了解”数组的大小:
希望您可以将接口更改为上面的 set_data 。它仍然接受 C 样式数组作为其第一个参数。它只是碰巧引用它。
工作原理
[更新:请参阅这里有关学习大小的更全面的讨论]
这是一个更通用的解决方案:
这是有效的,因为数组被作为引用传递给-一个数组。在 C/C++ 中,您不能将数组作为函数传递,相反,它会衰减为指针,并且您会丢失大小。但在 C++ 中,您可以传递对数组的引用。
通过引用传递数组需要类型完全匹配。数组的大小是其类型的一部分。这意味着我们可以使用模板参数 N 来学习大小。
使用返回向量的函数可能会更简单。通过适当的编译器优化,这应该比看起来更快。
You can 'learn' the size of the array automatically:
Hopefully, you can change the interface to set_data as above. It still accepts a C-style array as its first argument. It just happens to take it by reference.
How it works
[ Update: See here for a more comprehensive discussion on learning the size ]
Here is a more general solution:
This works because the array is being passed as a reference-to-an-array. In C/C++, you cannot pass an array as a function, instead it will decay to a pointer and you lose the size. But in C++, you can pass a reference to the array.
Passing an array by reference requires the types to match up exactly. The size of an array is part of its type. This means we can use the template parameter N to learn the size for us.
It might be even simpler to have this function which returns a vector. With appropriate compiler optimizations in effect, this should be faster than it looks.
std::vector::assign
是可行的方法,因为它代码很少。但实际上它是如何运作的呢?不是先调整大小然后复制吗?在我使用的 STL 的 MS 实现中,它正是这样做的。恐怕没有没有更快的方法来实现(重新)初始化您的
std::vector
。std::vector<double>::assign
is the way to go, because it's little code. But how does it work, actually? Doesnt't it resize and then copy? In MS implementation of STL I am using it does exactly so.I'm afraid there's no faster way to implement (re)initializing your
std::vector
.