这两个载体有什么不同? (智能指针) [英] What is different between these two vectors? (smart pointers)

查看:83
本文介绍了这两个载体有什么不同? (智能指针)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习智能指针。

  class  Elf {
private
int _health = 0 ;
public
Elf( const int & health){_ health = health;}
int getHealth(){ return _health}
};





这两者有什么不同?

第一种情况:

 vector< unique_ptr< Elf>>精灵; 
for auto i = 0 ; i< 10000 ; ++ i){
elves.push_back(unique_ptr< Elf>( Elf( 90 )));
}



第二种情况:

  auto  elves = make_unique< vector< Elf>>( 10000 ,Elf( 90 )) ; 



=========

第二个问题。

对于第一个案例,我可以通过以下方式访问私有成员_health:

  for  auto  i =  0 ; i<  10000 ; ++ i){
cout<<精灵[i] - > getHealth()<< ;
}



如何在第二种情况下访问_health?

解决方案

引用:

这两者有什么不同?



第一个是精灵(智能)指针的向量第二个是指向实际精灵矢量的(智能)指针。



您可以将第一个标记与指向抽屉柜的标志进行比较,每个抽屉中放置一个精灵(基本上是一个大房子),而第二个是胸部每个抽屉包含精灵的家庭住址(基本上是地址簿)的抽屉。



引用:

如何在第二种情况下访问_health?



解决方案1应该可以工作,但如果您更喜欢使用索引运算符的代码(因此更接近第一个版本),请尝试以下方法:

< pre lang =c ++> cout<< (*精灵)[i] .getHealth();







PS:

oops,只是注意到了约会。我认为这是一个新的请求,基于它在首页上的列表......


 #include< memory> 
#include< vector>


class Elf {
private:
int _health = 0;
public:
Elf(const int& health){_ health = health; }
int getHealth(){return _health; }
};


void Test()
{
using namespace std;
auto elves = make_unique< vector< Elf>>(10000,Elf(90));

int out;
for(auto i = 0; i< 10000; i ++)
{
out = elves-> at(i).getHealth(); / * out = 90 * /
}

}


I am learning about smart pointers.

class Elf {
private:
    int _health = 0;
public:
    Elf(const int &health){_health = health;}
    int getHealth(){return _health}
};



What is the different between these two?
First case:

vector<unique_ptr<Elf>> elves;
for (auto i = 0; i < 10000; ++i) {
    elves.push_back(unique_ptr<Elf>(new Elf(90)));
}


Second case:

auto elves = make_unique<vector<Elf>>(10000, Elf(90));


=========
The second question.
For the first case, I can access private member _health by this:

for(auto i = 0; i < 10000; ++i){
    cout << elves[i]->getHealth() << " ";
}


How to access _health in the second case?

解决方案

Quote:

What is the different between these two?


The first is a vector of (smart) pointers to elves and the second is a (smart) pointer to a vector of actual elves.

You could compare the first to a sign pointing to a chest of drawers with an elf sitting in each drawer (basically a large house), whereas the second is a chest of drawers where each drawer contains the home address of an elf (basically an address book).

Quote:

How to access _health in the second case?


Solution 1 should work, but if you prefer code that uses the index operator (and thus is closer to the first version), try this:

cout << (*elves)[i].getHealth();




P.S.:
oops, just noticed the date. I assumed it was a new request based on it's listing on the front page...


#include <memory>
#include <vector>


class Elf {
private:
	int _health = 0;
public:
	Elf(const int &health){ _health = health; }
	int getHealth(){ return _health; }
};


void Test()
{
	using namespace std;
	auto elves = make_unique<vector<Elf>>(10000, Elf(90));

	int out;
	for (auto i = 0; i < 10000; i++)
	{
		out = elves->at(i).getHealth(); /* out = 90 */
	}	

}


这篇关于这两个载体有什么不同? (智能指针)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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