做pc端网站哪家好,oa系统登录界面,如何查询网站被百度收录,富阳区住房和城乡建设局网站简介#xff1a; CSDN博客专家#xff0c;专注Android/Linux系统#xff0c;分享多mic语音方案、音视频、编解码等技术#xff0c;与大家一起成长#xff01; 优质专栏#xff1a;Audio工程师进阶系列【原创干货持续更新中……】#x1f680; 人生格言#xff1a; 人生… 简介 CSDN博客专家专注Android/Linux系统分享多mic语音方案、音视频、编解码等技术与大家一起成长 优质专栏Audio工程师进阶系列【原创干货持续更新中……】 人生格言 人生从来没有捷径只有行动才是治疗恐惧和懒惰的唯一良药. 更多原创,欢迎关注Android系统攻城狮 1.前言 本篇目的理解C之vector::insert与vector::insert用法区别。 在C的std::vector容器中insert和push_back是两个不同的方法用于向向量中添加元素的方式有所差异。 insert方法允许在向量的任意位置插入一个或多个元素。它接受两个迭代器参数第一个参数指定了插入位置第二个参数指定了要插入的元素。插入后原有的元素会向后移动腾出空间给新插入的元素。insert方法还可以接受一个范围的迭代器用于指定要插入的一段元素。 push_back方法用于在向量的末尾添加单个元素。它接受一个参数即要添加的元素。新元素被追加到向量的最后不会影响其他元素的位置。
主要区别如下
insert可以在向量的任意位置插入元素而push_back只能在末尾追加元素。insert可以一次性插入多个元素而push_back只能插入一个元素。insert会导致插入点之后的元素向后移动而push_back不会影响其他元素的位置。
如果需要在指定位置插入或插入多个元素可以使用insert方法。如果只需要在末尾添加单个元素可以使用push_back方法。
2.应用实例
v1.0 堆上申请内存push_back尾部插入
#include iostream
#include vectorint main()
{std::vectorstd::string* buf;buf new std::vectorstd::string();std::string buf1 11111111;(*buf).push_back(buf1);printf(xxx--------- %s(), line %d, buf %s\n,__FUNCTION__,__LINE__, (*buf).back().c_str());delete buf;}v2.0 栈上申请内存push_back尾部插入 std::vectorstd::string buf(100);std::string buf1 222222222!;buf.push_back(buf1); //它是插入最后一个元素printf(xxx--------- %s(), line %d, buf[0] %s\n,__FUNCTION__,__LINE__, buf[0].c_str());printf(xxx--------- %s(), line %d, buf[100] %s\n,__FUNCTION__,__LINE__, buf[100].c_str());printf(xxx--------- %s(), line %d, buf.front %s\n,__FUNCTION__,__LINE__, buf.front().c_str());printf(xxx--------- %s(), line %d, buf.back %s\n,__FUNCTION__,__LINE__, buf.back().c_str());v3.0 push_back从尾部插入
#include iostream
#include vectorvoid set_pointer(std::vectorstd::string *buf){std::string buf1 Hello Pointer!;//v1.0 从尾部插入(*buf).push_back(buf1);
}int main() {//std::vectorstd::string buf(0);//也是正确的,自动扩容std::vectorstd::string buf(100);set_pointer(buf);printf(xxx--------- %s(), line %d, buf[0] %s\n,__FUNCTION__,__LINE__, buf[0].c_str());printf(xxx--------- %s(), line %d, buf[100] %s\n,__FUNCTION__,__LINE__, buf[100].c_str());printf(xxx--------- %s(), line %d, buf.front %s\n,__FUNCTION__,__LINE__, buf.front().c_str());printf(xxx--------- %s(), line %d, buf.back %s\n,__FUNCTION__,__LINE__, buf.back().c_str());return 0;
}
v4.0 insert从begin开始位置插入
#include iostream
#include vectorvoid set_pointer(std::vectorstd::string *buf){std::string buf1 Hello Pointer!;//v2.0 从begin头部位置插入.(*buf).insert((*buf).begin(), buf1);
}int main() {//std::vectorstd::string buf(0);//也是正确的,自动扩容std::vectorstd::string buf(100);set_pointer(buf);printf(xxx--------- %s(), line %d, buf[0] %s\n,__FUNCTION__,__LINE__, buf[0].c_str());printf(xxx--------- %s(), line %d, buf[100] %s\n,__FUNCTION__,__LINE__, buf[100].c_str());printf(xxx--------- %s(), line %d, buf.front %s\n,__FUNCTION__,__LINE__, buf.front().c_str());printf(xxx--------- %s(), line %d, buf.back %s\n,__FUNCTION__,__LINE__, buf.back().c_str());return 0;
}
v5.0 对指针pbuf做insert赋值操作,即对它指向的buf对象赋值操作.
#include iostream
#include vector
#include cstringint main() {//1.在栈上创建vectorstd::string内存,并分配100个字节.std::vectorstd::string buf(100);buf.insert(buf.begin(),11111);//2.指针pbuf指向buf的vectorstring对象buf.std::vectorstd::string *pbuf buf;pbuf-insert(buf.begin()1,22222222);printf(buf[0] %s\n,buf.front().data());printf(buf[1] %s\n,buf[1].data());printf((*pbuf)[1] %s\n,(*pbuf)[0].data());printf((*pbuf)[1] %s\n,(*pbuf)[1].data());return 0;
} 因为指针pbuf指向buf,所以对pbuf插入字符串,就是对buf插入操作,buf才是真正的实体.