c – 将数组插入向量
发布时间:2020-12-16 09:51:52 所属栏目:百科 来源:网络整理
导读:我需要用向量编写简单的代码,我有点挣扎. 我有缓冲区(char数组)从回调中的另一个程序数据获取,我需要插入到向量的末尾 – 整个数组. vectoru_char vector; callback (new data inside the buffer) { copy buffer to the end of vector; } 最后,向量假设包含
我需要用向量编写简单的代码,我有点挣扎.
我有缓冲区(char数组)从回调中的另一个程序数据获取,我需要插入到向量的末尾 – 整个数组. vector<u_char> vector; callback (new data inside the buffer) { copy buffer to the end of vector; } 最后,向量假设包含来自缓冲区的连续数据. 有没有插入char by char with“for”的有效方法? for(...) vector.push_back[i]=buffer[j] 谢谢! 解决方法
假设你知道大小,你可以插入一个范围:
vector.insert(vector.end(),buffer,buffer + size); 对于这种事情,还有一种更通用的算法: #include <iterator> #include <algorithm> std::copy(buffer,buffer + size,std::back_inserter(vector)); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |