C++ 将向量转换为数组 - 有没有“标准”的方法来做到这一点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8682533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting a vector to an array - Is there a 'standard' way to do this?
提问by Milliams
I know you can just do: &theVector[0]
, but is this standard? Is this behavior alwaysguaranteed?
我知道你可以这样做:&theVector[0]
,但这是标准吗?这种行为总是有保证的吗?
If not, is there a better, less 'hackish' way to do this?
如果没有,是否有更好的,不那么“hackish”的方法来做到这一点?
回答by Mysticial
Yes, that behavior is guaranteed. Although I can't quote it, the standard guarantees that vector elements are stored consecutively in memory to allow this.
是的,这种行为是有保证的。虽然我不能引用它,但标准保证向量元素连续存储在内存中以允许这样做。
There is one exception though:
不过有一个例外:
It will not work for vector<bool>
because of a template specialization.
vector<bool>
由于模板专业化,它不起作用。
http://en.wikipedia.org/wiki/Sequence_container_%28C%2B%2B%29#Specialization_for_bool
http://en.wikipedia.org/wiki/Sequence_container_%28C%2B%2B%29#Specialization_for_bool
This specialization attempts to save memory by packing bools
together in a bit-field. However, it breaks some semantics and as such, &theVector[0]
on a vector<bool>
will not work.
这种专业化尝试通过bools
将位字段打包在一起来节省内存。但是,它破坏了一些语义,因此,&theVector[0]
在 avector<bool>
上将不起作用。
In any case, vector<bool>
is widely considered to be a mistakeso the alternative is to use std::deque<bool>
instead.
在任何情况下,都vector<bool>
被广泛认为是错误的,所以替代方案是使用std::deque<bool>
。
回答by Milliams
C++11 provides the data()
method on std::vector
which returns a T*
. This allows you to do:
C++11 提供了返回 a的data()
方法。这允许您执行以下操作:std::vector
T*
#include <iostream>
#include <vector>
int main()
{
std::vector<int> vector = {1,2,3,4,5};
int* array = vector.data();
std::cout << array[4] << std::endl; //Prints '5'
}
However, doing this (or any of the methods mentioned above) can be dangerous as the pointer could become invalid if the vector is resized. This can be shown with:
但是,这样做(或上述任何方法)可能很危险,因为如果调整向量的大小,指针可能会变得无效。这可以显示为:
#include <iostream>
#include <vector>
int main()
{
std::vector<int> vector = {1,2,3,4,5};
int* array = vector.data();
vector.resize(100); //This will reserve more memory and move the internal array
//This _may_ end up taking the place of the old array
std::vector<int> other = {6,7,8,9,10};
std::cout << array[4] << std::endl; //_May_ now print '10'
}
This could could crash or do just about anything so be careful using this.
这可能会崩溃或做任何事情,所以使用它时要小心。
回答by rashedcs
We can do this using data() method. C++11 provides this method. It returns a pointer to the first element in the vector. vector Even if it is empty, we can call this function itself without problems
我们可以使用 data() 方法来做到这一点。C++11 提供了这种方法。它返回一个指向向量中第一个元素的指针。vector 即使它是空的,我们也可以毫无问题地调用这个函数本身
vector<int>v;
int *arr = v.data();
回答by Software_Designer
A less 'hackish' way? Well you could simply copy :
一种不那么“hackish”的方式?那么你可以简单地复制:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vect0r;
int array[100];
//Fill vector
for(int i = 0; i < 10 ; i++) vect0r.push_back( i ) ;
//Copy vector to array[ ]
for( i = 0; i < vect0r.size(); i++) array[i] = vect0r[i];
//Dispay array[ ]
for( i = 0; i < vect0r.size(); i++) cout<< array[i] <<" \n";
cout<<" \n";
return 0;
}
More here : How to convert vector to array in C++
更多信息:如何在 C++ 中将向量转换为数组