【发布时间】:2016-02-18 15:40:11
【问题描述】:
有没有办法将 vector 构造为 2 个 vectors 的串联(除了创建辅助函数?)
例如:
const vector<int> first = {13};
const vector<int> second = {42};
const vector<int> concatenation = first + second;
我知道vector 没有 像string 这样的加法运算符,但这就是我想要的行为。这样concatenation 将包含:13 和 42。
我知道我可以像这样初始化concatenation,但是它阻止了我创建concatenation const:
vector<int> concatenation = first;
first.insert(concatenation.end(), second.cbegin(), second.cend());
【问题讨论】:
-
@RyanP 我错过了回答我问题的内容吗?或者我应该假设我的问题的答案是否定的,因为那里没有任何东西可以回答它?
-
如果您不能使用修改容器的方法并且您不想使用辅助方法并且向量没有加法运算符,那么恐怕您的问题的答案是否定的.
-
@JonathanMee 我同意它没有您正在寻找的具体答案,并且可能错过了。但是这个问题有 225 次投票,超过 10 万次浏览,而且我个人认为有很多答案,如果你想要的解决方案存在,它就不会被提及,因为这将是所有给出的答案。在这种情况下。
-
@JonathanMee 其中一些stackoverflow.com/a/4557156/4525052
标签: c++ vector constructor initialization addition