index / iterator的矢量访问 [英] vector acces by index/iterator

查看:48
本文介绍了index / iterator的矢量访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


在最近的一个帖子中向量与数组的速度我通过索引解决了矢量元素,从而了解了缓慢访问的问题。

遗憾的是我看不到解决办法。


我的情况:


A级

{

私人:

vector< int> ; vec;

public:

...

}


\\\\ first我将私有元素声明为数组,所以我总是按照索引访问部分:vec [i]


\\ now以便通过索引I访问向量时避免减速

想要使用迭代器,当我想要在某个函数中访问一个对象时,会出现以下问题,例如:


void函数(const A& a)

{

vector< int> :: iterator const p(a.vec。开始());

...

}

编译时我得到错误信息:

从''const int * const''无效转换为''int *''


我可以通过不将参数作为const来避免这个问题,但我是

害怕这样做,因为我必须在整个

程序中进行更改,并且在引用参数时可能会破坏一些数据。

reference。并且仅通过具有无效功能(A a)和我失去了很多时间

因为每次都会调用一个拷贝构造函数,而不是通过缓慢的向量索引访问来减少时间。


我希望有一种方法可以像数组中一样快地访问向量中的元素而不会让我的程序颠倒过来。所以,提前感谢

为您提供帮助。


哈根

Hello,

in a recent thread "speed of vector vs array" I read about the problem
of the slow acces by addressing vector elements by indexing,
unfortunately I see no workaround in my case.

My case:

class A
{
private:
vector<int> vec;
public:
...
}

\\at first I had the private element declared as array, so I always
accessed the parts by index: vec[i]

\\now in order to avoid the slowdown when accessing a vector by index I
want to use iterators, the following problem occurs when I want to
access an A object in a function, example:

void function(const A& a)
{
vector<int>::iterator const p( a.vec.begin() );
...
}
When compiling this I get the error message:
invalid conversion from ''const int* const'' to ''int*''

I could avoid this problem by not giving the parameter as const, but I''m
afraid to do so because I would have to change throughout the whole
program and maybe destroy some data when referring to parameters by
reference. And by just having "void function(A a)" I lose a lot of time
because everytime a copy-constructor would be called, instead of losing
the time by slow vector-index-accessment.

I hope there is a way I can access the elements in a vector as fast as
in an array without turning my program upside down. So thanks in advance
for your help.

Hagen

推荐答案



" Hagen" <公顷*** @ t-online.de>在消息中写道

新闻:cd ************* @ news.t-online.com ...

"Hagen" <ha***@t-online.de> wrote in message
news:cd*************@news.t-online.com...
你好,
我的情况:

A级
{
私人:
vector< int> vec;
公开:
...
}

\\\\首先我将私有元素声明为数组,所以我总是
通过索引访问部件:vec [i]

\\ n \\ n现在为了避免在通过索引访问向量时减速
想要使用迭代器,出现以下问题时我想
访问函数中的A对象,例如:

void函数(const A& a)
{
vector< int> :: iterator const p(a.vec.begin());


这样做


vector< int> :: const_iterator p(a.vec.begin());

...
}
Hello,

in a recent thread "speed of vector vs array" I read about the problem
of the slow acces by addressing vector elements by indexing,
unfortunately I see no workaround in my case.

My case:

class A
{
private:
vector<int> vec;
public:
...
}

\\at first I had the private element declared as array, so I always
accessed the parts by index: vec[i]

\\now in order to avoid the slowdown when accessing a vector by index I
want to use iterators, the following problem occurs when I want to
access an A object in a function, example:

void function(const A& a)
{
vector<int>::iterator const p( a.vec.begin() );
Do it like this

vector<int>::const_iterator p (a.vec.begin() );
...
}




john



john


" Hagen" <公顷*** @ t-online.de>在消息中写道

新闻:cd ************* @ news.t-online.com ...
"Hagen" <ha***@t-online.de> wrote in message
news:cd*************@news.t-online.com...
你好,
我的情况:

A级
{
私人:
vector< int> vec;
公开:
...
}

\\\\首先我将私有元素声明为数组,所以我总是
通过索引访问部件:vec [i]

\\ n \\ n现在为了避免在通过索引访问向量时减速
想要使用迭代器,出现以下问题时我想
访问函数中的A对象,例如:

void函数(const A& a)
{
vector< int> :: iterator const p(a.vec.begin());
...
}

编译时我得到错误信息:
来自''const的无效转换int * const''到''int *''

我可以通过不将参数作为const来避免这个问题,但是我很害怕这样做因为我会有改变整个
程序,并可能在参考参考时破坏一些数据。并且仅通过具有无效功能(A a)和我失去了很多时间
因为每次都会调用一个拷贝构造函数,而不是通过缓慢的向量索引访问来减少时间。

我希望有一个我可以像在数组中一样快地访问向量中的元素而不会使我的程序颠倒过来。所以,提前感谢您的帮助。
Hello,

in a recent thread "speed of vector vs array" I read about the problem
of the slow acces by addressing vector elements by indexing,
unfortunately I see no workaround in my case.

