Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有以下向量:
vector<float> data = { 1.2, 4.3, 2.4 }; vector<float> newdata;
然后,我想填充newdata尽可能n多地模仿data. n可能多于或少于 中的元素数data。我该怎么做呢?
newdata
n
data
使用构造函数获取范围作为输入并计算范围的正确长度:
std::size_t n = ...; std::vector<float> data = { 1.2, 4.3, 2.4 }; std::vector<float> newdata(data.begin(), data.begin() + std::min(data.size(), n));