本文介绍了返回一个矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 哪种方法最快/最好: std :: vector< intfoo1() { std :: vector< intv; ... reutun v; } std :: vector< int>& foo2() { static std :: vector< intv; ... reutun v; } void foo3(std :: vector< int>& v) { ... } 我倾向于说foo3是最好的,但foo1和foo2更多 " ;方便和QUOT ;.有没有办法像 foo3一样快速返回矢量? - -Gernot int main(int argc,char ** argv){printf ("%silto%c%cf%cgl%ssic%ccom%c"," ma",58,''g '',64," ba",46,10);}Which method is the fastest/best:std::vector<intfoo1(){std::vector<intv;...reutun v;}std::vector<int>& foo2(){static std::vector<intv;...reutun v;}void foo3(std::vector<int>&v){...}I tend to say foo3 is the best, however foo1 and foo2 are more"convenient". Is there any way of returning a vector as fast as withfoo3?---Gernotint main(int argc, char** argv) {printf("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, ''g'', 64, "ba", 46, 10);}推荐答案 不是线程安全的,我强烈推荐这个(不管 速度)。Not thread safe, i strongly recommend against this one (regardless ofspeed). 你可以自己测量一下,不过我怀疑你会看到 的显着差异(你可能会因为不使用 std :: vector,以便利为代价。) - Roland Csaszar ------- ---- \\\ /// -------------- +43 316 495 2129 软件开发------ \ \\ /// ----------- http:// www。 knapp.com KNAPP物流自动化 - \\V // - mailto:ro ************ @ knapp.comYou could measure it for yourself, though i doubt that you will seesignificant differences (you may get better performance by not usingstd::vector, at the cost of convenience).--Roland Csaszar ----------- \\\ /// -------------- +43 316 495 2129Software Development ------ \\\ /// ----------- http://www.knapp.comKNAPP Logistics Automation - \\V// - mailto:ro************@knapp.com 如果编译器具有RVO,可以快速为foo3。May be fast as foo3 if the compiler has RVO. 编程错误。badly programmed. 首选。 - Marcin Gabryszewski G DATA软件 www.gdata.pl 地址:< ;姓><点><姓><在>< GDATA><点>< pl>preferred.--Marcin GabryszewskiG DATA Software www.gdata.pladdress:<FirstName><dot><Surname><at><gdata><dot>< pl> 有了一个好的编译器,如果在初始化中使用 函数,上面显示的方法将是最快的: std :: vector< inttest = foo1(); 如果你用它来重新分配,它的效率会降低(但是建议使用b / b 扩展语言会带来效率): std: :vector< inttest; .... test = foo1(); 您可以使用std :: swap重新获得速度。With a good compiler, the method shown above will be fastest if thefunction is used in initialisation:std::vector<inttest = foo1();If you use it to reassign, it gets less efficient (but a proposal toextend the language will bring back efficiency):std::vector<inttest;....test = foo1();You can regain speed by using std::swap. 这根本不快:被调用者必须复制整个 向量。This is not fast at all: The callee will have to copy the entirevector. 这个将非常快。问题在于易于使用和 意图:初始化不再可能: std :: vector< inttest; test = foo3 (); 此外,代码将不再具有强大的异常保证(因为 大概是foo3从清除向量开始)。 我会选择foo1作为默认实现,并且可能提供 foo3 as: void foo3(std :: vector< ; int>& v){std :: vector< inttemp = foo1(); std :: swap(v,temp); }。This one will be pretty fast to. The problem is in ease of use andintent: Initialisation is no longer possible:std::vector<inttest;test = foo3();Also, the code will no longer have the strong exception guarantee (aspresumably foo3 begins by clearing the vector).I would choose foo1 as the default implementation, and perhaps supplyfoo3 as:void foo3(std::vector<int>&v) { std::vector<inttemp = foo1();std::swap(v,temp); }. / Peter/Peter 这篇关于返回一个矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 09:33