本文介绍了快速向量初始化C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
出于好奇,我想了解初始化向量的快速方法
out of curiosity i want to know quick ways of initializing vectors
我只知道
double inputar[]={1,0,0,0};
vector<double> input(inputar,inputar+4);
推荐答案
恕我直言,这是当前C ++标准的失败之一.向量可以很好地替代C数组,但是初始化一个数组可以比PITA多得多.
This is IMHO one of the failings of the current C++ standard. Vector makes a great replacement for C arrays, but initializing one is much more of a PITA.
我所听说的最好的是加速作业包.根据文档,您可以使用它来做到这一点:
The best I have heard of is the Boost assignment package. According to the docs, you can do this with it:
#include <boost/assign/std/vector.hpp> // for 'operator+=()'
#include <boost/assert.hpp>;
using namespace std;
using namespace boost::assign; // bring 'operator+=()' into scope
{
vector<int> values;
values += 1,2,3,4,5,6,7,8,9; // insert values at the end of the container
BOOST_ASSERT( values.size() == 9 );
BOOST_ASSERT( values[0] == 1 );
BOOST_ASSERT( values[8] == 9 );
}
这篇关于快速向量初始化C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!