的std ::对<,> [英] std::pair<,>

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

问题描述



您好,


我想问一下C ++的设计如何包含

std :: pair。首先,我不认为很多程序员会使用它。
会使用它。对于初学者来说,第一和第二个
成员是什么取决于你使用的是什么

for。例如,如果我在两个

维度空间中使用坐标,那么我喜欢使用x和y。所以

我也可以用x和
$ b $来定义我自己的结构,然后创建一个构造函数,这样我可以轻松地实例化对。


我想知道是否有办法创建一对类

使用std :: pair但是typedef是第一个和第二个
$ b使用C ++将$ b转换为x和y。我能想到的唯一方法

是子类std :: pair<,> ;.


最好的建议和推荐

使用std :: pair<,>

的做法和惯例是最受欢迎的。


谢谢,


Neil

解决方案



这是我使用std :: pair<的方法; T1级,T2级>:
#include< iostream>

#include< utility>


模板<类T1,类T2>

类坐标:public std :: pair< T1,T2> {

public:

坐标(T1 x,T2 y):std :: pair< T1,T2>(x,y),x(第一个),y(第二个){}

T1& x;

T2& y;

};


int main(){

坐标< int,int> foo(1,2);

std :: cout<< x = << foo.x<< " \\\
y =" << foo.y<< std :: endl;

}

关于使用std :: pair<,> <的最佳
做法和惯例的建议和推荐非常欢迎。

谢谢,

Neil




>我想问一下C ++的设计如何包含

std :: pair。


我认为主要是std :: map和类似地图的容器。

首先我不认为很多程序员
会用它。


我认为取决于程序员。我经常使用它



对于初学者来说,第一个和第二个
成员取决于你使用的对象是什么。例如,如果我在两个维空间中使用坐标,那么我喜欢使用x和y。所以
我不妨用x和
y成员定义我自己的结构并创建一个构造函数,以便我可以轻松地实例化对。


是的。的std ::对<>是不是专门用来代替代表两个实体的每个结构代替



我想知道是否有办法使用std :: pair创建一个对类
但是使用C ++对x和y输入第一个和第二个。我能想到的唯一方法是继承std :: pair<,>。


如果你希望你的结构与std :: pair<>兼容,那将是一种方式。如果没有,请将其设为

a私人会员,或者如您所说,创建一个新的

结构。

建议和推荐最佳
使用std :: pair<,>
的做法和约定非常受欢迎。




没有,imho。

Jonathan


Neil Zanella写道:


你好,

我想问一下为什么C ++的设计包括
std :: pair。首先,我不认为很多程序员会使用它。



我通常使用std :: make_pair()。



Brian Rodenborn



Hello,

I would like to ask how come the design of C++ includes
std::pair. First of all I don''t think many programmers
would use it. For starters, what the first and second
members are depends on what you are using the pair
for. For instance if I am using coordinates in two
dimensional space then I like to use x and y. So
I might as well define my own struct with x and
y members in it and create a constructor so
that I can easily instantiate pairs.

I wonder if there is a way to create a pair class
using std::pair but typedef its first and second
to x and y using C++. The only way I can think
of is to subclass std::pair<,>.

Suggestions and reccomendation on the best
practices and conventions for using std::pair<,>
are most welcome.

Thanks,

Neil

解决方案


Here is my way of making use of std::pair<class T1, class T2>:

#include <iostream>
#include <utility>

template<class T1, class T2>
class Coordinate: public std::pair<T1, T2> {
public:
Coordinate(T1 x, T2 y): std::pair<T1, T2>(x, y), x(first), y(second) { }
T1 &x;
T2 &y;
};

int main() {
Coordinate<int, int> foo(1, 2);
std::cout << "x = " << foo.x << "\ny = " << foo.y << std::endl;
}

Suggestions and reccomendation on the best
practices and conventions for using std::pair<,>
are most welcome.

Thanks,

Neil




> I would like to ask how come the design of C++ includes

std::pair.
I think primarly for std::map and the map-like containers.
First of all I don''t think many programmers
would use it.
Depends on the programmer, I think. I use it quite
often.
For starters, what the first and second
members are depends on what you are using the pair
for. For instance if I am using coordinates in two
dimensional space then I like to use x and y. So
I might as well define my own struct with x and
y members in it and create a constructor so
that I can easily instantiate pairs.
Yes. std::pair<> was not created specifically to replace
every ud structs representing two entities.
I wonder if there is a way to create a pair class
using std::pair but typedef its first and second
to x and y using C++. The only way I can think
of is to subclass std::pair<,>.
That would be one way if you want your struct to be
compatible with std::pair<>. If not, make it
a private member or, as you said, create a new
struct.
Suggestions and reccomendation on the best
practices and conventions for using std::pair<,>
are most welcome.



There are none, imho.
Jonathan


Neil Zanella wrote:


Hello,

I would like to ask how come the design of C++ includes
std::pair. First of all I don''t think many programmers
would use it.


I generally use std::make_pair().


Brian Rodenborn


这篇关于的std ::对&LT;,&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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