C++中的正向列表和对列表以及示例
转发列表
STL中的前向列表实现了单链表。从 C++11 引入,前向列表在插入、删除和移动操作(如排序)方面比其他容器更有用,并且允许时间常数插入和删除元素。它与列表的不同之处在于,前向列表仅跟踪下一个元素的位置,而列表同时跟踪下一个和前一个元素,从而增加了存储每个元素所需的存储空间。前向列表的缺点是它不能向后迭代,并且它的各个元素不能直接访问。当只需要前向遍历时,前向列表优于列表(就像单链表优于双向链表一样),因为我们可以节省空间。一些示例情况是,散列中的链接,图的邻接表表示等。
与前向列表一起使用的函数:
- push_front():该函数用于在前向列表的第一个位置插入元素。该函数的值被复制到容器中第一个元素之前的空间。前向列表的大小增加 1。
- pop_front():该函数用于删除列表的第一个元素。
列表
列表是允许非连续内存分配的序列容器。与向量相比,列表的遍历速度较慢,但一旦找到位置,插入和删除都很快。通常,当我们说 List 时,我们谈论的是双向链表。为了实现单链表,我们使用前向列表。
与列表一起使用的函数:
- front():返回列表中第一个元素的值。
- back():返回列表中最后一个元素的值。
- push_front(x):在列表的开头添加一个新元素“x”。
- push_back(x):在列表末尾添加一个新元素“x”。
一对
对容器是在
语法 1:
forward_list
Here,
data_type1 and data_type2 are the data types.
Declared forward list can store pairs as an element that consist of those data types only.
语法 2:
list
Here,
data_type1 and data_type2 are the data types.
Declared list can store pairs as an element that consists of those data types only.
对的转发列表
下面是对的前向列表的实现:
示例 1:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print forward
// list elements
void print(forward_list >&
forwardListOfPairs)
{
cout << "Forward List : " << '\n';
for (auto currentPair : forwardListOfPairs)
{
// Each element of the forwardList is
// a pair itself
pair currentpair = currentPair;
cout << "[ ";
// Printing pair contents
cout << currentPair.first << ' ' <<
currentPair.second;
cout << ']';
cout << '\n';
}
}
// Driver code
int main()
{
// Declaring a forward list of pairs
forward_list >
forwardListOfPairs;
// Declaring a pair
pair pair1;
// Initializing the
// pair
pair1 = make_pair(11, 22);
// Push the pair at the back
// in the forward list
forwardListOfPairs.push_front(pair1);
// Declaring another pair
pair pair2;
// Initializing the
// pair
pair2 = make_pair(33, 44);
// Push the pair at the front
// in the forward list
forwardListOfPairs.push_front(pair2);
// Declaring another pair
pair pair3;
// Initializing the pair
pair3 = make_pair(55, 66);
// Push the pair at the front
// in the forward list
forwardListOfPairs.push_front(pair3);
// Declaring another pair
pair pair4;
// Initializing the pair
pair4 = make_pair(77, 88);
// Push the pair at the front
// in the forward list
forwardListOfPairs.push_front(pair4);
// Calling print function
print(forwardListOfPairs);
return 0;
}
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print list
// contents
void print(forward_list >&
forwardListOfPairs)
{
cout << "Forward List : " << '\n';
for (auto currentPair : forwardListOfPairs)
{
// Each element of the forward list is
// a pair itself
pair currentpair = currentPair;
cout << "[ ";
// Printing pair elements
cout << "First: " << currentPair.first <<
" and " << "Second: " <<
currentPair.second;
cout << ']';
cout << '\n';
}
}
// Driver code
int main()
{
// Declaring a forward list of pairs
forward_list >
forwardListOfPairs;
// Declaring a pair
pair pair1;
// Initializing the
// pair
pair1 = make_pair(1, "Geeks");
// Push the pair at the back
// in the forwardList
forwardListOfPairs.push_front(pair1);
// Declaring another pair
pair pair2;
// Initializing the
// pair
pair2 = make_pair(2, "for");
// Push the pair at the front
// in the forward list
forwardListOfPairs.push_front(pair2);
// Declaring another pair
pair pair3;
// Initializing the pair
pair3 = make_pair(3, "Geeks");
// Push the pair at the front
// in the forwardList
forwardListOfPairs.push_front(pair3);
// Declaring another pair
pair pair4;
// Initializing the pair
pair4 = make_pair(4, "C++");
// Push the pair at the front
// in the forwardList
forwardListOfPairs.push_front(pair4);
// Calling print function
print(forwardListOfPairs);
return 0;
}
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print list
// contents
void print(list >&
listOfPairs)
{
cout << "List : " << '\n';
for (auto currentPair : listOfPairs)
{
// Each element of the list is
// a pair itself
pair currentpair = currentPair;
cout << "[ ";
// Printing pair elements
cout << "First: " << currentPair.first <<
" and " << "Second: " <<
currentPair.second;
cout << ']';
cout << '\n';
}
}
// Driver code
int main()
{
// Declaring a list of pairs
list > listOfPairs;
// Declaring a pair
pair pair1;
// Initializing the
// pair
pair1 = make_pair(11, 22);
// Push the pair at the back
// in the list
listOfPairs.push_back(pair1);
// Declaring another pair
pair pair2;
// Initializing the
// pair
pair2 = make_pair(33, 44);
// Push the pair at the back
// in the list
listOfPairs.push_back(pair2);
// Declaring another pair
pair pair3;
// Initializing the pair
pair3 = make_pair(55, 66);
// Push the pair at the front
// in the list
listOfPairs.push_front(pair3);
// Declaring another pair
pair pair4;
// Initializing the pair
pair4 = make_pair(77, 88);
// Push the pair at the back
// in the list
listOfPairs.push_back(pair4);
// Calling print function
print(listOfPairs);
return 0;
}
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print list
// contents
void print(list >&
listOfPairs)
{
cout << "List : " << '\n';
for (auto currentPair : listOfPairs)
{
// Each element of the forwardList is
// a pair itself
pair currentpair = currentPair;
cout << "[ ";
// Printing pair contents
cout << "First: " << currentPair.first <<
" and " << "Second: " <<
currentPair.second;
cout << ']';
cout << '\n';
}
}
// Driver code
int main()
{
// Declaring a list of pairs
list > listOfPairs;
// Declaring a pair
pair pair1;
// Initializing the
// pair
pair1 = make_pair(1, "Geeks");
// Push the pair at the back
// in the list
listOfPairs.push_front(pair1);
// Declaring another pair
pair pair2;
// Initializing the
// pair
pair2 = make_pair(2, "for");
// Push the pair at the front
// in the list
listOfPairs.push_front(pair2);
// Declaring another pair
pair pair3;
// Initializing the pair
pair3 = make_pair(3, "Geeks");
// Push the pair at the front
// in the list
listOfPairs.push_front(pair3);
// Declaring another pair
pair pair4;
// Initializing the pair
pair4 = make_pair(4, "C++");
// Push the pair at the front
// in the list
listOfPairs.push_front(pair4);
// Calling print function
print(listOfPairs);
return 0;
}
Forward List :
[ 77 88]
[ 55 66]
[ 33 44]
[ 11 22]
示例 2:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print list
// contents
void print(forward_list >&
forwardListOfPairs)
{
cout << "Forward List : " << '\n';
for (auto currentPair : forwardListOfPairs)
{
// Each element of the forward list is
// a pair itself
pair currentpair = currentPair;
cout << "[ ";
// Printing pair elements
cout << "First: " << currentPair.first <<
" and " << "Second: " <<
currentPair.second;
cout << ']';
cout << '\n';
}
}
// Driver code
int main()
{
// Declaring a forward list of pairs
forward_list >
forwardListOfPairs;
// Declaring a pair
pair pair1;
// Initializing the
// pair
pair1 = make_pair(1, "Geeks");
// Push the pair at the back
// in the forwardList
forwardListOfPairs.push_front(pair1);
// Declaring another pair
pair pair2;
// Initializing the
// pair
pair2 = make_pair(2, "for");
// Push the pair at the front
// in the forward list
forwardListOfPairs.push_front(pair2);
// Declaring another pair
pair pair3;
// Initializing the pair
pair3 = make_pair(3, "Geeks");
// Push the pair at the front
// in the forwardList
forwardListOfPairs.push_front(pair3);
// Declaring another pair
pair pair4;
// Initializing the pair
pair4 = make_pair(4, "C++");
// Push the pair at the front
// in the forwardList
forwardListOfPairs.push_front(pair4);
// Calling print function
print(forwardListOfPairs);
return 0;
}
Forward List :
[ First: 4 and Second: C++]
[ First: 3 and Second: Geeks]
[ First: 2 and Second: for]
[ First: 1 and Second: Geeks]
对列表
下面是对列表的实现:
示例 1:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print list
// contents
void print(list >&
listOfPairs)
{
cout << "List : " << '\n';
for (auto currentPair : listOfPairs)
{
// Each element of the list is
// a pair itself
pair currentpair = currentPair;
cout << "[ ";
// Printing pair elements
cout << "First: " << currentPair.first <<
" and " << "Second: " <<
currentPair.second;
cout << ']';
cout << '\n';
}
}
// Driver code
int main()
{
// Declaring a list of pairs
list > listOfPairs;
// Declaring a pair
pair pair1;
// Initializing the
// pair
pair1 = make_pair(11, 22);
// Push the pair at the back
// in the list
listOfPairs.push_back(pair1);
// Declaring another pair
pair pair2;
// Initializing the
// pair
pair2 = make_pair(33, 44);
// Push the pair at the back
// in the list
listOfPairs.push_back(pair2);
// Declaring another pair
pair pair3;
// Initializing the pair
pair3 = make_pair(55, 66);
// Push the pair at the front
// in the list
listOfPairs.push_front(pair3);
// Declaring another pair
pair pair4;
// Initializing the pair
pair4 = make_pair(77, 88);
// Push the pair at the back
// in the list
listOfPairs.push_back(pair4);
// Calling print function
print(listOfPairs);
return 0;
}
List :
[ First: 55 and Second: 66]
[ First: 11 and Second: 22]
[ First: 33 and Second: 44]
[ First: 77 and Second: 88]
示例 2:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print list
// contents
void print(list >&
listOfPairs)
{
cout << "List : " << '\n';
for (auto currentPair : listOfPairs)
{
// Each element of the forwardList is
// a pair itself
pair currentpair = currentPair;
cout << "[ ";
// Printing pair contents
cout << "First: " << currentPair.first <<
" and " << "Second: " <<
currentPair.second;
cout << ']';
cout << '\n';
}
}
// Driver code
int main()
{
// Declaring a list of pairs
list > listOfPairs;
// Declaring a pair
pair pair1;
// Initializing the
// pair
pair1 = make_pair(1, "Geeks");
// Push the pair at the back
// in the list
listOfPairs.push_front(pair1);
// Declaring another pair
pair pair2;
// Initializing the
// pair
pair2 = make_pair(2, "for");
// Push the pair at the front
// in the list
listOfPairs.push_front(pair2);
// Declaring another pair
pair pair3;
// Initializing the pair
pair3 = make_pair(3, "Geeks");
// Push the pair at the front
// in the list
listOfPairs.push_front(pair3);
// Declaring another pair
pair pair4;
// Initializing the pair
pair4 = make_pair(4, "C++");
// Push the pair at the front
// in the list
listOfPairs.push_front(pair4);
// Calling print function
print(listOfPairs);
return 0;
}
List :
[ First: 4 and Second: C++]
[ First: 3 and Second: Geeks]
[ First: 2 and Second: for]
[ First: 1 and Second: Geeks]