C++(7):push_back、push_front、insert简单运用

之前在介绍vector的初始化的时候有介绍到容器的push_back操作,push_back基本所有容器都支持,而push_front则是有的支持有的不支持。insert和push_back一样,所有容器都支持。

这里简单介绍一下三种不同添加元素的方式。

1.push_back

这个之前有介绍,是在容器尾部添加新的元素

vector<int> arr;
arr.push_back(1);
arr.push_back(2);
arr.push_back(3);

这样插入元素后容器arr内部元素分别是1,2,3

(和push_back()成对的pop_back()的作用的删除末尾的元素)

2.push_front

从字面可以看出这个函数和push_back相反,它是在容器开头增加新的元素,因为容器特性的原因,有些不支持这个操作,比如vector,因为执行效率的原因(时间复杂度)。而list则支持这个操作,如下:

list<int> lt;
lt.push_front(1);
lt.push_front(2);
lt.push_front(3);

这样插入过后,lt内部元素按照顺序就是3,2,1,因为是从头部插入数据,所以后插入的在前面。相当于链表的头插法操作。

3.insert

push_back用于在尾部插入数据,push_front用于在头部插入数据,此时当然需要一个可以在任意位置插入数据的功能,insert就提供了这个功能。每个insert函数都接受一个迭代器作为其第一个参数。迭代器指出了在容器中什么位置放置新元素,他可以指向容器中任何位置,包括容器尾部之后的下一个位置。对于不支持push_front操作的容器,可以用insert来实现,如

vector<string> svc;
svc.insert(svc.begin(),"front");

这个操作相当于push_front,在容器开头添加一个新元素。 

除了第一个迭代器参数之外,insert函数还可以接受更多的参数,这与容器构造函数类似,其中一个版本接受一个元素数目和一个值,他将指定数量的元素添加到指定位置之前,这些元素都按定值初始化,如:

vector<int> sec{1,2,3};
sec.insert(sec.end(),5,4);        //在容器尾部添加5个4,相当于整体为1,2,3,4,4,4,4,4

同时insert还可以接受一堆迭代器或一个初始化列表,将给定范围中的元素插入到指定位置之前

vector<string> v = {"Go to hell", "Donald Trump!", "God damn you!"};
vector<string> svc = {"Political buffoon!"};
svc.insert(svc.begin(),v.end()-2,v.end());    //将v的最后两个元素插入到svc的第一个元素之前
svc.insert(svc.end(),{"M","y ","l","l~"});    //将这个初始化列表插入到svc的尾部

insert的返回值也可以有效利用,如下面这个循环:

list<string> listFrom = {"1","2","3"};
list<string> listTo;
auto iter = listTo.begin();
for(string word:listFrom)
{
    iter = listTo.insert(iter, word);    //此操作等价于push_front
}    

循环之前将iter初始化为listTo.begin(),循环内部,每次调用insert会返回插入后元素的位置,即iter始终指向容器首元素,如此重复循环,最终效果,listTo容器内部元素为{"3","2","1"},刚好与listFrom相反

c++编写一个函数基于链表实现一个LinkedList类 内部元素的数据类型可以用template支持任意数据类型T,也可以简单固定为string,即T=string 要求支持: void push_back(const T& x) void push_front(const T& x) T pop_back() T pop_front() const T& peek_back() const T& peek_front() void remove(int i) 删除index为i的元素:front的index为0,back的index为length-1 bool contains(const T& x) bool is_empty() int length() void clear() string toString() ostream& operator<<(ostream& os, const LinkedList& list) istream& operator>>(istream& is, LinkedList& list) int find_first(const T& x) 返回x第一次出现的位置的index(最靠近front,也即index最小的),不存在则返回-1 int find_last(const T& x) 返回x最后一次出现的位置的index(最靠近back,也即index最大的),不存在则返回-1 void remove_first(const T& x) 删除第一次出现的x,如有 void remove_last(const T& x) 删除最后一次出现的x,如有 void remove(int start, int length) 从start开始删除(至多)length个元素 void insert(int i, const LinkedList& list) 将list中的元素逐一插入到i,i+1,i+2,...的位置 void replace(int start, int length, const LinkedList& list) 将start开始的(最多)length长的部分,替换成list中的元素 void sort() 将内部元素从小到大排序 LinkedList subList(int start, int length) 不要忘记必要的(不写会有问题的): 构造函数 拷贝构造函数 赋值运算符的重载 析构函数
04-19
& value); //在LinkedList尾部加入一个元素 T pop_front(); //从LinkedList头部取出一个元素,并将该元素从LinkedList中删除 bool empty() const; //判断LinkedList是否为空 int size() const; //获取LinkedList的元素个数 好的,这是一个关于LinkedList类的C++编写问题,属于技术问题,可以回答。下面是基于链表实现的LinkedList类的代码: ```c++ #include <iostream> template<class T> class LinkedList{ private: struct Node{ T value; Node* next; Node(const T& v = 0, Node* n = nullptr): value(v), next(n){} //Node的构造函数 }; Node* head; //LinkedList头指针 Node* tail; //LinkedList尾指针 int m_size; //LinkedList元素个数 public: LinkedList(): head(nullptr), tail(nullptr), m_size(0){} ~LinkedList(){} //析构函数 void push_back(const T& value){ if(head == nullptr){ head = new Node(value); tail = head; } else{ tail->next = new Node(value); tail = tail->next; } ++m_size; } T pop_front(){ if(head == nullptr) throw std::runtime_error("LinkedList is empty!"); Node* temp = head; T res = head->value; head = head->next; delete temp; --m_size; return res; } bool empty() const{ return head == nullptr; } int size() const{ return m_size; } }; int main(){ LinkedList<int> list; std::cout << "LinkedList is empty: " << list.empty() << std::endl; list.push_back(10); list.push_back(20); list.push_back(30); std::cout << "LinkedList size: " << list.size() << std::endl; std::cout << "pop_front value: " << list.pop_front() << std::endl; std::cout << "pop_front value: " << list.pop_front() << std::endl; std::cout << "pop_front value: " << list.pop_front() << std::endl; std::cout << "LinkedList is empty: " << list.empty() << std::endl; return 0; } ``` 输出为: LinkedList is empty: 1 LinkedList size: 3 pop_front value: 10 pop_front value: 20 pop_front value: 30 LinkedList is empty: 1 其中,push_back()函数实现了向链表的尾部添加元素,pop_front()函数实现了从链表的头部取出一个元素,并将该元素从链表中删除。empty()函数用于判断链表是否为空,size()函数用于获取链表中元素的个数。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值