是否有一种优雅的方法来创建和初始化const std::vector<const T>
类似于const T a[] = { ... }
固定(和小)数量的值?
我需要频繁调用一个期望a的函数vector<T>
,但这些值在我的情况下永远不会改变.
原则上我想到了类似的东西
namespace {
const std::vector<const T> v(??);
}
Run Code Online (Sandbox Code Playgroud)
因为v不会在这个编译单元之外使用.
Fer*_*cio 60
你要么必须等待C++ 0x,要么使用像Boost.Assign这样的东西.
例如:
#include <boost/assign/std/vector.hpp>
using namespace boost::assign; // bring 'operator+=()' into scope
vector<int> v;
v += 1,2,3,4,5;
Run Code Online (Sandbox Code Playgroud)
对于C++ 11:
vector<int> luggage_combo = { 1, 2, 3, 4, 5 };
Run Code Online (Sandbox Code Playgroud)
Ste*_*sop 39
如果您正在询问如何初始化const向量以使其具有有趣的内容,那么答案可能是使用复制构造函数.首先,你费力地填充一个向量,然后从中创建新的const向量.或者,您可以使用向量<InputIterator>(InputIterator,InputIterator)构造函数模板从其他类型的容器或数组进行初始化.如果是一个数组,则可以用初始化列表定义.
这样的东西有希望接近你想要的东西:
const T ra[3] = {t1, t2, t3};
const vector<const T> v(ra, ra+3);
Run Code Online (Sandbox Code Playgroud)
如果你问如何将const向量传递给一个带向量的函数,那么答案是:
要么
后者是其中一项非常正确的事情,会让任何看到它的人对护目镜做出评论,以及他们什么也不做.这正是const_cast的用途,但是有一个相当强大的论据说,如果你需要const_cast,你已经输了.
做这两件事(用复制构造函数创建一个非const的const向量,然后抛弃const)绝对是错误的 - 你应该只使用一个非const向量.所以最多选择其中一个做...
[编辑:刚刚注意到你在谈论vector <T>和const vector <const T>之间的区别.不幸的是,在STL中,vector <const T>和vector <T>是完全不相关的类型,并且在它们之间进行转换的唯一方法是通过复制.这是矢量和数组之间的区别 - T**可以静默安全地转换为const T*const*]
Sha*_*531 15
短而脏的方式(类似于Boost的list_of())
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
template <typename T>
struct vlist_of : public vector<T> {
vlist_of(const T& t) {
(*this)(t);
}
vlist_of& operator()(const T& t) {
this->push_back(t);
return *this;
}
};
int main() {
const vector<int> v = vlist_of<int>(1)(2)(3)(4)(5);
copy(v.begin(), v.end(), ostream_iterator<int>(cout, "\n"));
}
Run Code Online (Sandbox Code Playgroud)
现在,C++ 11具有初始化列表,因此您不需要这样做,甚至不需要使用Boost.但是,作为一个例子,您可以更有效地在C++ 11中执行上述操作:
#include <iostream>
#include <vector>
#include <utility>
#include <ostream>
using namespace std;
template <typename T>
struct vlist_of : public vector<T> {
vlist_of(T&& t) {
(*this)(move(t));
}
vlist_of& operator()(T&& t) {
this->push_back(move(t));
return *this;
}
};
int main() {
const vector<int> v = vlist_of<int>(1)(2)(3)(4)(5);
for (const auto& i: v) {
cout << i << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,它仍然不如使用C++ 11初始化列表那样有效,因为没有为vector定义operator =(vlist_of &&).
tjohns20的修改方式如下,可能是更好的c ++ 11 vlist_of:
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
template <typename T>
class vlist_of {
public:
vlist_of(T&& r) {
(*this)(move(r));
}
vlist_of& operator()(T&& r) {
v.push_back(move(r));
return *this;
}
vector<T>&& operator()() {
return move(v);
}
private:
vector<T> v;
};
int main() {
const auto v = vlist_of<int>(1)(2)(3)(4)(5)();
for (const auto& i : v) {
cout << i << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
Joh*_*ing 12
正如其他人所说,除非你给它一个指向源数组的指针,否则你不能像初始化C风格的数组一样初始化向量.但是在这种情况下,如果你的向量是一个全局const,为什么不使用旧的C风格的数组呢?
const int MyInts[] = {
1, 2, 3, 4, 5};
const size_t NumMyInts = sizeof(MyInts)/sizeof(MyInts[0]);
Run Code Online (Sandbox Code Playgroud)
你甚至可以对这个数组使用STL算法,就像你对const向量使用算法一样......
const int* myInt = std::find( &MyInts[0], &MyInts[NumMyInts], 3);
Run Code Online (Sandbox Code Playgroud)
您可以分两步完成:
namespace {
const T s_actual_array[] = { ... };
const std::vector<const T> s_blah(s_actual_array,
s_actual_array + (sizeof(s_actual_array) / sizeof(s_actual_array[0])));
}
Run Code Online (Sandbox Code Playgroud)
也许不像你想的那么美丽,但功能性.
小智 5
怎么样:
int ar[]={1,2,3,4,5,6};
const int TotalItems = sizeof(ar)/sizeof(ar[0]);
std::vector<int> v(ar, ar+TotalItems);
Run Code Online (Sandbox Code Playgroud)