how-to initialize #39;const std::vectorlt;Tgt;#39; like a c array(如何初始化“const std::vectorlt;Tgt;像一个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数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何初始化“const std::vector<T>"像一个c数组


- 如何对自定义类的向量使用std::find()? 2022-11-07
- 静态初始化顺序失败 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 从python回调到c++的选项 2022-11-16
- Stroustrup 的 Simple_window.h 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- C++ 协变模板 2021-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01