c++ - 如何从指向向量的指针访问向量的内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6946217/
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
How to access the contents of a vector from a pointer to the vector in C++?
提问by Pavan Dittakavi
I have a pointer to a vector. Now, how can I read the contents of the vector through pointer?
我有一个指向向量的指针。现在,如何通过指针读取向量的内容?
回答by Seb Holzapfel
There are many solutions, here's a few I've come up with:
有很多解决方案,这里有一些我想出的:
int main(int nArgs, char ** vArgs)
{
vector<int> *v = new vector<int>(10);
v->at(2); //Retrieve using pointer to member
v->operator[](2); //Retrieve using pointer to operator member
v->size(); //Retrieve size
vector<int> &vr = *v; //Create a reference
vr[2]; //Normal access through reference
delete &vr; //Delete the reference. You could do the same with
//a pointer (but not both!)
}
回答by Chad
Access it like any other pointer value:
像访问任何其他指针值一样访问它:
std::vector<int>* v = new std::vector<int>();
v->push_back(0);
v->push_back(12);
v->push_back(1);
int twelve = v->at(1);
int one = (*v)[2];
// iterate it
for(std::vector<int>::const_iterator cit = v->begin(), e = v->end;
cit != e; ++cit)
{
int value = *cit;
}
// or, more perversely
for(int x = 0; x < v->size(); ++x)
{
int value = (*v)[x];
}
// Or -- with C++ 11 support
for(auto i : *v)
{
int value = i;
}
回答by Andrew Rasmussen
Do you have a pointer to a vector because that's how you've coded it? You may want to reconsider this and use a (possibly const) reference. For example:
你有一个指向向量的指针,因为你就是这样编码的吗?您可能需要重新考虑这一点并使用(可能是常量)引用。例如:
#include <iostream>
#include <vector>
using namespace std;
void foo(vector<int>* a)
{
cout << a->at(0) << a->at(1) << a->at(2) << endl;
// expected result is "123"
}
int main()
{
vector<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
foo(&a);
}
While this is a valid program, the general C++ style is to pass a vector by reference rather than by pointer. This will be just as efficient, but then you don't have to deal with possibly null pointers and memory allocation/cleanup, etc. Use a const reference if you aren't going to modify the vector, and a non-const reference if you do need to make modifications.
虽然这是一个有效的程序,但一般的 C++ 风格是通过引用而不是通过指针传递向量。这将同样有效,但是您不必处理可能的空指针和内存分配/清理等。如果您不打算修改向量,请使用 const 引用,如果不修改则使用非常量引用您确实需要进行修改。
Here's the references version of the above program:
这是上述程序的参考版本:
#include <iostream>
#include <vector>
using namespace std;
void foo(const vector<int>& a)
{
cout << a[0] << a[1] << a[2] << endl;
// expected result is "123"
}
int main()
{
vector<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
foo(a);
}
As you can see, all of the information contained within a will be passed to the function foo, but it will not copy an entirely new value, since it is being passed by reference. It is therefore just as efficient as passing by pointer, and you can use it as a normal value rather than having to figure out how to use it as a pointer or having to dereference it.
如您所见,a 中包含的所有信息都将传递给函数 foo,但它不会复制一个全新的值,因为它是通过引用传递的。因此,它与传递指针一样有效,您可以将其用作普通值,而不必弄清楚如何将其用作指针或取消引用它。
回答by Mark Ransom
vector<int> v;
v.push_back(906);
vector<int> * p = &v;
cout << (*p)[0] << endl;
回答by Jon Benedicto
You can access the iterator methods directly:
您可以直接访问迭代器方法:
std::vector<int> *intVec;
std::vector<int>::iterator it;
for( it = intVec->begin(); it != intVec->end(); ++it )
{
}
If you want the array-access operator, you'd have to de-reference the pointer. For example:
如果您想要数组访问运算符,则必须取消引用指针。例如:
std::vector<int> *intVec;
int val = (*intVec)[0];
回答by eugene_che
There are a lot of solutions. For example you can use at()
method.
有很多解决方案。例如,您可以使用at()
方法。
*I assumed that you a looking for equivalent to []
operator.
*我假设您正在寻找相当于[]
运营商的人。
回答by sarat
The easiest way use it as array is use vector::data()
member.
将其用作数组的最简单方法是使用vector::data()
成员。