从std :: vector< double>转换到std :: vector< int> [英] Convert from std::vector<double> to std::vector<int>
问题描述
是否有非暴力方法这样做?
transform()看起来可能但没有预定义的函数对象。
std :: vector< double> src;
std :: vector< int> dest;
std :: vector< double> :: size_type size = src.size();
dest.reserve(size);
for(std :: vector< int> :: size_type i = 0;
i< size;
i ++)
{
dest [i] = static_cast< int>(src [i]);
}
"匿名" <在****** @ hotmail.com>在消息中写道
新闻:在**************************** @ news-fe-03.texas .rr.com ...
是否有非暴力方法这样做?
transform()看起来可能但没有预定义的函数对象。
std :: vector< double> src;
std :: vector< int> dest;
std :: vector< double> :: size_type size = src.size();
dest.reserve(size);
for(std :: vector< int> :: size_type i = 0;
i< size;
i ++)
{
dest [i] = static_cast< int>(src [i]);
}
怎么样?
std :: vector< int> dest(src.begin(),src.end());
我不记得迭代器用于初始化的任何要求
vector必须引用与vector元素相同类型的值。
Andrew Koenig schrieb:" Anonymous" <在****** @ hotmail.com>在消息中写道
新闻:在**************************** @ news-fe-03.texas.rr.com ...
是否有非暴力方法这样做?
transform()看起来可能但没有预定义的函数对象。
std :: vector< double> src;
std :: vector< int> dest;
std :: vector< double> :: size_type size = src.size();
dest.reserve(size);
for(std :: vector< int> :: size_type i = 0;
i< size;
i ++)
{
dest [i] = static_cast< int>(src [i]);
}
怎么样?
std :: vector< int> dest(src.begin(),src.end());
我不记得任何用于初始化
向量的迭代器必须引用它的值的要求键入为向量元素。
对,这是直截了当的方式而且有效,但可能导致
相当长关于double-> int转换的警告:
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits /stl_algobase.h:
在
函数`_OutputIter std :: __ copy(_RandomAccessIter,_RandomAccessIter,
_OutputIter,std :: random_access_iterator_tag)[ with _RandomAccessIter =
double *,_ OutputIter = int *]'':
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4 /include/g++-v3/bits/stl_algobase.h:266:
从`_OutputIter std :: __ copy_aux2实例化(_InputIter,
_InputIter,_OutputIter,__ true_type)[与_InputIter = double *,
_OutputIter = int *]''
/ usr / lib / gcc-lib / i 686-pc-linux-gnu / 3.3.4 / include / g ++ - v3 / bits / stl_algobase.h:303:
从`_OutputIter std :: __ copy_ni2实例化(_InputIter,_InputIter,>
_OutputIter,__ false_type)[with _InputIter = double *,_ OutputIter = int *]''
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3。 4 / include / g ++ - v3 / bits / stl_algobase.h:314:
从`_OutputIter std :: __ copy_ni1实例化(_InputIter,_InputIter,
_OutputIter,__ true_type)[ with _InputIter =
__gnu_cxx :: __ normal_iterator< double *,std :: vector< double,
std :: allocator< double> > >,_ OutputIter = int *]''
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits/stl_algobase.h :349:
实例化`_OutputIter std :: copy(_InputIter,_InputIter,
_OutputIter)[with _InputIter = __gnu_cxx :: __ normal_iterator< double *,
std :: vector< double,std :: allocator< double> > >,_OutputIter = int *]''
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits/stl_uninitialized.h :76:
实例化`_ForwardIter
std :: __ uninitialized_copy_aux(_InputIter,_InputIter,_ForwardIter,
__true_type)[with _InputIter = __gnu_cxx: :__ normal_iterator< double *,
std :: vector< double,std :: allocator< double> > >,_ ForwardIter = int *]''
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits/stl_uninitialized.h :112:
实例化`_ForwardIter std :: uninitialized_copy(_InputIter,
_InputIter,_ForwardIter)[with _InputIter =
__gnu_cxx :: __ normal_iterator< ; double *,std :: vector< double,
std :: allocator< double> > >,_ ForwardIter = int *]''
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits/stl_vector.h :829:
实例化为`void std :: vector< _Tp,
_Alloc> :: _ M_range_initialize(_ForwardIterator,_ForwardIterator,
std: :forward_iterator_tag)[with _ForwardIterator =
__gnu_cxx :: __ normal_iterator< double *,std :: vector< double,
std :: allocator< double> > >,_ Tp = int,_Alloc = std :: allocator< int>]''
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/ g ++ - v3 / bits / stl_vector.h:807:
实例化自`void std :: vector< _Tp,
_Alloc> :: _ M_initialize_dispatch(_InputIter,_InputIter,__ false_type )
[使用_InputIter = __gnu_cxx :: __ normal_iterator< double *,
std :: vector< double,std :: allocator< double> > >,_ Tp = int,_Alloc =
std :: allocator< int>]''
/ usr / lib / gcc-lib / i686-pc-linux- gnu / 3.3.4 / include / g ++ - v3 / bits / stl_vector.h:289:
从`std :: vector< _Tp,_Alloc> :: vector(_InputIterator,
)实例化
_InputIterator,typename std :: _ Vector_base< _Tp,
_Alloc> :: allocator_type&)[with _InputIterator =
__gnu_cxx :: __ normal_iterator< double *,std :: vector< double,
std :: allocator< double> > >,_ Tp = int,_Alloc = std :: allocator< int>]''
foo.cpp:6:从这里实例化
/ usr / lib / gcc -lib / i686-pc-linux-gnu / 3.3.4 / include / g ++ - v3 / bits / stl_algobase.h:241:
警告:转换
到` int''从'double''
干杯,
Malte
2005-03-03 -28,Anonymous< in ****** @ hotmail.com>写道:有没有一个非暴力方法这样做?
transform()看起来可能但没有预定义的函数对象。
我只是将static_cast包装在一个函数对象中,例如
template< typename T>
struct Static_cast {
模板< typename U>
T operator()(const U& x)const {return static_cast< T> (X); }
};
不漂亮,但完成工作并关闭编译器。我碰巧
相信能够明确地投射一个容器到另一个容器是一个足够的理由写一个5行功能对象。
干杯,
-
Donovan Rebbechi
http://pegasus.rutgers .edu / ~ellflord /
Is there a non-brute force method of doing this?
transform() looked likely but had no predefined function object.
std::vector<double> src;
std::vector<int> dest;
std::vector<double>::size_type size = src.size();
dest.reserve(size);
for (std::vector<int>::size_type i = 0;
i < size;
i++)
{
dest[i] = static_cast<int>(src[i]);
}
"Anonymous" <in******@hotmail.com> wrote in message
news:in****************************@news-fe-03.texas.rr.com...
Is there a non-brute force method of doing this?
transform() looked likely but had no predefined function object. std::vector<double> src;
std::vector<int> dest;
std::vector<double>::size_type size = src.size();
dest.reserve(size);
for (std::vector<int>::size_type i = 0;
i < size;
i++)
{
dest[i] = static_cast<int>(src[i]);
}
How about this?
std::vector<int> dest(src.begin(), src.end());
I can''t recall any requirement that the iterators used to initialize a
vector must refer to values of the same type as the vector elements.
Andrew Koenig schrieb:"Anonymous" <in******@hotmail.com> wrote in message
news:in****************************@news-fe-03.texas.rr.com...Is there a non-brute force method of doing this?
transform() looked likely but had no predefined function object.
std::vector<double> src;
std::vector<int> dest;
std::vector<double>::size_type size = src.size();
dest.reserve(size);
for (std::vector<int>::size_type i = 0;
i < size;
i++)
{
dest[i] = static_cast<int>(src[i]);
}
How about this?
std::vector<int> dest(src.begin(), src.end());
I can''t recall any requirement that the iterators used to initialize a
vector must refer to values of the same type as the vector elements.
Right, that''s the straightforward way and it works, but can result in a
rather longish warning about the double->int conversion:
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits/stl_algobase.h:
In
function `_OutputIter std::__copy(_RandomAccessIter, _RandomAccessIter,
_OutputIter, std::random_access_iterator_tag) [with _RandomAccessIter =
double*, _OutputIter = int*]'':
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits/stl_algobase.h:266:
instantiated from `_OutputIter std::__copy_aux2(_InputIter,
_InputIter, _OutputIter, __true_type) [with _InputIter = double*,
_OutputIter = int*]''
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits/stl_algobase.h:303:
instantiated from `_OutputIter std::__copy_ni2(_InputIter, _InputIter,
_OutputIter, __false_type) [with _InputIter = double*, _OutputIter = int*]''
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits/stl_algobase.h:314:
instantiated from `_OutputIter std::__copy_ni1(_InputIter, _InputIter,
_OutputIter, __true_type) [with _InputIter =
__gnu_cxx::__normal_iterator<double*, std::vector<double,
std::allocator<double> > >, _OutputIter = int*]''
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits/stl_algobase.h:349:
instantiated from `_OutputIter std::copy(_InputIter, _InputIter,
_OutputIter) [with _InputIter = __gnu_cxx::__normal_iterator<double*,
std::vector<double, std::allocator<double> > >, _OutputIter = int*]''
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits/stl_uninitialized.h:76:
instantiated from `_ForwardIter
std::__uninitialized_copy_aux(_InputIter, _InputIter, _ForwardIter,
__true_type) [with _InputIter = __gnu_cxx::__normal_iterator<double*,
std::vector<double, std::allocator<double> > >, _ForwardIter = int*]''
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits/stl_uninitialized.h:112:
instantiated from `_ForwardIter std::uninitialized_copy(_InputIter,
_InputIter, _ForwardIter) [with _InputIter =
__gnu_cxx::__normal_iterator<double*, std::vector<double,
std::allocator<double> > >, _ForwardIter = int*]''
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits/stl_vector.h:829:
instantiated from `void std::vector<_Tp,
_Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator,
std::forward_iterator_tag) [with _ForwardIterator =
__gnu_cxx::__normal_iterator<double*, std::vector<double,
std::allocator<double> > >, _Tp = int, _Alloc = std::allocator<int>]''
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits/stl_vector.h:807:
instantiated from `void std::vector<_Tp,
_Alloc>::_M_initialize_dispatch(_InputIter, _InputIter, __false_type)
[with _InputIter = __gnu_cxx::__normal_iterator<double*,
std::vector<double, std::allocator<double> > >, _Tp = int, _Alloc =
std::allocator<int>]''
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits/stl_vector.h:289:
instantiated from `std::vector<_Tp, _Alloc>::vector(_InputIterator,
_InputIterator, typename std::_Vector_base<_Tp,
_Alloc>::allocator_type&) [with _InputIterator =
__gnu_cxx::__normal_iterator<double*, std::vector<double,
std::allocator<double> > >, _Tp = int, _Alloc = std::allocator<int>]''
foo.cpp:6: instantiated from here
/usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.4/include/g++-v3/bits/stl_algobase.h:241:
warning: converting
to `int'' from `double''
Cheers,
Malte
On 2005-03-28, Anonymous <in******@hotmail.com> wrote:Is there a non-brute force method of doing this?
transform() looked likely but had no predefined function object.
I''d just wrap static_cast in a function object, e.g.
template <typename T>
struct Static_cast {
template <typename U>
T operator () (const U& x) const { return static_cast<T> (x); }
};
Not pretty, but gets the job done and shuts the compiler up. I happen to
believe that being able to explicitly "cast" one container to another is a
good enough reason to write a 5 line function object.
Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
这篇关于从std :: vector< double>转换到std :: vector< int>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!