C++中的正向列表和元组列表与示例
什么是转发列表?
STL中的前向列表用于实现单链表。它是从 C++11 开始引入的,前向列表在插入、删除和移动操作(如排序)中比其他容器更有用,并且允许时间常数插入和删除元素。
它与列表的不同之处在于,前向列表仅跟踪下一个元素的位置,而列表同时跟踪下一个和前一个元素,从而增加了存储每个元素所需的存储空间。
前向列表的缺点是它不能向后迭代,并且它的各个元素不能直接访问。
当只需要前向遍历时,前向列表优于列表(就像单链表优于双向链表一样),因为我们可以节省空间。一些示例情况是,散列中的链接,图的邻接表表示等。
与前向列表相关的函数:
- push_front() :该函数用于在前向列表的第一个位置插入元素。该函数的值被复制到容器中第一个元素之前的空间。前向列表的大小增加 1。
- pop_front():该函数用于删除列表的第一个元素。
- empty() :返回列表是否为空(1)或非(0)。
什么是列表?
列表是允许非连续内存分配的序列容器。与向量相比,列表的遍历速度较慢,但一旦找到位置,插入和删除都很快。通常,当我们说 List 时,我们谈论的是双向链表。为了实现单链表,我们使用前向列表。
与列表关联的函数:
- front() :返回列表中第一个元素的值。
- back() :返回列表中最后一个元素的值。
- push_front(x) :在列表的开头添加一个新元素“x”。
- push_back(x) :在列表末尾添加一个新元素“x”。
- empty() :返回列表是否为空(1)或非(0)。
什么是元组?
C++ 中的元组是一个能够对多个元素进行分组的对象。元素可以是相同的类型,也可以是不同的数据类型。元组元素的初始化顺序可以按照相同的顺序访问。
与元组关联的函数:
1. make_tuple():用于给元组赋值。传递的值应该与元组中声明的值顺序一致。
2. get():用于访问元组值并对其进行修改,它接受索引和元组名称作为参数来访问特定的元组元素。
元组的转发列表
在 C++ 中,元组的前向列表是一个前向列表,其中每个元素都是一个元组本身。虽然,一个元组可以包含更多或更少的元素,但为简单起见,我们使用了只有三个元素的元组。
句法:
forward_list
Here,
dataType1, dataType2, and dataType3 are similar or dissimilar data types.
示例 1:下面是演示元组前向列表工作的 C++ 程序。
C++
// C++ program to demonstrate
// the working of forward list
// of tuples
#include
using namespace std;
// Function to print forward
// list elements
void print(forward_list> &forwardListOftuples)
{
for (auto currentTuple : forwardListOftuples)
{
// Each element of the forward list is
// a tuple itself
tuple tuple = currentTuple;
cout << "[ ";
// Printing tuple elements
cout << get<0>(currentTuple) << ' ' <<
get<1>(currentTuple) << ' ' <<
get<2>(currentTuple);
cout << ']';
cout << '\n';
}
}
// Driver code
int main()
{
// Declaring a forward list of tuples
// having integer values only
forward_list >
forwardListOftuples;
// Declaring a tuple
tuple tuple1;
// Initializing the
// tuple
tuple1 = make_tuple(11, 22, 33);
// Push the tuple at the back
// in the forward list
forwardListOftuples.push_front(tuple1);
// Declaring another tuple
tuple tuple2;
// Initializing the
// tuple
tuple2 = make_tuple(33, 44, 55);
// Push the tuple at the front
// in the forward list
forwardListOftuples.push_front(tuple2);
// Declaring another tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple(55, 66, 77);
// Push the tuple at the front
// in the forward list
forwardListOftuples.push_front(tuple3);
// Declaring another tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple(77, 88, 99);
// Push the tuple at the front
// in the forward list
forwardListOftuples.push_front(tuple4);
// Calling print function
print(forwardListOftuples);
return 0;
}
C++
// C++ program to demonstrate
// the working of forward list
// of tuples
#include
using namespace std;
// Function to print forward
// list elements
void print(forward_list> &forwardListOftuples)
{
for (auto currentTuple : forwardListOftuples)
{
// Each element of the forward list is
// a tuple itself
tuple tuple =
currentTuple;
cout << "[ ";
// Printing tuple elements
cout << get<0>(currentTuple) << ' ' <<
get<1>(currentTuple) << ' ' <<
get<2>(currentTuple);
cout << ']';
cout << '\n';
}
}
// Driver code
int main()
{
// Declaring a forward list of tuples
// having first two values of string
// type and third value as bool type
forward_list>
forwardListOftuples;
// Declaring a tuple
tuple tuple1;
// Initializing the
// tuple
tuple1 = make_tuple("GeeksforGeeks",
"Computer Science", 0);
// Push the tuple at the back
// in the forward list
forwardListOftuples.push_front(tuple1);
// Declaring another tuple
tuple tuple2;
// Initializing the
// tuple
tuple2 = make_tuple("Java", "C++", 1);
// Push the tuple at the front
// in the forward list
forwardListOftuples.push_front(tuple2);
// Declaring another tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple("GFG", "C", 1);
// Push the tuple at the front
// in the forward list
forwardListOftuples.push_front(tuple3);
// Declaring another tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple("Swift", "Python", 0);
// Push the tuple at the front
// in the forward list
forwardListOftuples.push_front(tuple4);
// Calling print function
print(forwardListOftuples);
return 0;
}
C++
// C++ program to demonstrate
// the working of list of tuples
#include
using namespace std;
// Function to print
// list elements
void print(list> &listOftuples)
{
for (auto currentTuple : listOftuples)
{
// Each element of the List is
// a tuple itself
tuple tuple = currentTuple;
cout << "[ ";
// Printing tuple elements
cout << get<0>(currentTuple) << ' ' <<
get<1>(currentTuple) << ' ' <<
get<2>(currentTuple);
cout << ']';
cout << '\n';
}
}
// Driver code
int main()
{
// Declaring a List of tuples
// having all values of integer type
list>
listOftuples;
// Declaring a tuple
tuple tuple1;
// Initializing the
// tuple
tuple1 = make_tuple(11, 22, 33);
// Push the tuple at the back
// in the List
listOftuples.push_front(tuple1);
// Declaring another tuple
tuple tuple2;
// Initializing the
// tuple
tuple2 = make_tuple(33, 44, 55);
// Push the tuple at the front
// in the List
listOftuples.push_front(tuple2);
// Declaring another tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple(55, 66, 77);
// Push the tuple at the front
// in the List
listOftuples.push_front(tuple3);
// Declaring another tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple(77, 88, 99);
// Push the tuple at the front
// in the List
listOftuples.push_front(tuple4);
// Calling print function
print(listOftuples);
return 0;
}
C++
// C++ program to demonstrate
// the working of list of tuples
#include
using namespace std;
// Function to print
// list elements
void print(list> &listOftuples)
{
for (auto currentTuple : listOftuples)
{
// Each element of the List is
// a tuple itself
tuple tuple =
currentTuple;
cout << "[ ";
// Printing tuple elements
cout << get<0>(currentTuple) << ' ' <<
get<1>(currentTuple) << ' ' <<
get<2>(currentTuple);
cout << ']';
cout << '\n';
}
}
// Driver code
int main()
{
// Declaring a List of tuples
// having first two values as string type
// and third value is of bool type
list>
listOftuples;
// Declaring a tuple
tuple tuple1;
// Initializing the
// tuple
tuple1 = make_tuple("GeeksforGeeks",
"Computer Science", 0);
// Push the tuple at the back
// in the List
listOftuples.push_front(tuple1);
// Declaring another tuple
tuple tuple2;
// Initializing the
// tuple
tuple2 = make_tuple("Java", "C++", 1);
// Push the tuple at the front
// in the List
listOftuples.push_front(tuple2);
// Declaring another tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple("GFG", "C", 1);
// Push the tuple at the front
// in the List
listOftuples.push_front(tuple3);
// Declaring another tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple("Swift", "Python", 0);
// Push the tuple at the front
// in the List
listOftuples.push_front(tuple4);
// Calling print function
print(listOftuples);
return 0;
}
输出:
[ 77 88 99]
[ 55 66 77]
[ 33 44 55]
[ 11 22 33]
示例 2:下面是演示元组前向列表工作的 C++ 程序。
C++
// C++ program to demonstrate
// the working of forward list
// of tuples
#include
using namespace std;
// Function to print forward
// list elements
void print(forward_list> &forwardListOftuples)
{
for (auto currentTuple : forwardListOftuples)
{
// Each element of the forward list is
// a tuple itself
tuple tuple =
currentTuple;
cout << "[ ";
// Printing tuple elements
cout << get<0>(currentTuple) << ' ' <<
get<1>(currentTuple) << ' ' <<
get<2>(currentTuple);
cout << ']';
cout << '\n';
}
}
// Driver code
int main()
{
// Declaring a forward list of tuples
// having first two values of string
// type and third value as bool type
forward_list>
forwardListOftuples;
// Declaring a tuple
tuple tuple1;
// Initializing the
// tuple
tuple1 = make_tuple("GeeksforGeeks",
"Computer Science", 0);
// Push the tuple at the back
// in the forward list
forwardListOftuples.push_front(tuple1);
// Declaring another tuple
tuple tuple2;
// Initializing the
// tuple
tuple2 = make_tuple("Java", "C++", 1);
// Push the tuple at the front
// in the forward list
forwardListOftuples.push_front(tuple2);
// Declaring another tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple("GFG", "C", 1);
// Push the tuple at the front
// in the forward list
forwardListOftuples.push_front(tuple3);
// Declaring another tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple("Swift", "Python", 0);
// Push the tuple at the front
// in the forward list
forwardListOftuples.push_front(tuple4);
// Calling print function
print(forwardListOftuples);
return 0;
}
输出:
[ Swift Python 0]
[ GFG C 1]
[ Java C++ 1]
[ GeeksforGeeks Computer Science 0]
元组列表
在 C++ 中,元组列表是一个列表,其中每个元素都是一个元组本身。虽然,一个元组可以包含更多或更少的元素,但为简单起见,我们使用了只有三个元素的元组。
句法:
list
Here,
dataType1, dataType2, and dataType3 are similar or dissimilar data types.
示例 1:下面是演示元组列表工作的 C++ 程序。
C++
// C++ program to demonstrate
// the working of list of tuples
#include
using namespace std;
// Function to print
// list elements
void print(list> &listOftuples)
{
for (auto currentTuple : listOftuples)
{
// Each element of the List is
// a tuple itself
tuple tuple = currentTuple;
cout << "[ ";
// Printing tuple elements
cout << get<0>(currentTuple) << ' ' <<
get<1>(currentTuple) << ' ' <<
get<2>(currentTuple);
cout << ']';
cout << '\n';
}
}
// Driver code
int main()
{
// Declaring a List of tuples
// having all values of integer type
list>
listOftuples;
// Declaring a tuple
tuple tuple1;
// Initializing the
// tuple
tuple1 = make_tuple(11, 22, 33);
// Push the tuple at the back
// in the List
listOftuples.push_front(tuple1);
// Declaring another tuple
tuple tuple2;
// Initializing the
// tuple
tuple2 = make_tuple(33, 44, 55);
// Push the tuple at the front
// in the List
listOftuples.push_front(tuple2);
// Declaring another tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple(55, 66, 77);
// Push the tuple at the front
// in the List
listOftuples.push_front(tuple3);
// Declaring another tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple(77, 88, 99);
// Push the tuple at the front
// in the List
listOftuples.push_front(tuple4);
// Calling print function
print(listOftuples);
return 0;
}
输出:
[ 77 88 99]
[ 55 66 77]
[ 33 44 55]
[ 11 22 33]
示例 2:下面是演示元组列表工作的 C++ 程序。
C++
// C++ program to demonstrate
// the working of list of tuples
#include
using namespace std;
// Function to print
// list elements
void print(list> &listOftuples)
{
for (auto currentTuple : listOftuples)
{
// Each element of the List is
// a tuple itself
tuple tuple =
currentTuple;
cout << "[ ";
// Printing tuple elements
cout << get<0>(currentTuple) << ' ' <<
get<1>(currentTuple) << ' ' <<
get<2>(currentTuple);
cout << ']';
cout << '\n';
}
}
// Driver code
int main()
{
// Declaring a List of tuples
// having first two values as string type
// and third value is of bool type
list>
listOftuples;
// Declaring a tuple
tuple tuple1;
// Initializing the
// tuple
tuple1 = make_tuple("GeeksforGeeks",
"Computer Science", 0);
// Push the tuple at the back
// in the List
listOftuples.push_front(tuple1);
// Declaring another tuple
tuple tuple2;
// Initializing the
// tuple
tuple2 = make_tuple("Java", "C++", 1);
// Push the tuple at the front
// in the List
listOftuples.push_front(tuple2);
// Declaring another tuple
tuple tuple3;
// Initializing the tuple
tuple3 = make_tuple("GFG", "C", 1);
// Push the tuple at the front
// in the List
listOftuples.push_front(tuple3);
// Declaring another tuple
tuple tuple4;
// Initializing the tuple
tuple4 = make_tuple("Swift", "Python", 0);
// Push the tuple at the front
// in the List
listOftuples.push_front(tuple4);
// Calling print function
print(listOftuples);
return 0;
}
输出:
[ Swift Python 0]
[ GFG C 1]
[ Java C++ 1]
[ GeeksforGeeks Computer Science 0]