使用预定义值分配矢量 [英] assigning vector with predefined values
问题描述
我想为矢量分配预定义的vallues,例如:
std :: vector< doubletr;
tr = {3.36,2.09,1.47,1.1,0.87,0.72};
我该怎么做?我可以使用.push_back()来分配没有
的值吗? ?
提前致谢
Patrick
Hi,
I want to assign predefined vallues to a vector, like:
std::vector<doubletr;
tr={3.36,2.09,1.47,1.1,0.87,0.72};
how do i do this? Am I able to assign values without
using .push_back(); ?
Thanks in advance
Patrick
推荐答案
* meisterbartsch:
* meisterbartsch:
>
我想为一个向量分配预定义的vallues,例如:
std :: vector< doubletr;
tr = {3.36,2.09,1.47,1.1,0.87,0.72};
我该怎么办这个?
>
I want to assign predefined vallues to a vector, like:
std::vector<doubletr;
tr={3.36,2.09,1.47,1.1,0.87,0.72};
how do i do this?
你可以用这种方式初始化一个向量:
double const values [] = {1,2,3};
std :: size_t const n = sizeof(values)/ sizeof(* values);
std :: vector< doubletr(values,values + n );
您可以使用''assign''成员函数分配给向量:
tr.assign(值,值+ n);
You can initialize a vector this way:
double const values[] = {1, 2, 3};
std::size_t const n = sizeof(values)/sizeof(*values);
std::vector<doubletr( values, values+n );
You can assign to the vector using the ''assign'' member function:
tr.assign( values, values+n );
我可以在不使用.push_back()的情况下分配值。 ?
Am I able to assign values without using .push_back(); ?
是的。
-
答:因为它弄乱了订单人们通常会阅读文字。
问:为什么这么糟糕?
A:热门帖子。
问:什么是在usenet和电子邮件中最烦人的事情是?
Yes.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
4月27日,11:17,Alf P. Steinbach < a ... @ start.nowrote:
On 27 Apr, 11:17, "Alf P. Steinbach" <a...@start.nowrote:
* meisterbartsch:
* meisterbartsch:
< snip>
也看看boost :: assign库
<snip>
also look at boost::assign library
#include< algorithm>
#include< iostream>
#include< vector>
#include< iterator>
#include< cstddef> ;
使用命名空间std;
int main(){
vector< doubletr;
double double_list [] = {3.36,2.09,1.47,1.1,0.87,0.72};
size_t n = sizeof(double_list)/ sizeof(double_list [0]) ;
cout<< n<< endl;
copy(double_list,double_list + n,back_inserter(tr));
copy(tr.begin(),tr.end(),ostream_iterator< double( cout," \ n"));
返回EXIT_SUCCESS;
}
#include <algorithm>
#include <iostream>
#include <vector>
#include <iterator>
#include <cstddef>
using namespace std;
int main() {
vector<doubletr;
double double_list[] = {3.36,2.09,1.47,1.1,0.87,0.72};
size_t n = sizeof(double_list)/sizeof(double_list[0]);
cout << n << endl;
copy(double_list, double_list+n, back_inserter(tr));
copy(tr.begin(), tr.end(), ostream_iterator<double(cout, "\n"));
return EXIT_SUCCESS;
}
>
这篇关于使用预定义值分配矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!