My case:

class A
{
private:
vector<int> vec;
public:
...
}

\\at first I had the private element declared as array, so I always
accessed the parts by index: vec[i]

\\now in order to avoid the slowdown when accessing a vector by index I
want to use iterators, the following problem occurs when I want to
access an A object in a function, example:

void function(const A& a)
{
vector<int>::iterator const p( a.vec.begin() );
...
}
When compiling this I get the error message:
invalid conversion from ''const int* const'' to ''int*''

I could avoid this problem by not giving the parameter as const, but I''m
afraid to do so because I would have to change throughout the whole
program and maybe destroy some data when referring to parameters by
reference. And by just having "void function(A a)" I lose a lot of time
because everytime a copy-constructor would be called, instead of losing
the time by slow vector-index-accessment.

I hope there is a way I can access the elements in a vector as fast as
in an array without turning my program upside down. So thanks in advance
for your help.




您好


为什么不使用指针访问向量:

(因为向量元素保证在内存中是连续的)


void函数(const A& a)

{

int * i =& a.vec [0];


//使用指针访问:

cout<< ; 第一元素的价值: << * i<< endl;

i ++;

cout<< 第二元素的价值: << * i<< endl;


//或者就像数组一样:

cout<< 第一元素的价值: << i [0]<< endl;

cout<< 第二元素的价值: <<我[1]<<结束;

}


-

Elias



Hello

Why not access the vector using pointer as:
(since vector elements are guaranteed to be sequential in memory)

void function(const A& a)
{
int *i = &a.vec[0];

// access using pointers:
cout << "first element''s value:" << *i << endl;
i++;
cout << "second element''s value:" << *i << endl;

// or as if arrays:
cout << "first element''s value:" << i[0] << endl;
cout << "second element''s value:" << i[1] << endl;
}

--
Elias




" lallous" < LA ***** @ lgwm.org>在消息中写道

news:2m ************ @ uni-berlin.de ...

"lallous" <la*****@lgwm.org> wrote in message
news:2m************@uni-berlin.de...
" Hagen" <公顷*** @ t-online.de>在消息中写道
新闻:cd ************* @ news.t-online.com ...
"Hagen" <ha***@t-online.de> wrote in message
news:cd*************@news.t-online.com...
你好,

我的情况:

A级
{
私人:
vector< int> vec;
公开:
...
}

\\\\首先我将私有元素声明为数组,所以我总是
通过索引访问部件:vec [i]

\\ n \\ n现在为了避免在通过索引访问向量时减速
想要使用迭代器,出现以下问题时我想
访问函数中的A对象,例如:

void函数(const A& a)
{
vector< int> :: iterator const p(a.vec.begin());
...
}

编译时我得到错误信息:
来自''const的无效转换int * const''到''int *''

我可以通过不将参数作为const来避免这个问题,但是我很害怕这样做因为我会有改变整个
程序,并可能在参考参考时破坏一些数据。并且仅通过具有无效功能(A a)和我失去了很多时间
因为每次都会调用一个拷贝构造函数,而不是通过缓慢的向量索引访问来减少时间。

我希望有一个我可以像在数组中一样快地访问向量中的元素而不会使我的程序颠倒过来。所以,提前感谢您的帮助。
Hello,

in a recent thread "speed of vector vs array" I read about the problem
of the slow acces by addressing vector elements by indexing,
unfortunately I see no workaround in my case.

My case:

class A
{
private:
vector<int> vec;
public:
...
}

\\at first I had the private element declared as array, so I always
accessed the parts by index: vec[i]

\\now in order to avoid the slowdown when accessing a vector by index I
want to use iterators, the following problem occurs when I want to
access an A object in a function, example:

void function(const A& a)
{
vector<int>::iterator const p( a.vec.begin() );
...
}
When compiling this I get the error message:
invalid conversion from ''const int* const'' to ''int*''

I could avoid this problem by not giving the parameter as const, but I''m
afraid to do so because I would have to change throughout the whole
program and maybe destroy some data when referring to parameters by
reference. And by just having "void function(A a)" I lose a lot of time
because everytime a copy-constructor would be called, instead of losing
the time by slow vector-index-accessment.

I hope there is a way I can access the elements in a vector as fast as
in an array without turning my program upside down. So thanks in advance
for your help.



您好

为什么不使用指针访问矢量:
(因为矢量元素保证在内存中是顺序的)

void函数(const A& a)
{
int * i =& a.vec [0];



Hello

Why not access the vector using pointer as:
(since vector elements are guaranteed to be sequential in memory)

void function(const A& a)
{
int *i = &a.vec[0];




const int * i =& a.vec [0];


实现很有可能向量迭代器a

指针的typedef。在这种情况下使用迭代器并使用指针

将是相同的。


john



const int* i = &a.vec[0];

It''s quite possible for an implementation to make a vector iterator a
typedef for a pointer. In that case using an iterator and using a pointer
would be the same.

john


这篇关于index / iterator的矢量访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