从2个向量的连接构造向量 [英] Construction a vector from the concatenation of 2 vectors
本文介绍了从2个向量的连接构造向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种方法来构建向量
作为2 向量
的连接(除了创建帮助函数?)
例如:
const向量< int& first = {13};
const vector< int> second = {42};
const vector< int> concatenation = first + second;
我知道向量
't 有一个添加运算符 string
,但这是我想要的行为。 concatenation
将包含:13和42。
我知道我可以初始化 concatenation
这样,但它阻止我做连接
const
:
矢量< int> concatenation = first;
first.insert(concatenation.end(),second.cbegin(),second.cend());
解决方案
不,如果您需要
- 未定义帮助函数,
- 结果向量可以声明为
const
。
Is there a way to construct a vector
as the concatenation of 2 vector
s (Other than creating a helper function?)
For example:
const vector<int> first = {13};
const vector<int> second = {42};
const vector<int> concatenation = first + second;
I know that vector
doesn't have an addition operator like string
, but that's the behavior that I want. Such that concatenation
would contain: 13 and 42.
I know that I can initialize concatenation
like this, but it prevents me from making concatenation
const
:
vector<int> concatenation = first;
first.insert(concatenation.end(), second.cbegin(), second.cend());
解决方案
No, it's not possible if you require that
- no helper function is defined, and
- the resulting vector can be declared
const
.
这篇关于从2个向量的连接构造向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文