这是使用矢量的“首选”方式吗? [英] Is this the 'prefered' way to use a vector

查看:75
本文介绍了这是使用矢量的“首选”方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我将使用相当多的向量,我想确保我正在使用它

正确。


//

struct MYSTRUCT

{

....

}


std :: vector< MYSTRUCT> m_vMyStruct;

//

// ...

//

MYSTRUCT * GetItem(unsigned nItem) const

{

return(nItem> =

m_vMyStruct.size()?NULL:((MYSTRUCT *)&(m_vMyStruct- > begin()+ nItem)));

}

//

// ...

//

//我还可以拥有

const MYSTRUCT * GetItem(unsigned nItem)const

{

return(nItem> = m_vMyStruct.size()?NULL:((const

MYSTRUCT *)&(m_vMyStruct-> begin()+ nItem)));

}

//


我可以声明const和非const函数吗?

有没有更好的获取/使用矢量值的方法?


非常感谢


Simon

Hi,

I am going to use quite a few vectors and I want to make sure I am using it
properly.

//
struct MYSTRUCT
{
....
}

std::vector< MYSTRUCT > m_vMyStruct;
//
// ...
//
MYSTRUCT * GetItem( unsigned nItem )const
{
return ( nItem >=
m_vMyStruct.size()?NULL:((MYSTRUCT*)&(m_vMyStruct->begin()+nItem)));
}
//
// ...
//
// and can I also have
const MYSTRUCT * GetItem( unsigned nItem )const
{
return ( nItem >= m_vMyStruct.size()?NULL:((const
MYSTRUCT*)&(m_vMyStruct->begin()+nItem)));
}
//

can I declare both const and non-const functions?
Is there a better way to get/use a vector value?

many thanks

Simon

推荐答案

Simon写道:


我将使用相当多的向量,我想确保我使用它
正确。

//
struct MYSTRUCT
{
...
}


不要在结构上使用全部大写字母:


struct myStruct;

std :: vector< MYSTRUCT> m_vMyStruct;


std :: vector< myStruct> vec;


MYSTRUCT * GetItem(unsigned nItem)const


你只能在函数声明结束时使用const这是

是一个成员函数 - 它似乎不是。

{
return(nItem> =
m_vMyStruct.size()?NULL: ((MYSTRUCT *)&(m_vMyStruct-> begin()+ nItem)));
}


恭喜。你刚刚重新创建了这个轮子。摆脱你的功能

并且只需使用:

vec [nItem];

//我也可以拥有
const MYSTRUCT * GetItem(unsigned nItem)const
{
return(nItem> = m_vMyStruct.size()?NULL:((const
MYSTRUCT *)&(m_vMyStruct-> begin( )+ nItem)));
}


你再次重新发明轮子。摆脱你的功能只需

使用:

vec [nItem];

它已经为const和非const重载了版本。

我可以声明const和非const函数吗?


不,除非这些是成员函数 - 它们不是。

有更好的方法来获取/使用向量值吗?
Hi,

I am going to use quite a few vectors and I want to make sure I am using it
properly.

//
struct MYSTRUCT
{
...
}
Don''t use all caps for a struct:

struct myStruct;

std::vector< MYSTRUCT > m_vMyStruct;
std::vector<myStruct> vec;

MYSTRUCT * GetItem( unsigned nItem )const
you can only use const at the end of a function declaration is this
were a member function - which it does not appear to be.
{
return ( nItem >=
m_vMyStruct.size()?NULL:((MYSTRUCT*)&(m_vMyStruct->begin()+nItem)));
}
Congrats. You''ve just recreated the wheel. Get rid of your function
and just use:
vec[nItem];
// and can I also have
const MYSTRUCT * GetItem( unsigned nItem )const
{
return ( nItem >= m_vMyStruct.size()?NULL:((const
MYSTRUCT*)&(m_vMyStruct->begin()+nItem)));
}
You''ve reinvented the wheel - again. Get rid of your function and just
use:
vec[nItem];
It''s already overloaded for const and non-const versions.
can I declare both const and non-const functions?
No, unless these are members functions - which they are not.
Is there a better way to get/use a vector value?




正如我给你看的那样。你需要给自己一本关于使用

标准库的书。试试Josuttis''The C ++ Standard Library。


祝你好运,


汤姆。



As I showed you. You need to get yourself a decent book on using the
standard library. Try Josuttis'' The C++ Standard Library.

Best regards,

Tom.




Simon写道:

Simon wrote:


我将使用相当多的向量,我想制作确定我正在使用它


//结构MYSTRUCT
{
...
}


在结尾处缺少分号。

std :: vector< MYSTRUCT> m_vMyStruct;
//
// ...
//
MYSTRUCT * GetItem(unsigned nItem)const
{


const方法限定符只能用于成员函数。你有没有想要GetItem作为某类的成员函数?

return(nItem> =
m_vMyStruct.size()?NULL:((MYSTRUCT *) &(m_vMyStruct-> begin()+ nItem)));
}

