java push_back,将一个结构push_back()一个向量
How can I push_back a struct into a vector?
struct point {
int x;
int y;
};
vector a;
a.push_back( ??? );
解決方案
point mypoint = {0, 1};
a.push_back(mypoint);
Or if you're allowed, give point a constructor, so that you can use a temporary:
a.push_back(point(0,1));
Some people will object if you put a constructor in a class declared with struct, and it makes it non-POD, and maybe you aren't in control of the definition of point. So this option might not be available to you. However, you can write a function which provides the same convenience:
point make_point(int x, int y) {
point mypoint = {x, y};
return mypoint;
}
a.push_back(make_point(0, 1));
總結
以上是生活随笔為你收集整理的java push_back,将一个结构push_back()一个向量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java用法_Java 习惯用法总结
- 下一篇: java爬虫框架动态_java爬虫框架w