2011-08-04 246 views
37

我有一个指向矢量的指针。现在,我怎样才能通过指针读取矢量的内容?我知道这是一个基本问题,但我无法找到同样的答案。(C++)指向矢量的指针

+7

到目前为止您尝试过什么?你能提供一些代码,所以我们可以具体谈一谈吗? – Nate

+3

您确定要使用指向矢量的指针吗?使用引用可能更有意义。看到我的帖子下面。 –

回答

51

解决方法有很多,这里有几个我想出来的:

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!) 
} 
+0

引用部分是坏建议,因为'new'可以返回0. –

+0

@Don Reba - 不是真的;在大多数编译器上引发异常:http://stackoverflow.com/questions/550451/will-new-return-null-in-any-case – Schnommus

+0

另外,如果'new' **确实返回0,**所有**指针上的操作都不好,不仅仅是参考初始化。 – Schnommus

2

有很多解决方案。例如,您可以使用at()方法。

*我以为你找一个相当于[]的操作符。

+0

但如何知道向量中元素的数量? – Pavan

+1

@ user815961'v-> size()'返回向量 – Praetorian

12

访问它像任何其他指针值:

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; 
} 
+1

中向上的元素数目,就像“任何其他指针”一样。我不明白这个问题。你有一个指针,所以你使用'thing-> member'而不是'thing.member',这是唯一的区别(在99%的情况下,等等) –

11
vector<int> v; 
v.push_back(906); 
vector<int> * p = &v; 
cout << (*p)[0] << endl; 
5

您可以访问迭代方法直接:

std::vector<int> *intVec; 
std::vector<int>::iterator it; 

for(it = intVec->begin(); it != intVec->end(); ++it) 
{ 
} 

如果你想在数组访问运算符,你必须去引用指针。例如:

std::vector<int> *intVec; 

int val = (*intVec)[0]; 
-3

最简单的方式来使用它作为阵列是使用vector::data()成员。

+1

您使用向量的原因是为了抽象出背后的底层实现(连续数组)。这违背了面向对象的原则,更不用说效率极低了,因为当你可以使用vec-> at(index)的时候,你正在分配一个全新的n数组。更何况,这与原始海报的问题完全没有关系,因为你没有在任何地方使用指向矢量的指针。 –

+0

我只是给出了一个例子,如果我们想知道底层类型,我们可以使用vector :: value_type。但它是静态解释的。我想传达的观点是data()是获取底层指针的成员。这就是他的问题所在。 – sarat

+0

不,问题是'vector'是否可以通过指针读取。我没有看到OP中的任何内容,表明比这更深的东西。 –

11

你有一个指向矢量的指针,因为这就是你编码的方式吗?您可能需要重新考虑这一点并使用(可能是const)引用。例如:

#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); 
} 

虽然这是一个有效的程序,但一般的C++风格是通过引用而不是指针传递向量。这将会同样有效,但是你不需要处理可能的空指针和内存分配/清理等。如果你不打算修改向量,那么使用一个const引用,并且如果是非const引用if你确实需要修改。

下面是引用版本以上程序:

#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); 
} 

正如你所看到的,包含内的信息将被传递给函数foo,但它不会复制一个完全新的价值,因为它是通过引用传递的。因此,它与传递指针一样高效,并且您可以将其用作正常值,而不必知道如何将其用作指针或不必将其解引用。

+0

这可能是一个愚蠢的评论。但是由于您只使用at方法,使用语法(* a)[1]有什么优势?就我个人而言,我喜欢这一点,因为它更能反映我的说法,这使我更容易阅读代码(我的意思是至少在一种情况下显着提高性能) – patrik