// // ...
//
//并且可以我也有
const MYSTRUCT * GetItem(unsigned nItem)const
返回(nItem> = m_vMyStruct.size()?NULL:((const
MYSTRUCT *)& ;(m_vMyStruct-> begin()+ nItem)));
}
//

我可以声明const和非const函数吗?
Hi,

I am going to use quite a few vectors and I want to make sure I am using it
properly.

//
struct MYSTRUCT
{
...
}
missing semicolon at the end of struct.

std::vector< MYSTRUCT > m_vMyStruct;
//
// ...
//
MYSTRUCT * GetItem( unsigned nItem )const
{
"const" method qualifier can only be used for member functions. Did you
want GetItem as member function of some class?
return ( nItem >=
m_vMyStruct.size()?NULL:((MYSTRUCT*)&(m_vMyStruct->begin()+nItem)));
}
// // ...
//
// and can I also have
const MYSTRUCT * GetItem( unsigned nItem )const
{
return ( nItem >= m_vMyStruct.size()?NULL:((const
MYSTRUCT*)&(m_vMyStruct->begin()+nItem)));
}
//

can I declare both const and non-const functions?




你的意思是用_return types_表示函数为const和非const。

编号函数不能仅根据返回类型重载。 />
但是就const方法限定符

而言,它们可以重载。


顺便说一句,返回是没有意义的常量指针按值计算

- 指针按值返回,因此无论如何你都会在调用者中制作一份

的副本。注意,_pointer_是
常量,而不是指针所指向的数据。


最后,如果m_vMyStruct是一个公共变量,那么它就不会感觉

有它的get-set函数。但似乎你想把它们包装在一个类中。


希望有所帮助。



You mean to say functions with _return types_ as const and non-const.
No. functions cannot be overloaded on the basis of return type alone.
But they can be overloaded as far as const method qualifier is
concerned.

As an aside, it makes no sense to return a "constant pointer" by value
- the pointer is returned by value and hence anyways you will make a
copy of it in the caller. Note that it is the _pointer_ which is
constant, not the data pointed by that pointer.

Finally, if m_vMyStruct is a public variable then it makes no sense to
have get-set functions for it. But it seems that you wanted to wrap
them in a class.

hope that helps.




Simon skrev:

Simon skrev:


我将使用相当多的向量和我我想确保我正确地使用它


//结构MYSTRUCT
{
...
}


你应该只保留macroes的全大写名字。
std :: vector< MYSTRUCT> m_vMyStruct;
//
// ...
// MYSTRUCT * GetItem(unsigned nItem)const
{
return(nItem> =
m_vMyStruct.size()?NULL:((MYSTRUCT *)&(m_vMyStruct-> begin()+ nItem)));


它应该是m_vMyStruct.begin()而不是m_vMyStruct-> begin()。

m_vMyStruct不是指针!

你的代码错了。 m_vMyStruct.begin()+ nItem将是一个迭代器,所以

你会返回一个指向迭代器的指针。


还要注意你的指针不会是一旦你开始有效

删除或添加元素到m_vMyStruct。


为什么不简单地& m_vMyStruct [nItem]? }
//
// ...
//
//我还可以使用
const MYSTRUCT * GetItem(unsigned nItem)const
{
return(nItem> = m_vMyStruct.size()?NULL :((const
MYSTRUCT *)&(m_vMyStruct-> begin()+ nItem)));
}
//

我可以声明const和非const函数吗?
有没有更好的方法来获取/使用向量值?
要获得矢量值,通常使用val = vec [i];设置

你只需写vec [i] = val。

但是你给我们看的不是设置/获取的界面

矢量值。你已经向我们展示了一个类的getter / setter,

在内部将值存储在向量中。
非常感谢

Simon
Hi,

I am going to use quite a few vectors and I want to make sure I am using it
properly.

//
struct MYSTRUCT
{
...
}
You should reserve all-uppercase names for macroes only.
std::vector< MYSTRUCT > m_vMyStruct;
//
// ...
//
MYSTRUCT * GetItem( unsigned nItem )const
{
return ( nItem >=
m_vMyStruct.size()?NULL:((MYSTRUCT*)&(m_vMyStruct->begin()+nItem)));
it should be m_vMyStruct.begin() instead of m_vMyStruct->begin().
m_vMyStruct is not a pointer!
Your code is wrong. m_vMyStruct.begin()+nItem would be an iterator, so
you would return a pointer to an iterator.

Also beware that your pointers will not be valid as soon as you begin
removing or adding elements to m_vMyStruct.

Why not simply &m_vMyStruct[nItem]? }
//
// ...
//
// and can I also have
const MYSTRUCT * GetItem( unsigned nItem )const
{
return ( nItem >= m_vMyStruct.size()?NULL:((const
MYSTRUCT*)&(m_vMyStruct->begin()+nItem)));
}
//

can I declare both const and non-const functions?
Is there a better way to get/use a vector value? For getting a vector value, you normally use val = vec[i]; for setting
it you simply write vec[i] = val.
But what you have shown us is not an interface for setting/getting
vector values. You have shown us a getter/setter for a class that
internally stores values in a vector.
many thanks

Simon




/ Peter



/Peter


这篇关于这是使用矢量的“首选”方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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