如何初始化'const std :: vector< T>像c数组 [英] how-to initialize 'const std::vector<T>' like a c array

查看:506
本文介绍了如何初始化'const std :: vector< T>像c数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个优雅的方法来创建和初始化 const std :: vector< const T> like const T a [] = {。 ..} 到固定(和小)数值?

我需要频繁调用一个函数,期望一个向量< T& / code>,但这些值在我的情况下永远不会改变。

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.

原则上我想到了像

namespace {
  const std::vector<const T> v(??);
}

因为v不会在这个编译单元之外使用。

since v won't be used outside of this compilation unit.

推荐答案

您必须等待C ++ 0x或使用Boost.Assign

You either have to wait for C++0x or use something like Boost.Assign to do that.

例如:

#include <boost/assign/std/vector.hpp>
using namespace boost::assign; // bring 'operator+=()' into scope

vector<int> v;
v += 1,2,3,4,5;

对于C ++ 11:

vector<int> luggage_combo = { 1, 2, 3, 4, 5 };

这篇关于如何初始化'const std :: vector&lt; T&gt;像c数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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