liunx c++stdvectorjob_st job_restore_vector怎么把一个向量合并另一个向量里 使用c++98标准实现给出实现
在C++98标准下,可以使用循环来实现向量的合并。以下是一个示例实现:
#include <iostream>
#include <vector>
struct job_st {
int id;
std::string name;
};
void mergeVectors(std::vector<job_st>& dest, const std::vector<job_st>& src) {
for (std::vector<job_st>::const_iterator it = src.begin(); it != src.end(); ++it) {
dest.push_back(*it);
}
}
int main() {
std::vector<job_st> job_restore_vector;
std::vector<job_st> another_vector;
// 假设another_vector中已经有一些元素
job_st job1 = {1, "Job 1"};
job_st job2 = {2, "Job 2"};
another_vector.push_back(job1);
another_vector.push_back(job2);
mergeVectors(job_restore_vector, another_vector);
// 输出合并后的向量元素
for (std::vector<job_st>::iterator it = job_restore_vector.begin(); it != job_restore_vector.end(); ++it) {
std::cout << "id: " << it->id << ", name: " << it->name << std::endl;
}
return 0;
}
在上述示例中,mergeVectors函数通过循环将src向量中的元素逐个添加到dest向量中,实现了向量的合并。然后在main函数中,我们创建了两个向量job_restore_vector和another_vector,将another_vector中的元素合并到job_restore_vector中,并输出合并后的向量元素。
原文地址: http://www.cveoy.top/t/topic/jcUM 著作权归作者所有。请勿转载和采集!