C++ STL中的list::push_front() 和 list::push_back()
我们知道C++编程语言在其标准模板库(STL)中具有预定义的数据结构和算法实现;如果没有该实现,我们需要编写整个代码,这会给编译器带来很多编译时间,对于坐在计算机前的程序员来说也是一种耗时的过程。
list是一种重要的数据结构,被视为容器,帮助我们存储数据,可以是任何数据类型,如int、char、string、long、short等。push_front()操作帮助我们向已创建的List数据结构中添加元素,而push_back()函数允许我们从List数据结构的末尾添加特性或数据。
语法
listname.push_front(value)
C++ 代码
// Here we are writing down the C++ programming language code to demonstrate
// the concept of list::push_front() and list::push_back() in C++ STL
#include
#include
using namespace std;
// The main driver code functionality starts from here
int main()
{
// the below code snippet helps us with creating a List Data structure
// in C++ programming language STL (Standard Template Library)
list mylist{ 1, 2, 3, 4, 5 };
//the below code snippet helps us with pushing the data to the list
mylist.push_front(6);
// Now list becomes 6, 1, 2, 3, 4, 5
// Now the list contains the elements 6, 1, 2, 3, 4, 5
// now we are using the auto function available for us in the C++
// programming language Standard Template Library helping us to print data
for (auto it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
// end of the C++ programming language code
}
输出:
/ t m p / d J J Y Z f g 6J u.o
// Output of the List created
6 1 2 3 4 5
C++ 代码
// Here we are writing down the C++ programming language code to demonstrate
// the concept of list::push_front() and list::push_back() in C++ STL
#include
#include
using namespace std;
// The main driver code functionality starts from here
int main()
{
// the below code snippet helps us with creating a List Data structure
// in C++ programming language STL (Standard Template Library)
list mylist{};
//the below code snippet helps us push the data to the list
mylist.push_front(43);
mylist.push_front(58);
mylist.push_front(24);
mylist.push_front(6);
mylist.push_front(45);
mylist.push_front(89);
mylist.push_front(7);
// Now list becomes 6 7 24 43 45 58 89
// Now, we shall implement the sorting function
mylist.sort();
// now we are using the auto function available for us in the C++
// programming language Standard Template Library helping us to print
//data
for (auto it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
// end of the C++ programming language code
}
输出:
/ t m p / d J J Y Z f g 6J u.o
6 7 24 43 45 58 89
C++代码
// Here we are writing down the C++ programming language code to demonstrate
// the concept of list::push_front() and list::push_back() in C++ STL
#include
#include
using namespace std;
// The main driver code functionality starts from here
int main()
{
// the below code snippet helps us with creating a List Data structure
// in C++ programming language STL (Standard Template Library)
list mylist{ 1, 2, 3, 4, 5 };
//The code snippet below helps us push the data to the list
mylist.push_back(6);
// Now the list becomes 1, 2, 3, 4, 5, 6
// now we are using the auto function available for us in the C++
// programming language Standard Template Library helping us to print
//data
for (auto it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
// end of the C++ programming language code
}
输出:
/ t m p / d J J Y Z f g 6J u.o
1 2 3 4 5 6
C++代码
// Here we are writing down the C++ programming language code to demonstrate
// the concept of list::push_front() and list::push_back() in C++ STL
#include
#include
using namespace std;
// The main driver code functionality starts from here
int main()
{
// the below code snippet helps us with creating a List Data structure
// in C++ programming language STL (Standard Template Library)
list mylist{};
//The code snippet below helps us push the data to the list
mylist.push_back(7);
mylist.push_back(89);
mylist.push_back(45);
mylist.push_back(6);
mylist.push_back(24);
mylist.push_back(58);
mylist.push_back(43);
// Now the list becomes 6 7 24 43 45 58 89
// Now, we shall implement the sorting function
mylist.sort();
// now we are using the auto function available for us in the C++
// programming language Standard Template Library helping us to print
//data
for (auto it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
// end of the C++ programming language code
}
输出:
6 7 24 43 45 58 89