本文介绍了如何初始化“const std::vector<T>"像一个c数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
Is there an elegant way to create and initialize a const std::vector<const T>
like const T a[] = { ... }
to a fixed (and small) number of values?
I need to call a function frequently which expects a vector<T>
, but these values will never change in my case.
In principle I thought of something like
namespace {
const std::vector<const T> v(??);
}
since v won't be used outside of this compilation unit.
解决方案
For C++11:
vector<int> luggage_combo = { 1, 2, 3, 4, 5 };
Original answer:
You would either have to wait for C++0x or use something like Boost.Assign to do that.
e.g.:
#include <boost/assign/std/vector.hpp>
using namespace boost::assign; // bring 'operator+=()' into scope
vector<int> v;
v += 1,2,3,4,5;
这篇关于如何初始化“const std::vector<T>"像一个c数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!