我想将值vector
插入set<int>
,typedef
使用typedef std::set<int> blockSet_t;
std::vector<blockSet_t>* myvector
定义,如下所示:
myvector[0]
我想要的值为set
int
myvector[1]
个,set
int
vector
个set
等等。
目前,我将此main()
{
std::vector<blockSet_t> myvector;
filereader(myvector);
}
传递给正在解析具有set
整数的文件的函数。
喜欢:
blockSet_t myset;
我从文件中读取set
并将其存储在其他vector
我使用以下循环将此filereader(&myvector)
{
for(int i=0;i<size;i++)
{
myvector.push_back(myset); // It does not give error but I don't know myset is stored in which location
//what I want is to have someting like this
myvector[i].push_back(myset); //so I can store different sets at different locations
}
}
存储到vector
中的特定位置:
set
我也想不通,如何在set
中显示值。
由于它是vector
s的向量,我想显示每个void onCollisionEnter(Collision collision)
{
Debug.Log("Collision running");
if(collision.gameObject.tag == "Enemy" && this.gameObject.tag == "MainCamera")
{
Debug.Log("Hit Enemy");
if (Controller.Health > 0)
{
Controller.Health -= .2f;
}
} else if (collision.gameObject.tag == "Scenary" && this.gameObject.tag == "Bullet")
{
Debug.Log("Bullet hits Scenary");
Destroy(this.gameObject);
} else if (collision.gameObject.tag == "Enemy" && this.gameObject.tag == "Bullet")
{
Debug.Log("Bullet hits Enemy");
Destroy(this.gameObject);
EnemyScript.Health-=.2f;
}
}
(在不同的i
索引上)。
非常感谢这方面的任何帮助。
感谢。
答案 0 :(得分:1)
首先,push_back
函数被称为push_back
,因为它会推回一个对象。
这意味着,如果您的vector
为空并且您致电push_back
,则您推送的对象将有0
位置。
如果您的vector
中有n
个对象,则表示push_back
推送的对象将具有n
个索引。
myvector.push_back(myset);
std::cout<<"index of myset is "<<myvector.size()-1<<std::endl;
其次,如果要打印值,则必须为operator<<
类创建自己的std::ostream
重载函数。这是在C ++中打印值的常用方法。
假设您想要在set
个花瓶中打印{}
,在方括号[]
中打印矢量:
#include <ostream>
inline std::ostream& operator<<(std::ostream &os,const blockSet_t &mySet)
{
os<<"{ ";
for(const auto &value:mySet)
{
os<<value<<' ';
}
os<<"};
return os;
}
inline std::ostream& operator<<(std::ostream &os,const std::vector<blockSet_t> &myvector)
{
os<<"[ ";
for(const auto &mySet:myvector)
{
os<<mySet<<' ';
}
os<<"];
return os;
}
接下来,您必须cout
这样的对象:
#include <ostream>
main()
{
std::vector<blockSet_t> myvector;
filereader(myvector);
std::cout<<myvector<<std::endl;
}